Hello, I am fairly new to the world of Arduino but I'm eager to learn! I've made a few small blinking and control projects and am looking to put together something slightly more sophisticated for a class project. I purchased four SR-04 ultrasonic distance sensors to connect to an old rock crawler R/C car. I have the sensors up and running and the code functions well (thanks to mem and mjmcondor) but I need a little help from here. I'm sure this has been answered elsewhere but after days of research I am stuck...
I am using an array to control multiple ultrasonic distance sensors. I have no coding experience, so I am trying to figure out how to pull a value from that array on a specific pin, and assign that value to a function.
For example, if no sensors read less than 6 inches, esc writes 94 (a slow crawl), but if an object is detected within 6 inches on sensor pin one within the array, esc writes 89 (stop).
Any help is greatly appreciated. If I can be more specific with my question, let me know. I'm not sure I made sense...
Below is a copy of the code I am trying to modify (again, thanks to mem and mjmcondor for the initial code):
#include <Servo.h>
Servo servo; // create servo object to control a servo for steering
Servo esc; //create servo object to control an esc for motor speed
int val;//speed of motor
int ultraSoundSignalPins[] = {13,12,11,10}; // Front Left,Front, Front Right, Rear Ultrasound signal pins
char *pingString[] = {"Left ","Front ", "Right ", "Rear "}; // just something to print to indicate direction
const int front = 10;//front sensor on pin 10
const int right = 11;//right sensor on pin 11
const int left = 12;// left sensor on pin 12
const int rear = 13;//rear sensor on pin 12
void setup()
{
servo.attach(6); // attaches a servo to pin 6
esc.attach(5);//attaches an esc to pin 5 Serial.begin(9600);//begine serial monitor
}
void loop()
{
unsigned long ultrasoundValue;
for(int i=0; i < 4; i++)//for every interger less than 4, cycle one pin
{
ultrasoundValue = ping(i); Serial.print(pingString*);*
_ Serial.print(ultrasoundValue);_
_ Serial.print("in, "); _
delay(50);*
}*
_ Serial.println();_
delay(50);*
_ if (untrasoundSignalPins == 11 && echo / 58.138 <= 6){//**This is the part_
//I don't understand. How do I pull am individual pin's value and assign*
//an output to that value?*
esc.write(89);*
}*
else{*
esc.write(94);*
}*
*} * //Ping function unsigned long ping(int i) {
unsigned long echo;*
_ pinMode(ultraSoundSignalPins*, OUTPUT); // Switch signalpin to output*_ _ digitalWrite(ultraSoundSignalPins*, LOW); // Send low pulse*
* delayMicroseconds(2); // Wait for 2 microseconds* digitalWrite(ultraSoundSignalPins*, HIGH); // Send high pulse* * delayMicroseconds(5); // Wait for 5 microseconds* digitalWrite(ultraSoundSignalPins*, LOW); // Holdoff* pinMode(ultraSoundSignalPins*, INPUT); // Switch signalpin to input* digitalWrite(ultraSoundSignalPins*, HIGH); // Turn on pullup resistor* echo = pulseIn(ultraSoundSignalPins*, HIGH); //Listen for echo* return (echo / 58.138) * .39; //convert to CM then to inches_ } [/quote]
Thanks for the input, grumpy_mike. Like I said, I'm fairly new at this so I must not be asking the right question. (Probably why I cant find the answer...)
I attached the source code I was able to get functioning with the Arduino and four sonic distance sensors. When the sketch is loaded, the serial monitor displays the correct distances on each sensor so I know the code functions properly and I know I'm getting the sensor information to the Arduino.
Now I want to take the information coming in from each sensor and program a specific response based on conditions.
The array in the working sketch reads the distance from each sensor one at a time. All I need to to is figure out how to say something along the lines of;
if (reading pin 10 && distance <= 6 inches){
perform some action}
if (reading pin 11 && distance <= 6 inches){
perform some other action}
I have a feeling I'm still not asking the right question, but I appreciate any help, even if it's telling me what I'm asking doesn't make sense, haha!
Here's the working source code:
int ultraSoundSignalPins[] = {7,8,9,12}; // Front Left,Front, Front Right, Rear Ultrasound signal pins
char *pingString[] = {"Front Left ","Front ", "Front Right ", "Rear "}; // just something to print to indicate direction
delay(50);*
*} * //Ping function unsigned long ping(int i) {
unsigned long echo;*
_ pinMode(ultraSoundSignalPins*, OUTPUT); // Switch signalpin to output*_ _ digitalWrite(ultraSoundSignalPins*, LOW); // Send low pulse*
* delayMicroseconds(2); // Wait for 2 microseconds* digitalWrite(ultraSoundSignalPins*, HIGH); // Send high pulse* * delayMicroseconds(5); // Wait for 5 microseconds* digitalWrite(ultraSoundSignalPins*, LOW); // Holdoff* pinMode(ultraSoundSignalPins*, INPUT); // Switch signalpin to input* digitalWrite(ultraSoundSignalPins*, HIGH); // Turn on pullup resistor* echo = pulseIn(ultraSoundSignalPins*, HIGH); //Listen for echo* return (echo / 58.138) * .39; //convert to CM then to inches_ } [/quote] Basically, in the context of the sketch (I hope...) when (i) is pin 10 ("Front" for instance) and ([echo / 58.138] *.39) is equal to 6 inches, do something.
When you attach code you do not use the quote icon you use the one next to it with <> over it. That way there is a proper box we can copy from.
What are version of the IDE are you using? I am on 1.0.6 and this will not compile.
The lines that say:-
pinMode(ultraSoundSignalPins, OUTPUT); // Switch signalpin to output
Should mention the specific pin like this:-
pinMode(ultraSoundSignalPins[i], OUTPUT); // Switch signalpin to output
The line:-
return (echo / 58.138) * .39; //convert to CM then to inches
Is mixing up int maths with floating point numbers. Do it all in floating point and then convert to an int. There is no need for a long here.
Use print statements to see if the sensors are returning the right values.
I'm not sure whats going on with the copying of the sketch, I tried using the IDE's "Copy for forum" but it seems to be finicky, let me try again;
int ultraSoundSignalPins[] = {7,8,9,12}; // Front Left,Front, Front Right, Rear Ultrasound signal pins
char *pingString[] = {"Front Left ","Front ", "Front Right ", "Rear "}; // just something to print to indicate direction
void setup()
{
Serial.begin(9600);
}
void loop()
{
unsigned long ultrasoundValue;
for(int i=0; i < 4; i++)
{
ultrasoundValue = ping(i);
Serial.print(pingString[i]);
Serial.print(ultrasoundValue);
Serial.print("in, ");
delay(50);
}
Serial.println();
delay(50);
}
//Ping function
unsigned long ping(int i)
{
unsigned long echo;
pinMode(ultraSoundSignalPins[i], OUTPUT); // Switch signalpin to output
digitalWrite(ultraSoundSignalPins[i], LOW); // Send low pulse
delayMicroseconds(2); // Wait for 2 microseconds
digitalWrite(ultraSoundSignalPins[i], HIGH); // Send high pulse
delayMicroseconds(5); // Wait for 5 microseconds
digitalWrite(ultraSoundSignalPins[i], LOW); // Holdoff
pinMode(ultraSoundSignalPins[i], INPUT); // Switch signalpin to input
digitalWrite(ultraSoundSignalPins[i], HIGH); // Turn on pullup resistor
echo = pulseIn(ultraSoundSignalPins[i], HIGH); //Listen for echo
return (echo / 58.138) * .39; //convert to CM then to inches
}
I'm using IDE 1.0.6 and the sketch compiles and loads on my machine...
Would you happen to know of any resources beyond the Ardino.cc tutorial and reference pages? I'm happy to do the research myself, but so far have come up fruitless...
At the risk of sounding completely helpless, I'm not even sure how to convert a floating point into an int. As of this post the sensors are all returning correct values. I'm off to see if I can find how to pull off that suggestion, thanks again grumpy_mike.
Let me know if my attached code reads properly this time. I haven't posted in this forum before, and I swear, I'm not totally useless, maybe just a bit of a slow learner, haha!