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