Program flow question

Hi

I am trying to do the following:

Data line is held high until the button is pressed, once the button is pressed the following sequence is initiated.

  1. A preamble is sent

  2. First set of data is sent

3 Second set of data is sent

The button in tested again and if still pressed the second set of data is sent again, this second set of data is repeated until the button is released, where upon the flow of the program returns to the start and waits for the button to be pressed again.

I have placed an "if" at the end of the second set of data to test the state of the button, if the result is that the button is still pressed a "goto" sends the program flow to "subsequent:" at the start of the second set of data.........this is where the problem seems to lie......can a "goto" only work if the label is further forward in the program?

Should I be using a completley different approach?

When the button is pressed data is sent, but on releasing the button data continues to be sent.

delays have been included between the data sets so that I can see what is going on on my 'scope

The button is held "high" by a 10k resistor to +5V, pressing the button takes the pin low to 0V

Can you help please?

const int data = 12;
const int xfcbutton = 2; 

int buttonState = 1;




// the setup routine runs once when you press reset:
void setup() {                
  // initialize the digital pin as an output.
  pinMode(data, OUTPUT); 

  pinMode(xfcbutton, INPUT);
  digitalWrite(data, HIGH); // sets data out to high initially


}
//                                                                                   Preamble = 0000000 followed by marker

//                                                    XFC data first time through     = 0100 0 1000 0 0011 0 0010 0

//                                                    XFC data second time through = 0000 0 1000 0 0011 0 0010 0
void loop() {

xfcstart:


  buttonState =digitalRead(xfcbutton);

  if (buttonState ==HIGH) goto xfcstart;

  digitalWrite(data, LOW);   // Zero starts                   Preamble starts 0000000 marker
  delayMicroseconds(190);               // 
  digitalWrite(data, HIGH);    // 
  delayMicroseconds(230);               // Zero ends

// code removed so I can post this!!

   digitalWrite(data, LOW); // marker starts
  delayMicroseconds(190);
  digitalWrite(data, HIGH);    // 
  delayMicroseconds(795); // marker ends                        End of preamble

  // first 20 bit data stream begins

delay(5);

  digitalWrite(data, LOW);   // Zero starts                 0100
  delayMicroseconds(190);                
  digitalWrite(data, HIGH);    
  delayMicroseconds(230);               // Zero ends
  //code removed so I can post this!!

  digitalWrite(data, LOW);   // Zero starts               0
  delayMicroseconds(190);                
  digitalWrite(data, HIGH);    
  delayMicroseconds(230);               // Zero ends

  // First 20 bit data stream ends


  // Second 20 bit data stream begins

subsequent:

delay(5);

  digitalWrite(data, LOW);   // Zero starts                 0000
  delayMicroseconds(190);                
  digitalWrite(data, HIGH);    
  delayMicroseconds(230);               // Zero ends
// code removed so that I can post this on the forum!!  

  digitalWrite(data, LOW);   // Zero starts               0
  delayMicroseconds(190);                
  digitalWrite(data, HIGH);    
  delayMicroseconds(230);               // Zero ends

  // second pass through finishes

  delay (10);

 

 if(buttonState ==LOW) goto subsequent;

}

(Get rid of the "goto"s - then we can start talking)

Instead of 'goto' use do/while http://arduino.cc/en/Reference/DoWhile

Should I be using a completley different approach?

yes you can. i suggest you to read Redirect Notice

This:

xfcstart:


  buttonState =digitalRead(xfcbutton);

  if (buttonState ==HIGH) goto xfcstart;

is just the same as while (digitalRead(xfcbutton) == HIGH) { }.

Instead of 'goto' use do/while

This is not the same as a while loop - a "do..while" will always execute the body of the loop at least once, but a "while" may not execute the body of the loop at all.

AWOL:
This is not the same as a while loop - a "do..while" will always execute the body of the loop at least once, but a "while" may not execute the body of the loop at all.

A do/while loop provides the behaviour requested by the OP - a while loop does not.

AWOL:

Instead of 'goto' use do/while

This is not the same as a while loop - a "do..while" will always execute the body of the loop at least once, but a "while" may not execute the body of the loop at all.

Do/While will execute the code at least once as is requested/needed (3 Second set of data is sent) and then repeat if the button is still pressed.
Probably my poor choice of syntax. Should maybe have said Do-While or Do..While but the link pointed to the correct place :smiley:

A do/while loop provides the behaviour requested by the OP - a while loop does not.

See reply #4.

@OP you need to look at blink without delay.

Mark

Hi

Thanks for all your replies...........I read in the Arduino reference that "goto" wasnt looked upon too kindly, but I didnt realize it was such a huge taboo!!

After a bit of playing around I managed to get the code working properly

const int data = 12;
const int xfcbutton = 2; 
int buttonState = 1;

// the setup routine runs once when you press reset:
void setup() {                
  // initialize the digital pin as an output.
  pinMode(data, OUTPUT); 

  pinMode(xfcbutton, INPUT);
  digitalWrite(data, HIGH); // sets data out to high initially


}
//                                                    Preamble = 0000000 followed by marker

//                                                    XFC data first time through  = 0100 0 1000 0 0011 0 0010 0

//                                                    XFC data second time through = 0000 0 1000 0 0011 0 0010 0
void loop() {
  
  while (digitalRead(xfcbutton) == HIGH){}
  
  digitalWrite(data, LOW);   // Zero starts                   Preamble starts 0000000 + marker
  delayMicroseconds(190);               // 
  digitalWrite(data, HIGH);    // 
  delayMicroseconds(230);               // Zero ends


// some code removed 

  digitalWrite(data, LOW); // marker starts
  delayMicroseconds(190);
  digitalWrite(data, HIGH);    // 
  delayMicroseconds(795); // marker ends                        End of preamble

  // first 20 bit data stream begins

//delay(5);

  digitalWrite(data, LOW);   // Zero starts                 0100
  delayMicroseconds(190);                
  digitalWrite(data, HIGH);    
  delayMicroseconds(230);               // Zero ends
 
//some code removed

  digitalWrite(data, LOW);   // Zero starts               0
  delayMicroseconds(190);                
  digitalWrite(data, HIGH);    
  delayMicroseconds(230);               // Zero ends

  // First 20 bit data stream ends


  // Second 20 bit data stream begins

do {

//delay(5);

  digitalWrite(data, LOW);   // Zero starts                 0000
  delayMicroseconds(190);                
  digitalWrite(data, HIGH);    
  delayMicroseconds(230);               // Zero ends
  
//some code removed so that I can post this!!

  digitalWrite(data, LOW);   // Zero starts               0
  delayMicroseconds(190);                
  digitalWrite(data, HIGH);    
  delayMicroseconds(230);               // Zero ends

  // second pass through finishes

 // delay (10);
 }
  
    while (digitalRead(xfcbutton) == LOW);

}

but I didnt realize it was such a huge taboo!!

They are not taboo. There ARE cases where a goto is needed. In 33 years of programming in C, I haven't seen one, but that doesn't mean that they don't exist.

They are NOT a substitute for structured programming, though, which is why you were encouraged to goto some place else, until you got rid of them.

It's interesting, isn't it, that getting rid of them also got rid of your problem. Something to consider when using a construct that is frowned on. There is almost always good reason for not liking certain allowed constructs.