const int anPin1 = A0;
const int anPin2 = A1;
const int triggerPin = 2;
long anVolt1, anVolt2;
void setup() {
//This opens up a serial connection to shoot the results back to the PC console
Serial.begin(9600);
pinMode(triggerPin,OUTPUT);
delay(200); //Gives time for the sensors to boot and calibrate
}
void start_sensor(){
digitalWrite(triggerPin,HIGH);
delay(1);
digitalWrite(triggerPin,LOW);
}
void read_sensor(){
//Used to read in the analog voltage output that is being sent by the XL-MaxSonar device.
//Scale factor is (Vcc/1024) per centimeter. A 5V supply yields ~4.9mV/cm for standard range sensors
anVolt1 = analogRead(anPin1);
anVolt2 = analogRead(anPin2);
}
void printall() {
Serial.print("S1");
Serial.print("=");
Serial.print(anVolt1);
Serial.print("cm");
Serial.print(" ");
Serial.print("S2");
Serial.print("=");
Serial.print(anVolt2);
Serial.print("cm");
Serial.println();
}
void loop () {
start_sensor();
read_sensor();
printall();
delay(200); // This delay time changes by 100 for every sensor in the chain. For 4 sensors this will be 400
}
NOTE I put my hand in front of the sensors at one point, those are the values where both sensors read ~17-21cm.
S2 is consistently low, and occasionally jumps all over the place. I've checked many times to make sure I'm connected to the correct pins, I'm not sure what else to try. Any help is greatly appreciated!
the Arduino only has one ADC and this ADC multiplexes the analog ports.
That means a measurement on one can affect the other.
sometimes ignoring one read might help
void read_sensor()
{
//Used to read in the analog voltage output that is being sent by the XL-MaxSonar device.
//Scale factor is (Vcc/1024) per centimeter. A 5V supply yields ~4.9mV/cm for standard range sensors
anVolt1 = analogRead(anPin1);
anVolt1 = analogRead(anPin1);
anVolt2 = analogRead(anPin2);
anVolt2 = analogRead(anPin2);
}
robtillaart:
the Arduino only has one ADC and this ADC multiplexes the analog ports.
That means a measurement on one can affect the other.
sometimes ignoring one read might help
void read_sensor()
{
//Used to read in the analog voltage output that is being sent by the XL-MaxSonar device.
//Scale factor is (Vcc/1024) per centimeter. A 5V supply yields ~4.9mV/cm for standard range sensors
anVolt1 = analogRead(anPin1);
anVolt1 = analogRead(anPin1);
just to test, can reverse them? (might give an indication of what's happening)
void read_sensor()
{
//Used to read in the analog voltage output that is being sent by the XL-MaxSonar device.
//Scale factor is (Vcc/1024) per centimeter. A 5V supply yields ~4.9mV/cm for standard range sensors
anVolt2 = analogRead(anPin2);
anVolt2 = analogRead(anPin2);
anVolt1 = analogRead(anPin1);
anVolt1 = analogRead(anPin1);
}