Warning

 

Close
Confirm Action

Are you sure you wish to do this?

Cancel Confirm
AR15.COM
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);
}
}
 
View Quote
7/5/2016 8:10:33 PM EDT
[#1]
Load it, push run, see what happens.

That's the best thing about Arduino.
7/5/2016 8:31:42 PM EDT
[#2]
Quote History
Quoted:
Load it, push run, see what happens.

That's the best thing about Arduino.
View Quote


No time. The robot is being rebuilt and I need to have the coding done by COB tomorrow.
7/5/2016 9:01:34 PM EDT
[#3]
Don't you need to do a pinMode for pin 13 as an input before you do this?

Sensor1=digitalRead(13);



7/5/2016 9:05:47 PM EDT
[#4]
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.


7/5/2016 9:25:59 PM EDT
[#5]
Quote History
Quoted:
Don't you need to do a pinMode for pin 13 as an input before you do this?

Sensor1=digitalRead(13);



View Quote



Error  - Thanks
7/5/2016 9:28:09 PM EDT
[#6]
Quote History
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.


View Quote


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.
7/5/2016 9:32:52 PM EDT
[#7]
But Sensor1 is not going to be >2 or >400

Sensor1 will be 0 or 1.



7/5/2016 10:02:29 PM EDT
[#8]
Quote History
Quoted:
But Sensor1 is not going to be >2 or >400

Sensor1 will be 0 or 1.



View Quote



Sensor1 (and 2 & 3) are ultrasonic range sensors. They measure distance so I want the robot to turn if it senses an object.
7/5/2016 10:44:10 PM EDT
[#9]
Quote History
Quoted:



Sensor1 (and 2 & 3) are ultrasonic range sensors. They measure distance so I want the robot to turn if it senses an object.
View Quote View All Quotes
View All Quotes
Quote History
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.

7/6/2016 5:15:02 AM EDT
[#10]
Quote History
Quoted:



Sensor1 (and 2 & 3) are ultrasonic range sensors. They measure distance so I want the robot to turn if it senses an object.
View Quote View All Quotes
View All Quotes
Quote History
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.



7/6/2016 11:33:40 AM EDT
[#11]
Goddamn it, we have code tags.  Use them.
7/6/2016 12:57:17 PM EDT
[#12]
As others have pointed out, digitalRead will only return HIGH/LOW (0/1), so the comparison against 2 (or 400) can never be true.