1

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

3
  • Can you paste here the serial monitor output that you get, and write what you would expect? Otherwise your question won't be very clear to everyone.
    – jfpoilpret
    Commented Dec 19, 2014 at 5:37
  • jfpoilpret i have updated my question thank you
    – har
    Commented Dec 19, 2014 at 16:35
  • The delays likely won't affect the stability, since the switching of the ADC pins into the multiplexer in analogRead() doesn't happen before the delay(). If you switch pins with an extra analogRead() and throw away the value before the delay() you will get more stable readings.
    – Dave X
    Commented Mar 6, 2017 at 18:59

2 Answers 2

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);



 }
0
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.