Stap 4: Code
Hier is mijn code;
Please respect het kostte me tijd om te schrijven;
Code door Jason McLaughlin
2015
Const int channel_a_enable = 6;
Const int channel_a_input_1 = 4;
Const int channel_a_input_2 = 7;
Const int channel_b_enable = 5;
Const int channel_b_input_3 = 3;
Const int channel_b_input_4 = 2;
#define trigPin 10
#define echoPin 9
VOID Setup {}
pinMode (trigPin, OUTPUT);
pinMode (echoPin, INPUT);
pinMode (channel_a_enable, OUTPUT); Kanaal A inschakelen
pinMode (channel_a_input_1, OUTPUT); Kanaal A ingang 1
pinMode (channel_a_input_2, OUTPUT); Kanaal A ingang 2
pinMode (channel_b_enable, OUTPUT); Kanaal B inschakelen
pinMode (channel_b_input_3, OUTPUT); Kanaal B ingang 3
pinMode (channel_b_input_4, OUTPUT); Kanaal B ingang 4
Serial.begin (9600);
Serial.println ('gestart'); }
void loop {}
Ping/Ultra Sonic Sensor
int duur, afstand;
digitalWrite (trigPin, hoge);
delayMicroseconds(1000);
digitalWrite (trigPin, laag);
duur = pulseIn (echoPin, hoge);
afstand = (duur / 2) / 29.1;
Serial.Print(distance);
Serial.println ("cm");
delay(0);
Vooruit
if(Distance>5) {Serial.println ("vooruit:)");
analogWrite (channel_a_enable, 255);
digitalWrite (channel_a_input_1, hoge);
digitalWrite (channel_a_input_2, laag);
analogWrite (channel_b_enable, 255);
digitalWrite (channel_b_input_3, hoge);
digitalWrite (channel_b_input_4, laag);
delay(50);
allInputsOff();}
anders if(distance<5)
{ }
else {Serial.println ("kanaal A Idle");
analogWrite (channel_a_enable, 0);
digitalWrite (channel_a_input_1, laag);
digitalWrite (channel_a_input_2, laag);
delay(50);
allInputsOff();}
if(Distance<5)
{Serial.println("Turing");
analogWrite (channel_a_enable, 255);
digitalWrite (channel_a_input_1, laag);
digitalWrite (channel_a_input_2, hoge);
analogWrite (channel_b_enable, 255);
digitalWrite (channel_b_input_3, hoge);
digitalWrite (channel_b_input_4, laag);
delay(50);
allInputsOff();}
anders als (afstand > 5) {}
anders
{Serial.println ("kanaal A reverse Idle");
analogWrite (channel_a_enable, 0);
digitalWrite (channel_a_input_1, laag);
digitalWrite (channel_a_input_2, laag);
delay(50);
allInputsOff();}
}
VOID allInputsOff()
{
digitalWrite (4, laag);
digitalWrite (7, laag);
digitalWrite (6, laag);
digitalWrite (3, laag);
digitalWrite (2, laag);
digitalWrite (5, laag);
}