Pages: [1]   Go Down
Author Topic: start code from the begining .How?  (Read 690 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Sr. Member
****
Karma: 0
Posts: 250
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I have a program that makes some tasks and it checks for infrared signals.

all i want is every time it checks for IR signals i want the scetch to start from begining. I tried with goto but it only works inside their void

Code:
byte incoming_IR_data;
void setup() {                

  pinMode(13, OUTPUT);    
}

void loop() {
  start:
  task_0();
  task_1();
  task_2(); 
}

void task_0(){
  
  //some code here
   check_IR();
}
void task_1(){
  
  //some code here
   check_IR();
}
void task_2(){
  
  //some code here
   check_IR();
}
void check_IR(){
 //Some code that checks IR and stores data from Infrared to incoming_IR_data variable
 
     if (incoming_IR_data==12524){
       goto start;  
     }
  
  
}
« Last Edit: May 06, 2013, 03:06:11 pm by acmiilangr » Logged

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 118
Posts: 4407
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You do realise that the very purpose of the loop() function i to run the same code again and again don't you ?
Code:
void loop()
{
  task_0();
  check_IR();
}
will call the task_0() function then the check_IR() function then task_0() then check_IR() and so on with no special instructions.  Your 'code' seems to indicate that the loop should only start again if a specific code in read by the IR function.

Can you explain more clearly what you want to do ?
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

0
Offline Offline
Sr. Member
****
Karma: 0
Posts: 250
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

you are right. my original scetch is really big so i decided to give you an example on what i want to do.

so check again my code.
it runs many task and in all these tasks it checks for IR.
all i need is to start from begining when a IR signal is coming
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 310
Posts: 26632
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I tried with goto but it only works inside their void
A void is nothing - a null, an emptiness, a big hole.
Did you mean "function"?
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

0
Offline Offline
Sr. Member
****
Karma: 0
Posts: 250
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

yes. function
Logged

Mid-Atlantic, USA
Offline Offline
God Member
*****
Karma: 30
Posts: 515
"Remember kids, the only difference between Science and screwing around is writing it down." - Adam Savage
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I have a program that makes some tasks and it checks for infrared signals.

all i want is every time it checks for IR signals i want the scetch to start from begining. I tried with goto but it only works inside their void

Since you want the 3 tasks to do something different, (and presumably if you notice IR data after task_1 you want to skip task_2), you might want something like this: (using flags)

Code:
byte incoming_IR_data;
boolean IR_received;  // Flag for knowing if you have to do something different when IR information has been received. Should initialize to 0 (which ==FALSE)

void setup() {                

  pinMode(13, OUTPUT);    
}

void loop() {
  if (IR_received)  //
  {
    // code to handle IR stuff if needed. Otherwise unwrap the following line out of the if statement.
    IR_received = FALSE;  // (Re)set the received flag
  }
  task_0();
  task_1();
  task_2(); 
}

void task_0()
{
  // don't need to check IR_received here because we haven't checked IR communication since we last reset the flag
  //some code here
   check_IR();
}
void task_1()
{

  if (!IR_received)  // if IR_received == FALSE
  {
    return; // return from the function w/o doing anything
  }
  
  //some code here
   check_IR();
}
void task_2()
{

  if (!IR_received)  // if IR_received == FALSE
  {
    return; // return from the function w/o doing anything
  }
  
  //some code here
   check_IR();
}
void check_IR()
{
 //Some code that checks IR and stores data from Infrared to incoming_IR_data variable
 
     if (incoming_IR_data==12524)
     {
       IR_received == TRUE;  // Set the flag that basically neuters the other functions.
     }
}

Now, I'm not sure if this is what you really want, but I think it should do what you asked. smiley-wink
Logged


Offline Offline
Edison Member
*
Karma: 37
Posts: 1355
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It's still not that clear what you want to do...

loop() will automatically loop/repeat, unless you are stuck in another loop.

You can break out of a for-loop, while-loop, or do-while loop, with break, but you shouldn't generally break-out of your main loop because your program will end and nothing will happen 'till you hardware-reset.

Or as Sembazuru suggested, you may need an [yurl=http://arduino.cc/en/Reference/If]if[/url]-statement.

Quote
I tried with goto but it only works inside their void.

 - loop() is a function.

 - void at beginning of a function prototype or function definition means the the function returns nothing when the function ends/returns.   Otherwise, you'll see the varaible-type that's returned by the function (such as int).

 - The empty parenthesis indicate that no values are passed into the function. 

And FYI - It's generally considered "bad practice" to use goto in C/C++.
Logged

0
Offline Offline
Sr. Member
****
Karma: 0
Posts: 250
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

well this is what i have made using arduino code:


the code is really complicated and big. user can program it via IR remote control.

each loop takes about 1-3 minutes. I want the add a new feauture now. "night mode"
when user presses "off" button on remote control, the cross display need to show only temperature. (this is night mode).
i succed to do that with a flag, but the functions are spending a lot of time and it needs to finish one of them and then go to night mode.
for example when it scrolling the time, when user presses "off" button it waits to finish this fuction (maybe about 5-10 seconds) and then it goes to night mode.. and this is not what i want from that. i want to turning on night mode in real time


edit.

i found this
void(* resetFunc) (void) = 0; //declare reset function @ address 0
...
resetFunc();  //call reset

is it safe to use it?
« Last Edit: May 06, 2013, 04:22:25 pm by acmiilangr » Logged

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 118
Posts: 4407
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I don't understand what going in to 'night mode' has to do with starting your code from the beginning.  Can you please explain ?
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

0
Offline Offline
Sr. Member
****
Karma: 0
Posts: 250
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

there is a function that checks if there is "OFF" signal from IR remote control (lets name this function IR_CHECK). if user presses off button then it stores "1" to a temp variables. if user press  "ON" button it stores "0" to that temp variable

on my project there are many other functions (displaying graphic efe,showing scrolling text and other)
all these functions are calling IR_CHECK function to check if there is "OFF" or "ON" signal. and they works like this:

if (temp==0){
  task_0();
  task_1();
  task_2();
 //...
 //...
}
if (temp==1){
  task_temperature();//show only temperature
}

so when user presses OFF button the only fanction that runs is task_temperature. the only problem is that it needs to wait to finish the current task (some of them are spending some seconds)
Logged

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12630
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

A better way to achieve what you're asking for is to redesign your sketch to be non-blocking so that loop() runs to completion instead of waiting for your display to complete, and put your test for IR input at the start of loop().
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Des Moines, WA - USA
Offline Offline
God Member
*****
Karma: 25
Posts: 779
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I think you're trying to complicate something that is really simple!

Code:
const uint8_t   pinLED  = 13;

byte incoming_IR_data;

bool check_IR()
{
    // Some code that checks IR and stores data from Infrared to
    // incoming_IR_data variable

    if ( incoming_IR_data == 12524 )
    {
        return false;
    }
}

bool task_2()
{
    bool    result;
   
    result = check_IR();
    if ( ! result )
    {
        return result;
    }

    ...

    return true;
}

bool task_1()
{
    bool    result;
   
    result = check_IR();
    if ( ! result )
    {
        return result;
    }

    ...

    return true;
}

bool task_0()
{
    bool    result;
   
    result = check_IR();
    if ( ! result )
    {
        return result;
    }

    ...

    return true;
}

void loop()
{
start:
    if ( false == task_0() ) return;
    if ( false == task_1() ) return;
    if ( false == task_2() ) return;
}

void setup()
{
    pinMode(pinLED, OUTPUT);     
}
Logged

Offline Offline
Edison Member
*
Karma: 33
Posts: 1481
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If you really want to do this, check out set_jmp() and jmp().

But it might suck up a lot of stack.
Logged

Pages: [1]   Go Up
Jump to: