Created
September 30, 2018 09:37
-
-
Save dbousamra/819be480c45016d2a80c34fe1533b54c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const int trigPin = 8; | |
const int echoPin = 10; | |
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; | |
void setup() { | |
// put your setup code here, to run once: | |
pinMode( channel_a_enable, OUTPUT ); // Channel A enable | |
pinMode( channel_a_input_1, OUTPUT ); // Channel A input 1 | |
pinMode( channel_a_input_2, OUTPUT ); // Channel A input 2 | |
pinMode( channel_b_enable, OUTPUT ); // Channel B enable | |
pinMode( channel_b_input_3, OUTPUT ); // Channel B input 3 | |
pinMode( channel_b_input_4, OUTPUT ); // Channel B input 4 | |
Serial.begin( 9600 ); | |
Serial.println("Starting up"); | |
} | |
void loop() { | |
// establish variables for duration of the ping, | |
// and the distance result in inches and centimeters: | |
Serial.println("Channel A forward"); | |
analogWrite( channel_a_enable, 255); | |
digitalWrite( channel_a_input_1, HIGH); | |
digitalWrite( channel_a_input_2, LOW); | |
long duration, inches, cm; | |
// The sensor is triggered by a HIGH pulse of 10 or more microseconds. | |
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse: | |
pinMode(trigPin, OUTPUT); | |
digitalWrite(trigPin, LOW); | |
delayMicroseconds(2); | |
digitalWrite(trigPin, HIGH); | |
delayMicroseconds(10); | |
digitalWrite(trigPin, LOW); | |
// Read the signal from the sensor: a HIGH pulse whose | |
// duration is the time (in microseconds) from the sending | |
// of the ping to the reception of its echo off of an object. | |
pinMode(echoPin, INPUT); | |
duration = pulseIn(echoPin, HIGH); | |
// convert the time into a distance | |
cm = microsecondsToCentimeters(duration); | |
if (cm > 150) | |
{ | |
Serial.println("Channel A forward"); | |
analogWrite( channel_a_enable, 255); | |
digitalWrite( channel_a_input_1, HIGH); | |
digitalWrite( channel_a_input_2, LOW); | |
analogWrite( channel_b_enable, 255); | |
digitalWrite( channel_b_input_3, HIGH); | |
digitalWrite( channel_b_input_4, LOW); | |
} else { | |
analogWrite( channel_a_enable, 255); | |
digitalWrite( channel_a_input_1, HIGH); | |
digitalWrite( channel_a_input_2, LOW); | |
analogWrite( channel_b_enable, 255); | |
digitalWrite( channel_b_input_3, LOW); | |
digitalWrite( channel_b_input_4, HIGH); | |
} | |
} | |
long microsecondsToCentimeters(long microseconds) | |
{ | |
return microseconds / 29 / 2; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment