Académique Documents
Professionnel Documents
Culture Documents
Trnh by bi: Nguyn Xun Hiu Ging vin B mn H thng in i hc Nng nghip H Ni
Thng 11-2013
+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
+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:
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
+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
+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