Greetings!
I am currently using two infrared distance sensors sold on sparkfun: Infrared Proximity Sensor Long Range - Sharp GP2Y0A02YK0F - SEN-08958 - SparkFun Electronics
I have a simple sketch measuring a single sensor and some filtering. The second sketch I am trying to incorporate an additional IR sensor by putting the analog pins into an array so I may read both sensors values simultaneously. This is my issue; I am not receiving any value from the additional sensor pin.
I have eliminated basic hardware issues (enough current, working sensors, testing additional sensor+original code, etc) So this issue is purely code.
Here is my sketch thus far:
/////pin declaration/////////////////
const int sensor[] = {A0,A1}; //Analog 0 pin
const int red_led = 9; //digital pin 9
const int green_led = 7; //digital pin 7
////////////////////////////////////
boolean bool_var = false;
int sense_readings;
int i;
////////averager of 20 values//////////////////
const int numReadings = 20; //higher the number, more numbers read/averaged. slows program down
int readings[numReadings]; // the readings from the analog input
int index = 0; // the index of the current reading
int total = 0; // the running total
int average = 0; // the average
///////////////////////////////////////////////////
int counter;
void setup(){
Serial.begin(115200); //set your baud rate in arduino serial monitor
pinMode(red_led, OUTPUT);
pinMode(green_led, OUTPUT);
}
void loop(){
for (i = 0; i < 1; i++) {
sense_readings = analogRead(sensor[i]);
Serial.println(sensor[i]);
}
sensor_read();
}
int sensor_read(){
digitalWrite(red_led, LOW);
digitalWrite(green_led, HIGH);
for (int counting=0; counting <=20; counting++){ //this is a counter; number 100 inside is adjustable for longer delay
Serial.print("timer = ");
Serial.println(counting);
// subtract the last reading:
total= total - readings[index];
// read from the sensor:
readings[index] = sense_readings; //analogRead(sensor);
// add the reading to the total:
total= total + readings[index];
// advance to the next position in the array:
index = index + 1;
// if we're at the end of the array...
if (index >= numReadings)
// ...wrap around to the beginning:
index = 0;
// calculate the average:
average = total / numReadings;
// send it to the computer as ASCII digits
Serial.print("averaged sensor readings = ");
Serial.println(average);
Serial.println();
delay(300); // delay in between reads for stability
while(bool_var ==false && average >= 100){ // The sensor is spaced between the end of the wall by 4ft
digitalWrite(red_led, HIGH);
digitalWrite(green_led, LOW);
counting=0;
bool_var = true;
break;
}
if (counting ==20 && bool_var==true){
Serial.println("SEND A SIGNAL TO IP CAMERA");
digitalWrite(red_led, HIGH);
digitalWrite(green_led, LOW);
bool_var=false;
}
else if (average <100 && counting ==20){
digitalWrite(red_led, LOW);
digitalWrite(green_led, HIGH);
bool_var=false;
}
}
}
Have you tried it, refering to analogRead(A0) and analogRead(A1), directly ?
for (i = 0; i < 1; i++) {
Should be
for (i = 0; i < 2; i++) {
revised code:
/////pin declaration/////////////////
int sensors[] = {A0,A1};
const int red_led = 9; //digital pin 9
//const int yellow_led = 6; //main indicator/digital pin 6
const int green_led = 7; //digital pin 7
////////////////////////////////////
boolean bool_var = false;
int sensor_pins = sensors[2];
////////averager of 20 values//////////////////
const int numReadings = 20; //higher the number, more numbers read/averaged. slows program down
int readings2[numReadings]; // the readings from the analog input
int readings[numReadings]; // the readings from the analog input
int index = 0; // the index of the current reading
int total = 0; // the running total
int average = 0; // the average
///////////////////////////////////////////////////
int counter;
void setup(){
Serial.begin(115200); //set your baud rate in arduino serial monitor
pinMode(red_led, OUTPUT);
// pinMode(yellow_led, OUTPUT);
pinMode(green_led, OUTPUT);
}
void loop(){
//led_standby();
sensor_read();
}
int sensor_read(){
int read_sensor = analogRead(sensor_pins);
digitalWrite(red_led, LOW);
digitalWrite(green_led, HIGH);
for (int counting=0; counting <=20; counting++){ //this is a counter; number 100 inside is adjustable for longer delay
Serial.print("timer = ");
Serial.println(counting);
// subtract the last reading:
total= total - readings[index];
// read from the sensor:
readings[index] = read_sensor;
// add the reading to the total:
total= total + readings[index];
// advance to the next position in the array:
index = index + 1;
// if we're at the end of the array...
if (index >= numReadings)
// ...wrap around to the beginning:
index = 0;
// calculate the average:
average = total / numReadings;
// send it to the computer as ASCII digits
Serial.print("averaged sensor readings = ");
Serial.println(average);
Serial.println();
delay(200); // delay in between reads for stability
while(bool_var ==false && average >= 100){ // The sensor is spaced between the end of the wall by 4ft
digitalWrite(red_led, HIGH);
digitalWrite(green_led, LOW);
counting=0;
bool_var = true;
break;
}
if (counting ==20 && bool_var==true){
Serial.println("SEND A SIGNAL TO IP CAMERA");
digitalWrite(red_led, HIGH);
digitalWrite(green_led, LOW);
bool_var=false;
}
else if (average <100 && counting ==20){
digitalWrite(red_led, LOW);
digitalWrite(green_led, HIGH);
bool_var=false;
}
}
}
still not much luck
read_sensor = analogRead(sensor_pins);
is not going to work.
Read the reference section on how analogRead works.
You can not read two sensors simultaneously you can only read them one after the other.
/////pin declaration/////////////////
int analog0 = A0;
int analog1 = A1;
int sensor[2] = {analog0, analog1}; //Analog 0 pin
const int red_led = 9; //digital pin 9
const int green_led = 7; //digital pin 7
////////////////////////////////////
boolean bool_var = false;
int sense_readings;
int i;
////////averager of 20 values//////////////////
const int numReadings = 20; //higher the number, more numbers read/averaged. slows program down
int readings[numReadings]; // the readings from the analog input
int index = 0; // the index of the current reading
int total = 0; // the running total
int average = 0; // the average
///////////////////////////////////////////////////
int counter;
void setup(){
Serial.begin(115200); //set your baud rate in arduino serial monitor
pinMode(red_led, OUTPUT);
pinMode(green_led, OUTPUT);
}
void loop(){
for (i = 0; i < 2; i++) {
sense_readings = analogRead(sensor[i]);
// Serial.println(sensor[i]);
//int new_val = analogRead(sensor[i]);
Serial.println(sense_readings);
delay(300);
}
}
This seems to print out simultaneously the values of two sensors. Anyone confirm this? it seems fine.
Well it is hardly simultaneous is it with a 300mS delay and the delay caused by a print statement between readings.
While you can't get simultaneous readings because there is only one A/D converter on the arduino you can get better than that if you remove the delay and the print from the for loop.