Pages: 1 [2]   Go Down
Author Topic: Switch-case loop problem?!  (Read 1453 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 22
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

@Arrch

Code:
#include <Servo.h>



int var = 0;



int Lstate = 2;      // pin 2 - citire
int var_Lstate = 0; 

int pos_tm1 = 120;     // 127 best
int pos_tm2 = 30;    // variable to store the servo position
Servo myservo_tm;

int flag_tm1 = 0;
int flag_tm2 = 0;
long prag_steptm1 = 0;
long prag_steptm2 = 0;
long prag_tm1 = 0;
long prag_tm2 = 0;


void setup()
{Serial.begin(9600);
pinMode (Lstate, INPUT);  // sets pin 2 as digital input

 myservo_tm.attach(5);
  myservo_tm.write(120);
}

void loop()
{
 
  var_Lstate = digitalRead(Lstate);    // reads the status of the sensor
  //Serial.println(flag_tm2);
  //Serial.println(flag_tm1);
  //Serial.println(var_Lstate);
Serial.println(var);
   if(flag_tm1 == 1)
  {
    if(millis() - prag_steptm1 > 55)
    {
      prag_steptm1 = millis();
      if(pos_tm1 > 29)
      { 
        pos_tm1--;
        myservo_tm.write(pos_tm1);
      }
      else
      flag_tm1 = 0;
    }
   
  }
 
   if(flag_tm2 == 1)
  {
    if(millis() - prag_steptm2 > 55)
    {
      prag_steptm2 = millis();
      if(pos_tm2 < 121)
      { 
        pos_tm2++;
        myservo_tm.write(pos_tm2);
      }
      else
      flag_tm2 = 0;
    }
  }
 
 
  switch(var)
  {
  case 0:
      pos_tm1 = 120;
      pos_tm2 = 30;
      if(var_Lstate == 0)
    {   
     
      prag_tm1 = millis();
      prag_tm2 = millis();
      var = 1;
    }
  break;
 
 
  case 1:
      if( millis() - prag_tm1 > 3000)
    {
      flag_tm1 = 1;
    }
    if( millis() - prag_tm2 > 10000)
    {
      flag_tm2 = 1;
      var = 0;
    }
  break;
 
 
  }
}
 
 

Still doesn't work properly...
Logged

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

Still doesn't work properly...

... but you aren't going to tell us what it's doing wrong?
Logged

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

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 610
Posts: 49059
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
  var_Lstate = digitalRead(Lstate);    // reads the status of the sensor
  //Serial.println(flag_tm2);
  //Serial.println(flag_tm1);
  //Serial.println(var_Lstate);
Serial.println(var);
Read something. Print something else. I miss how the print() is useful.
Logged

California
Offline Offline
Faraday Member
**
Karma: 88
Posts: 3375
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
[code]    if( millis() - prag_tm2 > 10000)
    {
      flag_tm2 = 1;
      var = 0;
    }

So after 10 seconds, go back to state 0 and
  
Code:
 case 0:
      pos_tm1 = 120;
      pos_tm2 = 30;

Reset your positions, regardless of whether or not the servos have run their course? Once you hit state 0, your increments and decrements become ineffective. You should only be resetting your values until after you are sure that they are done moving. Perhaps it would be better to do that the same time you clear their flags, or better yet, add a 3rd state that will wait until both flags are cleared before resetting the position values and returning to state 0. Paying closer attention to what happens during the states and during the transitions will also help down the road. That was why I posted the previous helper functions for changing states.

Code:
switch (state)
{
  case FIRST:
    // this happens every loop, when it is in this state
    if (some condition exists)
    {
      // set the state variable to SECOND
      // this will only occur once, during the transition from FIRST to SECOND
    }
    break;
  case SECOND:
  ...
}

You also should take some steps to remove redundant code. Obviously, you are going to be expanding on this, so having additional lines of code and variables is going to make it more difficult to debug. Hint: You don't need two prag_tmX variables and your check for flag and check for interval can be done on a single line.[/code]
« Last Edit: March 23, 2013, 09:51:16 am by Arrch » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 22
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It's working now, thank you very much Arrch

Code:
#include <Servo.h>



int var = 0;



int Lstate = 2;      // pin 2 - citire
int var_Lstate = 0; 

int pos_tm1 = 120;     // 127 best
int pos_tm2 = 30;    // variable to store the servo position
Servo myservo_tm;

int flag_tm1 = 0;
int flag_tm2 = 0;
long prag_steptm1 = 0;
long prag_steptm2 = 0;
long prag_tm1 = 0;
long prag_tm2 = 0;

long prag_pos = 0;

void setup()
{Serial.begin(9600);
pinMode (Lstate, INPUT);  // sets pin 2 as digital input

 myservo_tm.attach(5);
  myservo_tm.write(120);
}

void loop()
{
 
  var_Lstate = digitalRead(Lstate);    // reads the status of the sensor
//  Serial.println(flag_tm2);
//  Serial.println(flag_tm1);
//  Serial.println(var_Lstate);
Serial.println(flag_tm1);
//Serial.println(pos_tm2);

   if(flag_tm1 == 1)
  {
    if(millis() - prag_steptm1 > 55)
    {
      prag_steptm1 = millis();
      if(pos_tm1 > 29)
      { 
        pos_tm1--;
        myservo_tm.write(pos_tm1);
      }
      else     
      flag_tm1 = 0; 
      }
    }
 
 
   if(flag_tm2 == 1)
  {
    if(millis() - prag_steptm2 > 55)
    {
      prag_steptm2 = millis();
      if(pos_tm2 < 121)
      { 
        pos_tm2++;
        myservo_tm.write(pos_tm2);
      }
      else
      flag_tm2 = 0;
    }
  }
 
 
 
  switch(var)
  {
  case 0:
     
      if(var_Lstate == 0)
    {   
     // pos_tm1 = 120;
     // pos_tm2 = 30;
      prag_tm1 = millis();
      prag_tm2 = millis();
      var = 1;
    }
  break;
 
 
  case 1:
      if( millis() - prag_tm1 > 3000)
    {
      flag_tm1 = 1;
    }
    if( millis() - prag_tm2 > 10000)
    {
      flag_tm2 = 1;   
      prag_pos = millis();
      var = 2;
    }
 
  break;
  case 2:
     if(millis() - prag_pos > 10)
     {
       if(flag_tm1 == 0 && flag_tm2 == 0)
       {
         pos_tm1 = 120;
         pos_tm2 = 30;
         var = 0;
       }
     }
  break;
  }
  }
 
 
 

I know I used different variables for all things i wanted to do, and i know i could use the same variable for more than one thing but, the comitee of examiners don't know so well all this programming stuff and i wanted to be easy for them to track all the commands using representative variable names.
I know i could use one case for every action i want but, in this way it seems more easier to track the program (for ex: case 2 does x,y and z actions).

My entire code for the bridge is almost ready.If some1 is interested i'll post it too.
Thank you all again for your help.

Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 610
Posts: 49059
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
the comitee of examiners don't know so well all this programming stuff
I hope that they don't surprise you. They may know far more than you think they do.
Logged

New Jersey
Offline Offline
Faraday Member
**
Karma: 67
Posts: 3677
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If you want to make your code easier for your examiners to read, consider sorting out the indentation and whitespace in your sketch.
Logged

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 98
Posts: 4801
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The IDE has Auto-Format in the Tools tab....
Logged

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

Offline Offline
Faraday Member
**
Karma: 62
Posts: 3007
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Tollgate or tollbar.   Tollbooth makes no sense.
Logged

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 98
Posts: 4801
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Tollbooth is what the operator stands inside. I've seen them since 1961, back in the age when they took money and gave change. By 64-65 they had Exact Change Only lanes but even to the mid-80's I knew a girl who worked in a tollbooth.

Maybe what makes sense depends on where you're from and where you've been.
Logged

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

Pages: 1 [2]   Go Up
Jump to: