Get out of a loop / goto:

Hi.

My problem.

I need to break a loop / goto on the press of a button.

At the moment i have the circuit working as it is should do, when i press button1 the program jumps to the next function stores it in memory and displays what it is supposed to do. On button2 i have it going to a function that displays all the functions every 15 secs, but as you can see from my code once i put it into a loop i cannot get out of it. I would like button2 to either switch the loop on or off depending on the current state of the loop.

Has anyone have any ideas on a better way approach this, at the moment the only i can break the loop is by doing a reset but i would like it to work only the buttons alone.

look at the function displayall() this is where my problem is.

Thanks.

#include <TVout.h>
#include <fontALL.h>
#include <EEPROM.h>



TVout TV;
const int  pushButton = 2;  
const int  pushButton1 = 3;
int buttonState;
int buttonState1;
int led = 13;

int epromvalue;
int epromaddress = 1;

int buttonPushCounter;


long randNumber;


int totalfunctions = 4;





void setup() {
  
  pinMode(pushButton, INPUT); 
  pinMode(pushButton1, INPUT); 
  pinMode(led, OUTPUT); 

  
  
    
  TV.begin(PAL,120,96); 
  TV.select_font(font6x8); 
  
  epromvalue = EEPROM.read(epromaddress);
  
   
  
  
        switch (epromvalue)
        {
          case 1:
          TV.clear_screen();
          block1();
          break;
          
          case 2:
          TV.clear_screen();
          block2();
          break;
          
          case 3:
          TV.clear_screen();
          block3();
          break;
          
          case 4:
          TV.clear_screen();
          block4();
          break;

         
        } 
 
  randomSeed(analogRead(0)); //?
}

void loop() {
  

  
  buttonState = digitalRead(pushButton);
  buttonState1 = digitalRead(pushButton1);
  
  if (buttonState1 == HIGH)
  {
    
    displayall();    
    
    TV.clear_screen(); 
  }
 
  
  
  if (buttonState == HIGH)
  {
    
    digitalWrite(led, HIGH);
     
    buttonPushCounter++;
    if (buttonPushCounter > totalfunctions)
    {
      buttonPushCounter = 1;
    }
   
   
   
   
    
   
    TV.clear_screen(); //Only do a clear screen if the button is pressed.
  
      switch (buttonPushCounter)
      {
        case 1:
        block1();
        break;
        
        case 2:
        block2();
        break;
        
        case 3:
        block3();
        break;

        case 4:
        block4();
        break;

        
      }


}
  else
  {
      digitalWrite(led, LOW);  
  }



 
      

  
delay(1000);  
}


void displayall()
{
      here:
      
      digitalWrite(led, HIGH);
      
      loadbars();
      block1();
      TV.delay(14000);
      loadbars();
      block2();
      TV.delay(14000);
      loadbars();
      block3();
      TV.delay(14000);
      loadbars();
      block4();
      TV.delay(14000); 
      
      digitalWrite(led, LOW);
      goto here;
  
      
}


void block1()
{
    TV.draw_rect(0,0,TV.hres()-1,TV.vres()-10,WHITE,WHITE);
    TV.print(0,88,"2E0AVI - 144.750");
    EEPROM.write(epromaddress, 1);
  
  
}

void loadbars()
{
  
  //draw_rect(x0,y0,width,height,color,fillcolor)
    //TV.draw_rect(0,0,1,10,WHITE,WHITE);
    TV.clear_screen();
    TV.select_font(font6x8);
    TV.draw_rect(18,28,80,34,WHITE);
    TV.print(38,75,"LOADING");
    for (int i=20; i<95;i++)
    {
      //TV.println(i);
     TV.draw_rect(i,30,1,30,WHITE,WHITE);
     randNumber = random(0, 110);

    delay(randNumber); 
    }
    TV.clear_screen();
    //TV.draw_column(5,5,60,WHITE);  
   // TV.draw_row(1,0,60,WHITE);
}

void block2()
{
    
    TV.clear_screen();
    TV.select_font(font6x8);
    TV.draw_rect(18,28,80,34,WHITE);
    TV.print(37,40,"2E0AVI");  
    EEPROM.write(epromaddress, 2);
}

void block3()
{
   
    TV.select_font(font6x8);
    TV.draw_rect(0,0,TV.hres()-1,TV.vres()-3,WHITE);
    TV.draw_rect(0,0,TV.hres()-1,12,WHITE);
    TV.print(39,3,"GENERATOR");
    
    TV.draw_rect(0,12,TV.hres()-1,10,WHITE);
    TV.print(2,14,"CALLSIGN : 2E0AVI");
    
    TV.draw_rect(0,22,TV.hres()-1,10,WHITE);
    TV.print(2,24,"TALKBACK : 144.750");
    
     
    TV.draw_rect(0,42,TV.hres()-1,10,WHITE);
    TV.print(2,34,"NAME : ANTHONY");    
     //TV.draw_rect(74,12,40,82,WHITE);
    
    TV.print(2,44,"LOCATION : IO93LE"); 
    
    TV.draw_rect(0,62,TV.hres()-1,10,WHITE);
    TV.print(2,54,"V/OUTPUT : 435.000");    
    
    TV.print(2,64,"POWER : 2W");    
    
    TV.draw_rect(0,82,TV.hres()-1,10,WHITE);
    TV.print(2,74,"MODE : AM");
    
    int h = TV.hres();
    int v = TV.vres();
    
    TV.print(2,84,"H/V RES :");
    TV.print(45,84,h);
    TV.print(65,84,"X");
    TV.print(72,84,v);
    
    EEPROM.write(epromaddress, 3);  
   
}

void block4()
{
    
    TV.clear_screen();
    TV.select_font(font6x8);
     TV.print(43,35,"2E0AVI");
     TV.draw_row(45,0,118,WHITE);
     TV.print(36,50,"70CM ATV");
   
  
    EEPROM.write(epromaddress, 4);
    
}

Use an actual loop rather than 'goto'

http://www.cplusplus.com/doc/tutorial/control/

Then you can use the break keyword.

Break can be used to exit a for loop. Would this help?

for (;;) {
    (do stuff)
    if (digitalRead(blah) == 1) break;
}

Using an actual loop instead of goto is the first step.

The second step is to NOT use delay(). While you 'delay()', you can do nothing else. So, since you want to do something else (in this case, monitor for a button push), you have to eliminate all your delays.

Look at the blink without delay sample for the recommended method of doing this.

On a side note, I think it's a little absurd that the TVout library implements it's own method of delay(). Utterly pointless.

jraskell:
Using an actual loop instead of goto is the first step.

The second step is to NOT use delay(). While you 'delay()', you can do nothing else. So, since you want to do something else (in this case, monitor for a button push), you have to eliminate all your delays.

Look at the blink without delay sample for the recommended method of doing this.

On a side note, I think it's a little absurd that the TVout library implements it's own method of delay(). Utterly pointless.

It has to delay for 15 seconds it will just scroll through each display at lighting speed and no one would see anything.

billroy:
Break can be used to exit a for loop. Would this help?

for (;;) {

(do stuff)
    if (digitalRead(blah) == 1) break;
}

I will look at this thanks, i will add the button status check before the delay is called.

Thanks.

It has to delay for 15 seconds it will just scroll through each display at lighting speed and no one would see anything.

I understand that, and what I'm telling you is that if you delay(15000), you will not be able to do anything else at all during those 15 seconds. There are other methods of causing portions of your sketch to wait 15 seconds while the rest of the sketch continues to execute. Blink without delay is one of those methods. Look at it. Learn it. Use it.