I have not used the Arduino in over 2 years and have forgotten a bunch of stuff I was wondering if someone could help me out. I have two analog sensors both the same connected to the appropriate analog pins. I am getting a read on the serial monitor and when i test the sensors the values are changing so i know the circuit is correct and everything is in the right place. I just have an issue with my code.
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
}
// the loop routine runs over and over again forever:
void loop() {
// read the input on analog pin 0:
int sensorValue1 = analogRead(A0);
// print out the value you read:
Serial.println(sensorValue1);
//Serial.print("val1 = " );
Serial.print(sensorValue1);
Serial.print(" ");
delay(1000); // delay in between reads for stability
// read the input on analog pin 1:
int sensorValue2 = analogRead(A1);
// print out the value you read:
Serial.println(sensorValue2);
//Serial.print("val2 = " );
Serial.print(sensorValue2);
Serial.print(" ");
delay(1000); // delay in between reads for stability
}
once i view this in the serial monitor it is not organized and the sensorvalue1 is skipping over to the next line. I would appreciate the help.
This is the serial output on the serial monitor
There are two sensors so there should be two values but in the picture the values are all messed up i would ideally like it to give me a two seperate values one for each sensor
val1= sensorvalue1 val2= sensorvalue2
2 Answers 2
Try something like this
void loop() {
// read the input on analog pin 0 and 1 with delays for stability:
delay(1000);
int sensorValue1 = analogRead(A0);
delay(1000);
int sensorValue2 = analogRead(A1);
// print out the value you read:
Serial.print("val1 = " );
Serial.print(sensorValue1);
Serial.print(" val2 = " );
Serial.println(sensorValue2);
}
The problem here seems like you are writing the value with a println(), and then again the value without new line but space. Then you write value2 with an ending newline and again value2 without newline. In your output there is also a string that does not match up anywhere in the code you posted. Double check what you are uploading and check if it has error.
analogRead()
doesn't happen before thedelay()
. If you switch pins with an extraanalogRead()
and throw away the value before the delay() you will get more stable readings.