incrimenting or decrimenting when button is pressed

I am trying to write code that will increment or decrement a voltage when I press either 1 or 2, for now I am just testing it on the serial monitor.

When I press 2 the voltage will not decrement.

Here is my code:

float volt=0.0;

void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Serial.print(volt);
}

void loop() {
// put your main code here, to run repeatedly:
if (Serial.available()>0){
if (Serial.read()==49){
volt= volt+0.5;
Serial.print(volt);
}
else if (Serial.read()==50){
volt = volt-0.5;
Serial.print(volt);
}
}

}

any suggestions?

any suggestions?

Read once. Compare twice.

void loop()
{
   if(Serial.available()>0)
   {
      char ltr = Serial.read();
      if(ltr == '1')
      {
        volt= volt+0.5;
        Serial.print(volt);
      }
      else if(ltr == '2')
      {
         volt = volt-0.5;
         Serial.print(volt);
      }
   }
}

Notice too how I added spaces in the right places, removed useless spaces and comments, and lined stuff up.

I took your code, placed it in the IDE, and then used Ctrl-T from the Tools menu to have the IDE reformat your code:

float volt = 0.0;

void setup() {
  Serial.begin(9600);
  Serial.print(volt);
}

void loop() {
  if (Serial.available() > 0) {
    if (Serial.read() == 49) {
      volt = volt + 0.5;
      Serial.print(volt);
    }
    else if (Serial.read() == 50) {
      volt = volt - 0.5;
      Serial.print(volt);
    }
  }

}

Another thing I like about the auto-format is the spacing it inserts around expressions. To me, that makes everything easier to read. I would also suggest you follow Paul's lead and remove the comments as they add nothing at this point. Finally, please read the How to Post sticky at the top of the Forum, especially on the use of code tags when posting source code.

I like the idea!

I added float volt=0.0; at the beginning because it was undeclared.

I then got this error message:

core/core.a(main.cpp.o): In function main': /Users/Andy/Downloads/Arduino.app/Contents/Java/hardware/arduino/avr/cores/arduino/main.cpp:43: undefined reference to setup'
collect2: error: ld returned 1 exit status
exit status 1
Error compiling.

Any idea what the problem is now?

Thanks!

I see. It works now, thanks!

You could have used the code in post #2 as well...

econjack:
You could have used the code in post #2 as well...

You could have, but really shouldn't, for the reason set out in reply#1

AWOL:
You could have, but really shouldn't, for the reason set out in reply#1

Some time ago you dinged me for giving an answer rather than letting the poster do some of the work. That's what I did here. I just assumed he would implement your changes to the code.

I haven't posted any code.
Reading the serial port twice when you're only sure that there's one character to read is silly.

It was Paul's code that I used, and he didn't bother to correct it, probably also leaving it for the poster to correct.

econjack:
It was Paul's code that I used, and he didn't bother to correct it, probably also leaving it for the poster to correct.

It most certainly was NOT my code. I did correct all the errors in OPs code.