Beginners Serial input problem

I am having trouble getting my program to wait for a key board input.

Task is to use the arduino uno to output a PWM waveform to some hardware and to wait whilst I make some measurements. I then plan to send a serial byte (any byte) to get it to move on, increase the PWM value by an predefined increment and repeat until the max value of 255 is reached.

the first wait loop WAIT1 is working and I can start the program, wait until I enter the stepsize but it gets stuck in the WAIT2 loop.

Please can anyone point me to why my code does not work please.

TIA

When I enter stepsze2, my serial window shows:

Enter step size 1-9 Step size =2
value is 0

My code is

int val=0;
int stepsize=1;
int dummy=0;
void setup() {

Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
Serial.print ("Enter step size 1-9 ");
WAIT1:
if (Serial.available()==0) {
goto WAIT1;//wait for key press
}
stepsize=Serial.read()-48;//convert from ASCII to number
Serial.print(“Step size =”);
Serial.println(stepsize,DEC);
Serial.print ("value is ");
Serial.println(val,DEC);
NEXT:
{
analogWrite(9,val);
Serial.flush();// might not be needed?
WAIT2:
if (Serial.available()==0) {
goto WAIT2; //wait for key press <<<CODE GETS STUCK HERE
}
dummy=Serial.read();// used to read the input buffer but I don’t use the data
val=val+stepsize;
if (val<=255) {
goto NEXT;
}
}
}

void loop() {
}

This is NOT basic. Ditch the stupid gotos and labels.

   while(Serial.available() == 0)
   {
      // Do nothing
   }

Where do you assign a value to val? Why would you expect it to contain anything other than 0?

Do NOT use GOTO. Not until you are experienced enough to be answering complex questions here :slight_smile: And by then you will probably realize you don't actually need it. :slight_smile:

Have a look at Serial Input Basics. I think you want to increment your PWM value every time the variable newData becomes true

...R

Thanks for your replies.

Sorted now.