I am brand new to Arduino and the IDE. I'm a fast learner and learned basic, c+/++, java, and SQL about a decade ago, so the knowledge is practically all in the airwaves now. I'm trying to read data from two motion sensors I have attached to a breadboard. The following is my code and I keep receiving errors for a few things, which will be detailed at the end of the sketch.
This is a sketch I found that works with one sensor, and I tried to just copy it for two more inputs to receive another sensor, which doesn't seem to work...
/*
HC-SR04 Ping distance sensor]
VCC to arduino 5v GND to arduino GND
Echo to Arduino pin 13 Trig to Arduino pin 12
Red POS to Arduino pin 11
Green POS to Arduino pin 10
560 ohm resistor to both LED NEG and GRD power rail
More info at: http://goo.gl/kJ8Gl
Original code improvements to the Ping sketch sourced from Trollmaker.com
Some code and wiring inspired by User:Dstaub/robotcar - Wikiversity
*/
#define trigPin 6
#define echoPin 7
#define led 11
#define led2 10
int x = 0;
int ledpin = 13;
int serialvalue;
void setup() {
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(led, OUTPUT);
pinMode(led2, OUTPUT);
digitalWrite(13,HIGH);
delay(600);
digitalWrite(13,LOW);
pinMode(12,OUTPUT);
pinMode(13,OUTPUT);
// for second sensor
pinMode(4, OUTPUT);
pinMode(5, INPUT);
pinMode(9, OUTPUT);
pinMode(8, OUTPUT);
digitalWrite(13,HIGH);
delay(600);
digitalWrite(13,LOW);
pinMode(12,OUTPUT);
pinMode(13,OUTPUT);
}
void loop() {
long duration, distance;
digitalWrite(4, LOW); // Added this line
delayMicroseconds(2); // Added this line
digitalWrite(4, HIGH);
// delayMicroseconds(1000); - Removed this line
delayMicroseconds(10); // Added this line
digitalWrite(4, LOW);
duration = pulseIn(5, HIGH);
distance = (duration/2) / 29.1;
if (distance < 4) { // This is where the LED On/Off happens
digitalWrite(9,HIGH); // When the Red condition is met, the Green LED should turn off
digitalWrite(8,LOW);
// Second Sensor
long duration, distance;
digitalWrite(trigPin, LOW); // Added this line
delayMicroseconds(2); // Added this line
digitalWrite(trigPin, HIGH);
// delayMicroseconds(1000); - Removed this line
delayMicroseconds(10); // Added this line
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
if (distance < 4) { // This is where the LED On/Off happens
digitalWrite(led,HIGH); // When the Red condition is met, the Green LED should turn off
digitalWrite(led2,LOW);
}
else {
digitalWrite(led,LOW);
digitalWrite(led2,HIGH);
digitalWrite(9,LOW);
digitalWrite(8,HIGH);
}
if (distance >= 200 || distance <= 0){
Serial.println("Out of range");
}
else {
Serial.print(distance);
Serial.println(" cm");
}
delay(500);
if (Serial.available() > 0){ // Check serial buffer for characters
serialvalue = Serial.read();
if (serialvalue == 'r') { // If an 'r' is received then read the pins (r = ascii code 114)
for (int pin= 0; pin<=5; pin++){ // Read and send analog pins 0-5
x = analogRead(pin);
sendValue (x);
}
for (int pin= 2; pin<=11; pin++){ // Read and send digital pins 2-11. Modify this so you're
x = digitalRead(pin); // only reading the pins that aren't used below for digital out
sendValue (x);
}
Serial.println(); // Send a carriage returnt to mark end of pin data.
delay (5); // add a delay to prevent crashing/overloading of the serial port
}
}
if(serialvalue == 0){
digitalWrite(12, LOW);
}
else if(serialvalue == 1){
digitalWrite(12, HIGH);
}
else if(serialvalue == 2){
digitalWrite(13, LOW);
}
else if(serialvalue == 3){
digitalWrite(13, HIGH);
}
//--------------------------End of receiving numbers---------------------------------
}//end of if (Serial.available() > 0
void sendValue (int x){ // function to send the pin value followed by a "space".
Serial.print(x);
Serial.print(32);
}
}
sketch_apr25d.ino: In function 'void loop()':
sketch_apr25d:97: error: 'sendValue' was not declared in this scope
sketch_apr25d:104: error: 'sendValue' was not declared in this scope
sketch_apr25d:130: error: a function-definition is not allowed here before '{' token
I eventually want to have two LED strips lined up vertically, with the sensors about 5 feet apart, that will turn on respectively when something passes the sensors in a horizontal motion.
Any help would be fantastic and greatly appreciated. Once I get this figured out I have lots of projects I plan on integrating into one giant art installation.
Thanks!