Posted: 7/5/2016 8:04:43 PM EDT
|
Code did not work nor did the robot, class is over. Thanks.
https://www.arduino.cc/en/Main/ArduinoBoardUno This is for my robot. All I want it to do is to go straight and turn left or right it the area is clear. It will use ultrasonic sensors (forgot the type), one straight ahead and one on each side. #define trigPin 8 #define echoPin 12 void setup() { Serial.begin (9600); pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); pinMode(9, OUTPUT); pinMode(10, OUTPUT); } void loop() { int Sensor1; float duration, distance; digitalWrite(trigPin, LOW); delayMicroseconds(2); digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); duration = pulseIn(echoPin, HIGH); distance = (duration / 2) * 0.0344; if (distance >=400 || distance <= 2){ Serial.print('Distance = '); Serial.println("Out of range"); } else { Serial.print("Distance = "); Serial.print(distance); Serial.println(" cm"); } delay(500); Sensor1=digitalRead(13); if(Sensor1 > 2) { digitalWrite(9, HIGH); //Turn left digitalWrite(10,LOW); } if(Sensor1 > 400) { digitalWrite(10, HIGH); //Turn right digitalWrite(9, LOW); } } |
|
And also, this does not make sense to me:
Sensor1=digitalRead(13); if(Sensor1 > 2) { digitalWrite(9, HIGH); //Turn left digitalWrite(10,LOW); } if(Sensor1 > 400) { digitalWrite(10, HIGH); //Turn right digitalWrite(9, LOW); } What are you trying to accomplish there? The output of digitalRead() is assigned to Sensor1 - that is either going to be HIGH or LOW - it will not be a distance value. |
|
Quoted:
And also, this does not make sense to me: Sensor1=digitalRead(13); if(Sensor1 > 2) { digitalWrite(9, HIGH); //Turn left digitalWrite(10,LOW); } if(Sensor1 > 400) { digitalWrite(10, HIGH); //Turn right digitalWrite(9, LOW); } What are you trying to accomplish there? The output of digitalRead() is assigned to Sensor1 - that is either going to be HIGH or LOW - it will not be a distance value. The first part is to set up one sensor. I need to setup 3 sensors. Second part is what my buddy helped me with. 2 pins on the UNO will be used to control 2 motors. The // means that is a non recognized command aka comment. |
|
Quoted:
Sensor1 (and 2 & 3) are ultrasonic range sensors. They measure distance so I want the robot to turn if it senses an object. Quoted:
Quoted:
But Sensor1 is not going to be >2 or >400 Sensor1 will be 0 or 1. Sensor1 (and 2 & 3) are ultrasonic range sensors. They measure distance so I want the robot to turn if it senses an object. They measure distance, but what do they output. This is why it's so difficult to troubleshoot other people's builds and code over the internet when you don't have the hardware to measure, test it on, or at least read the spec sheets. |
|
Quoted:
Sensor1 (and 2 & 3) are ultrasonic range sensors. They measure distance so I want the robot to turn if it senses an object. Quoted:
Quoted:
But Sensor1 is not going to be >2 or >400 Sensor1 will be 0 or 1. Sensor1 (and 2 & 3) are ultrasonic range sensors. They measure distance so I want the robot to turn if it senses an object. Part of your code is measuring distance via this call: duration = pulseIn(echoPin, HIGH); distance = (duration / 2) * 0.0344; But later in your code it looks like you are trying to measure distance on pin 13 with a digitalRead call. Again, digitalRead returns either HIGH or LOW, which will be either the number 0 or the number 1, but will not be a distance reported by a sensor. |