Stap 7: Code
De onderstaande code in de Arduino waarmee de motoren geladen.
int kosten = 0; int val = 0; int val1 = 0; int valm = 0; int valm1 = 0; int sprong = 3; int ctr = 0; //counter const int pwmA = 3; Const int pwmB = 11; Const int brakeA = 9; Const int brakeB = 8; Const int dirA = 12; Const int dirB = 13; Const int rechts = 5; Const int links = 7; Const int chglt = 6; void setup {pinMode (dirA, OUTPUT); pinMode (brakeA, OUTPUT); pinMode (dirB, OUTPUT); pinMode (brakeB, OUTPUT); pinMode(right,INPUT); pinMode(left,INPUT); pinMode(chglt,OUTPUT); digitalWrite (dirA, hoge); //forward A digitalWrite (brakeA, LOW); //release brakeA analogWrite (pwmA, 100); //set snelheid A digitalWrite (chglt, hoge); //test gratis licht digitalWrite (dirB, hoge); //forward B motor digitalWrite(brakeB,LOW); / / analogWrite (pwmB, 100); //set snelheid B delay(700), digitalWrite (chglt ,Low); //set opladen licht af} ongeldig lus () {als (springen == 0) {analogWrite (pwmA, 0); //charge gevonden analogWrite(pwmB,0); digitalWrite(chglt,HIGH); delay(50000);} als (springen == 1) {valm=analogRead(0); valm1=analogRead(1); als (valm > 500 of valm1 > 500) {valm=analogRead(0); valm1=analogRead(1); als (valm > 450 of valm1 > 450) {digitalWrite (brakeA, hoge); digitalWrite (brakeB, hoge); digitalWrite (dirA LAAG); //reverse A digitalWrite (brakeA, laag); / / analogWrite (pwmA, 200); digitalWrite(dirB,LOW); digitalWrite(brakeB,LOW); analogWrite(pwmB,200); vertraging (700), //backup digitalWrite (brakeA, hoge); //stop één wiel analogWrite(pwmA,0); delay(1000); digitalWrite (brakeB, hoge); //stop andere wiel //start beide wielen voorwaartse digitalWrite(dirA,HIGH); digitalWrite(brakeA,LOW); analogWrite(pwmA,140); digitalWrite(dirB,HIGH); digitalWrite(brakeB,LOW); analogWrite(pwmB,140); delay(1000); }}} //find station als (springen == 3) {analogWrite(pwmA,0); analogWrite(pwmB,0); val=digitalRead(right); val1=digitalRead(left); als (val == HIGH & & val1 == HIGH) {analogWrite(pwmA,140); analogWrite(pwmB,140); als (ctr < 150) {ctr = ctr + 1; kosten = analogRead (2); //test voor gratis station if(charge>100) {springen = 0; ctr = 5000;} if(ctr==150) {ctr = 0;}}} als (val == HIGH & & val1 == LOW) {analogWrite (pwmA 190); analogWrite(pwmB,50); Als (ctr < 150) {ctr = ctr + 1; kosten = analogRead (2); //test voor gratis station if(charge>100) {springen = 0; ctr = 5000;} if(ctr==150) {ctr = 0;}}} Als (val1 == HIGH & & val == LOW) {analogWrite(pwmB,190); analogWrite(pwmA,50); als (ctr < 150) {ctr = ctr + 1; kosten = analogRead (2); //test voor gratis station if(charge>100) {springen = 0; ctr = 5000;} if(ctr==150) {ctr = 0;}}}}}