Vous êtes sur la page 1sur 15

MOBILE ROBOT LP TRNH C BN S DNG ARDUINO IDE

Trnh by bi: Nguyn Xun Hiu Ging vin B mn H thng in i hc Nng nghip H Ni

Thng 11-2013

FIRST ARDUINO PROGRAM


1.Mc tiu: Kt thc phn ny, hc vin c th: +nh ngha cc chn s l u vo hay u ra +a tn hiu s u ra ln cao hay xung thp +Nhn d liu u vo s +Np xut chng trnh bi Arduino 2.Bc lp trnh +Chy chng trnh Arduino IDE, theo m lnh sau:
void setup() { //put your setup code here, to run once: pinMode(13,OUTPUT); // configure pin 13 as output, LED5 (LED on the right) pinMode(2,INPUT); //configure pin 2 as input, SW2 (push button on the right) } void loop() { //put your main code here, to run repeatedly: if (digitalRead(2)==HIGH) {//if push button is not pressed, button read as high digitalWrite(13,HIGH); //turn off LED5 if push button is not pressed } else { //else, button is pressed digitalWrite(13,LOW); //turn on LED5 } }

+To th mc tn l MobileRobot trong My document/arduino/.., lu chng trnh ny vi tn l Worksheet1 trong th mc va lp +Xut chng trnh n mobile robot. Quan st tn hiu ca n LED bn phi +Kim tra n LED ca bn bng cch bt tt c cc n LED ln (nh nh ngha cc chn l u ra) 3.Luyn tp +Thay i chng trnh; bt n LED2 khi bm nt SW3. Lu chng trnh ny vi tn Worksheet1_EX1 trong th mc MobileRobot + Vit chng trnh mi, LED2 s tt khong 5 ln trong thi gian 200 mili giy nu bm nt SW3. Lu chng trnh ny vi tn Worksheet1_EX2 trong th mc MobileRobot

C D LIU SENSOR
1.Mc tiu: Kt thc phn ny, hc sinh c th: +M t u tn hiu tng t lm u vo +c d liu tng t v bt n LED cn c vo tn hiu tng t +Xut chng trnh vo Arduino 2.Bc thc hin: +Chy chng trnh trong Arduino theo cc cu lnh sau:
int sensepin = A6; int LED2=A3, LED3=A1, LED4=4, LED5=13; void setup() { //put your setup code here, to run once: pinMode(LED2,OUTPUT); // configure pin A3 as output, LED2 pinMode(LED3,OUTPUT); //configure pin A1 as output, LED3 pinMode(LED4,OUTPUT); // configure pin 4 as output, LED4 pinMode(LED5,OUTPUT); //configure pin 13 as output, LED5 pinMode(sensepin,INPUT); //configure pin A6 as input, sensor1 } void loop() { //put your main code here, to run repeatedly: int sensorData=analogRead(sensepin); //read sensor1 (pin A6) and store //then in variable name "SensorData" int ledLevel=map(sensorData, 0, 512, 0, 4); //map the sensorData then store // in ledLevel //turn on Led according to sensor level if (ledLevel==0) { digitalWrite(LED2,HIGH); digitalWrite(LED3,HIGH); digitalWrite(LED4,HIGH); digitalWrite(LED5,HIGH); }

else if (ledLevel==1) { digitalWrite(LED2,LOW); digitalWrite(LED3,HIGH); digitalWrite(LED4,HIGH); digitalWrite(LED5,HIGH); } else if (ledLevel==2) { digitalWrite(LED2,HIGH); digitalWrite(LED3,LOW); digitalWrite(LED4,HIGH); digitalWrite(LED5,HIGH); } else if (ledLevel==3) { digitalWrite(LED2,HIGH); digitalWrite(LED3,HIGH); digitalWrite(LED4,LOW); digitalWrite(LED5,HIGH); } else if (ledLevel==4) { digitalWrite(LED2,HIGH); digitalWrite(LED3,HIGH); digitalWrite(LED4,HIGH); digitalWrite(LED5,LOW); } }

+Lu chng trnh ny vi tn Worksheet2 trong th mc MobileRobot +Xut chng trnh sang mobile robot. Chm vo Sensor SEN1 v quan st cc n LED +Lp li chng trnh bng cch s dng cc sensor SEN2, SEN3 v SEN4 3.Luyn tp: +Vit chng trnh bt n LED5 nu sensor SEN1 ln hn 128. Lu chng trnh vi tn Worksheet2_Ex1 trong th mc MobileRobot +Thay i chng trnh trn, bt n LED2, LED3 v LED4 bng cch s dng cc sensor SEN2, SEN3, v SEN4. Lu chng trnh vi tn Worksheet2_Ex2 trong th mc MobileRobot

GIAO TIP NI TIP


1.Mc tiu: +Cho gi tr ban u ca giao thc ni tip +a vn bn v hin th ln mn hnh ni tip +Xut chng trnh trn Arduino 2.Th t thc hin: +Chy chng trnh trn Arduino theo m lnh sau: void setup() { //put your setup code here, to run once: Serial.begin(9600); // initialize serial comm at 9600 baud rate } int number=0; void loop() { //put your main code here, to run repeatedly: Serial.print("The number is"); //print on screen Serial.println(number); //print the number with line feed delay(500); //delay 500 ms number++; //increment by 1 }

+Lu chng trnh trn vi tn Worksheet3 trong th mc MobileRobot +Xut chng trnh ny ra Robot +Chn Tools > Serial Monitor +Quan st on vn bn c hin th trn cng ni tip (mn hnh) 3.Luyn tp: +Vit chng trnh thc hin vic m t 0 n 100 v hin th trn cng ni tip. V d:

One Two Three

Lu chng trnh ny vi tn Worksheet3_Ex1 trong th mc MobileRobot

IU KHIN MOBILE ROBOT T MY TNH


1.Mc tiu Kt thc phn ny, hc sinh c th: +Chn gi tr ban u cho giao tip ni tip +iu khin LED2 t my tnh +Xut chng trnh ra Arduino 2.Th t +Chy Arduino IDE theo m lnh sau:
//this program use computer keyboard to turn on or off LED //press number 1 to turn on LED //press number 0 to turn off LED int ledPin=A3; void setup() { //put your setup code here, to run once: pinMode(ledPin,OUTPUT); // configure pin A3 as output Serial.begin(9600); } void loop() { //put your main code here, to run repeatedly: while (Serial.available()==0); //wait for keyboard press int val=Serial.read()-'0'; //get the number being pressed

if (val==1) { //if key '1' is pressed, tunr on LED digitalWrite(ledPin,LOW); Serial.println("The LED is ON"); } else if (val==0) { //if key '0' is pressed, turn off LED digitalWrite(ledPin,HIGH); Serial.println("The LED is OFF"); } else { Serial.println("Invalid input"); //if any other key press, ignore Serial.flush(); //Flushes the buffer of incoming serial data } }

+Lu chng trnh trn di tn Worksheet4 trong th mc MobileRobot +Xut chng trnh ra mobile robot +Chn Tools > Serial Monitor +Gi k t 1 v quan st LED2, gi k t 0 v li quan st LED2 3.Luyn tp Thay i chng trnh trn s dng cc gi thit sau: a.Bt LED2 nu nhn c k t 1 b.Bt LED3 nu nhn c k t 2 c.Bt LED4 nu nhn c k t 3 d.Bt LED5 nu nhn c k t 4 e.Tt tt c cc n nu nhn c k t 0 Lu chng trnh vi tn Worksheet4_Ex1 trong th mc MobileRobot

HIN TH D LIU SENSOR TRN MN HNH


1.Mc tiu Kt thc phn ny, hc vin c th: +Chn gi tr ban u cho giao tip ni tip +Gi on vn bn v hin th n trn mn hnh ni tip Xut chng trnh trn Arrduino 2.Th t +Chy chng trnh trn Arduino theo m lnh sau:
void setup() { //put your main code here, to run once: Serial.begin(9600); //initialize serial comm at 9600 baudrate pinMode(A6,INPUT); //configure pin A6 as input, sensor1 (sensor on the right) } void loop() { //put your main code here, to run repeatedly: int val=analogRead(A6); //read sensor (pin A6) and store then in val; Serial.println(val); //print the sensor data on computer screen delay(250); //delay for 0.25 second }

+Lu chng trnh vi tn Worksheet5 trong th mc MobileRobot +Xut chng trnh ra mobile robot +Chn Tools > Serial Monitor +t robot ln rnh, di chuyn n quanh ng trng v quan st on vn bn hin th trn cng ni tip +Cng c th hin th thng tin vi d liu ca sensor khc 3.Luyn tp +Vit chng trnh hin th d liu ca tt c cc sensor trn mn hnh. Lu chng trnh ny vi tn Worksheet5_Ex1 trong th mc MobileRobot

NGT 1 MILI GIY


1.Mc tiu Kt thc phn ny, hc vin c th: +Chn gi tr ban u cho giao tip ni tip +Chn ngt trong thi gian l 1 mili giy +Xut chng trnh trn Arduino +Hin th on vn bn trn cng ni tip (mn hnh) 2.Th t +Download Timer1.zip t trang .... +Gii nn v lu vo th mc trong My document/arduino/libraries/.. (Cn khi ng li chng trnh arduino ide th vin cp nht thm file d liu) +Chy Arduino IDE. Chn File > Examples > 01.Basics > BareMinimum Chn Sketch>Import Library...>TimerOne_v9 +S dng m lnh sau:
//this is a test program on interrupt //and display counter on terminal #include <TimerOne.h> void setup() { //put your setup code here, to run once: Serial.begin(9600); //initialize serial comm at 9600 baudrate int frequency=1000000/1000; //running at 1000 Hz Timer1.initialize(frequency); //set a timer of length 100000 microseconds //attach the service routine here

NGT 1 MILI GIY


Timer1.attachInterrupt(oneMilisecondInterrupt); } void loop() { //put your main code here, to run repeatedly: } int counter=0, oneMillisecondCnt=0; void oneMilisecondInterrupt() { oneMillisecondCnt++; counter++; Serial.println(counter); } }

+Lu chng trnh trn vi tn Worksheet6 trong th mc MobileRobot +Xut chng trnh ra mobile robot +Chn Tools > Serial Monitor +Quan st on vn bn hin th trn cng ni tip 3.Luyn tp +Vit chng trnh m t 0 n 100 v hin th trn cng ni tip (mn hnh) nhng cn hin th t ting anh, v d: One Two Three Lu chng trnh ny vi tn Worksheet6_Ex1 trong th mc MobileRobot

S DNG DY
1.Mc tiu Kt thc phn ny, hc vin c th: +Chn gi tr ban u cho tt c cc chn u ra cho n LED s dng dy +Xut chng trnh trn arduino 2.Th t +Chy Arduino IDE, theo m lnh sau y:
byte LED[ ]={A3, A1, 4, 13}; //led pin void setup() { //put your setup code here, to run once: for (int i=0; i<4 ; i++) { pinMode(LED[i],OUTPUT); //configure all LED pins as output digitalWrite(LED[i],HIGH); //turn off all LED } } void loop() { //put your main code here, to run repeatedly: for (int i=0; i<4; i++) { digitalWrite(LED[i],LOW); //turn on all LED delay(200); //delay for 0.2 second digitalWrite(LED[i],HIGH); // turn off all LED delay(200); // delay for 0.2 second } }

+Lu chng trnh li di tn Worksheet7 trong th mc MobileRobot +Xut chng trnh n mobile robot 3.Luyn tp +S dng dy ci t thng s ban u cho chn sensor. Lu chng trnh di tn Worksheet7_Ex1 trong th mc MobileRobot +Thay i chng trnh trn hin th d liu ca tt c cc sensor trn mn hnh ni tip. Lu chng trnh li di tn Worksheet7_Ex2 trong th mc MobileRobot

Vous aimerez peut-être aussi