Push button (first push = do nothing,second push=start program)

Hello.I would be grateful if somebody can help

I need help about push button code.
I have this code and i wish to start with 1 push button.

First time when i push button program is off and when i push second time button program start.

Hire is code for my project and is working.

/////////////////////////////
int Nema42matka   = 1000;   
int Nema42matka2  = 1000;  // 2 x Nema42matka 
int Nema42nopeus  = 2000;  
//
int Nema34matka   = 4000;  
int Nema34nopeus  = 1000;  
/////////////////////////////


////////////////////////
int Nema42Pin     = 3;//
int Nema42Dir     = 4;//
//
int Imupumppu     = 5;//
//
int Nema34Pin     = 6;//
int Nema34Dir     = 7;//
////////////////////////
int buttonPin = 11;

int task1Status=0;
int buttonVal = 0;
int prevButtonVal = 0;
int stepNumber = 0;




void setup()

{

  pinMode(Nema42Pin,OUTPUT);
  pinMode(Nema42Dir,OUTPUT);
  pinMode(Nema34Pin,OUTPUT);
  pinMode(Nema34Dir,OUTPUT);
  pinMode(Imupumppu,OUTPUT);
  pinMode(buttonPin, INPUT_PULLUP);

}

void loop(){

  prevButtonVal = buttonVal;
  buttonVal = digitalRead(buttonPin);

}
  if (buttonVal == LOW && prevButtonVal == HIGH) {
    stepNumber ++;
    if (stepNumber > 1) {
      stepNumber = 0;

    }

if (stepNumber == 1) {
      digitalWrite(Imupumppu,HIGH);
}


void doOneThingOfTask1()

{

    if (task1Status==10)
    task1Status=0;

  switch (task1Status)
  {
  case 0:

    {
     if (stepNumber == 1) {
      digitalWrite(Imupumppu,HIGH); 

    }
  case 1:
    {
      delay(2000);
      digitalWrite(Nema42Pin,LOW);
      for(int n = 0; n <Nema42matka; n++) { 
        digitalWrite(Nema42Dir,LOW);
        delayMicroseconds(Nema42nopeus);
        digitalWrite(Nema42Dir,HIGH);

      }
    case 2:
      {

        digitalWrite(Nema34Pin,LOW);
        for(int n = 0; n <Nema34matka; n++) { 
          digitalWrite(Nema34Dir,LOW);
          delayMicroseconds(Nema34nopeus);
          digitalWrite(Nema34Dir,HIGH);

        }
      case 3:
        {

          digitalWrite(Imupumppu,LOW);



        }
      case 4:
        {
          delay(2000);
          digitalWrite(Nema34Pin,HIGH);
          for(int n = 0; n <Nema34matka; n++) { 
            digitalWrite(Nema34Dir,LOW);
            delayMicroseconds(Nema34nopeus);
            digitalWrite(Nema34Dir,HIGH);

          }
        case 9:
          {
            digitalWrite(Nema42Pin,LOW);
            for(int n = 0; n <Nema42matka2; n++) { 
              digitalWrite(Nema42Dir,HIGH);
              delayMicroseconds(Nema42nopeus);
              digitalWrite(Nema42Dir,LOW);

            }
          }
        }
      }}}}}}}}}}

You should better debounce the switch. For testing purposes print the stepNumber state to the Serial Monitor, on every change.

Hire is code for my project and is working.

That code can not possibly be said to be working, when it won't even compile.

i'm so sorry :-[

I posted wrong code.I will post tomorrow real code.

Hire is code and is working when i upload without push button.

I have connected 2 stepper motors and dc motor.

/////////////////////////////
int Nema42matka   = 300;   
int Nema42matka2  = 600;  // 2 x Nema42matka 
int Nema42nopeus  = 5000;  
//
int Nema34matka   = 10000;  
int Nema34nopeus  = 300;  
/////////////////////////////


////////////////////////
int Nema42Pin     = 3;//
int Nema42Dir     = 4;//
//
int Imupumppu     = 5;//
//
int Nema34Pin     = 6;//
int Nema34Dir     = 7;//
////////////////////////


int task1Status=0;




void setup()

{

  pinMode(Nema42Pin,OUTPUT);
  pinMode(Nema42Dir,OUTPUT);
  pinMode(Nema34Pin,OUTPUT);
  pinMode(Nema34Dir,OUTPUT);
  pinMode(Imupumppu,OUTPUT);


}

void loop()

{
  doOneThingOfTask1();

}
void doOneThingOfTask1()
{
  if (task1Status==10)
    task1Status=0;

  switch (task1Status)
  {
  case 0:

    {
      digitalWrite(Imupumppu,HIGH);


    }
  case 1:
    {
      delay(3000);
      digitalWrite(Nema42Pin,LOW);
      for(int n = 0; n <Nema42matka; n++) { 
        digitalWrite(Nema42Dir,LOW);
        delayMicroseconds(Nema42nopeus);
        digitalWrite(Nema42Dir,HIGH);

      }
    case 2:
      {

        digitalWrite(Nema34Pin,LOW);
        for(int n = 0; n <Nema34matka; n++) { 
          digitalWrite(Nema34Dir,LOW);
          delayMicroseconds(Nema34nopeus);
          digitalWrite(Nema34Dir,HIGH);

        }
      case 3:
        {
          delay(2000);
          digitalWrite(Imupumppu,LOW);



        }
      case 4:
        {
          delay(2000);
          digitalWrite(Nema34Pin,HIGH);
          for(int n = 0; n <Nema34matka; n++) { 
            digitalWrite(Nema34Dir,LOW);
            delayMicroseconds(Nema34nopeus);
            digitalWrite(Nema34Dir,HIGH);

          }
        case 9:
          {
            digitalWrite(Nema42Pin,HIGH);
            for(int n = 0; n <Nema42matka2; n++) { 
              digitalWrite(Nema42Dir,HIGH);
              delayMicroseconds(Nema42nopeus);
              digitalWrite(Nema42Dir,LOW);

            }
          }
        }
      }
    }
  }
}

Hello anybody here ??? :cry:

Can i get help for christmas ? :slight_smile:

Make a button toggle a variable, if the variable is true, run the code in your loop, else do nothing.

Need help pleaseeee

/////////////////////////////
int Nema42matka   = 100;   
int Nema42matka2  = 200;  // 2 x Nema42matka 
int Nema42nopeus  = 3000;  
//
int Nema34matka   = 10000;  
int Nema34nopeus  = 500;  
////////////////////////////


/////////////////////////
int Nema42Pin     = 3; //
int Nema42Dir     = 4; //
                       //
int Imupumppu     = 5; //
                       // 
int Nema34Pin     = 6; //
int Nema34Dir     = 7; //
int buttonPin     = 10;//
/////////////////////////

int task1Status=0;
int buttonVal     = 0;
int prevButtonVal = 0;
int stepNumber    = 0;






void setup()

{

  pinMode(Nema42Pin,OUTPUT);
  pinMode(Nema42Dir,OUTPUT);
  pinMode(Nema34Pin,OUTPUT);
  pinMode(Nema34Dir,OUTPUT);
  pinMode(Imupumppu,OUTPUT);
  pinMode(buttonPin, INPUT_PULLUP);

}

void loop()
{
prevButtonVal = buttonVal;
buttonVal = digitalRead(buttonPin);

if (buttonVal == LOW && prevButtonVal == HIGH) {
    stepNumber ++;
    if (stepNumber > 2) {
      stepNumber = 0;

 }

 if (stepNumber == 1) {///  need code when putton is pressed first time (do nothing)
 }

    } else if (stepNumber == 2) {  /// when button is pressed second time (start program)
 
                                         

  doOneThingOfTask1();

}
void doOneThingOfTask1()
{
  if (task1Status==10)
    task1Status=0;

  switch (task1Status)          
  {
  case 0:

    {
      digitalWrite(Imupumppu,HIGH);


    }
  case 1:
    {
      delay(3000);
      digitalWrite(Nema42Pin,LOW);
      for(int n = 0; n <Nema42matka; n++) { 
        digitalWrite(Nema42Dir,LOW);
        delayMicroseconds(Nema42nopeus);
        digitalWrite(Nema42Dir,HIGH);

      }
    case 2:
      {

        digitalWrite(Nema34Pin,LOW);
        for(int n = 0; n <Nema34matka; n++) { 
          digitalWrite(Nema34Dir,LOW);
          delayMicroseconds(Nema34nopeus);
          digitalWrite(Nema34Dir,HIGH);

        }
      case 3:
        {
          delay(2000);
          digitalWrite(Imupumppu,LOW);



        }
      case 4:
        {
          delay(2000);
          digitalWrite(Nema34Pin,HIGH);
          for(int n = 0; n <Nema34matka; n++) { 
            digitalWrite(Nema34Dir,LOW);
            delayMicroseconds(Nema34nopeus);
            digitalWrite(Nema34Dir,HIGH);

          }
        case 9:
          {
            digitalWrite(Nema42Pin,HIGH);
            for(int n = 0; n <Nema42matka2; n++) { 
              digitalWrite(Nema42Dir,HIGH);
              delayMicroseconds(Nema42nopeus);
              digitalWrite(Nema42Dir,LOW);

            }
          }
        }
      }
    }
  }
}

This may help:

Ahmo:

 if (stepNumber == 1) {///  need code when putton is pressed first time (do nothing)

}

} else if (stepNumber == 2) {  /// when button is pressed second time (start program)
//move } down

doOneThingOfTask1();
// } here!
}

Thank you for replay

You men like this

getting error: zapoceto_2.ino:67:2: error: expected unqualified-id before ‘else’

/////////////////////////////
int Nema42matka   = 100;   
int Nema42matka2  = 200;  // 2 x Nema42matka 
int Nema42nopeus  = 3000;  
//
int Nema34matka   = 10000;  
int Nema34nopeus  = 500;  
////////////////////////////


/////////////////////////
int Nema42Pin     = 3; //
int Nema42Dir     = 4; //
                       //
int Imupumppu     = 5; //
                       // 
int Nema34Pin     = 6; //
int Nema34Dir     = 7; //
int buttonPin     = 10;//
/////////////////////////

int task1Status=0;
int buttonVal     = 0;
int prevButtonVal = 0;
int stepNumber    = 0;






void setup()

{

  pinMode(Nema42Pin,OUTPUT);
  pinMode(Nema42Dir,OUTPUT);
  pinMode(Nema34Pin,OUTPUT);
  pinMode(Nema34Dir,OUTPUT);
  pinMode(Imupumppu,OUTPUT);
  pinMode(buttonPin, INPUT_PULLUP);

}

void loop()
{
prevButtonVal = buttonVal;
buttonVal = digitalRead(buttonPin);

if (buttonVal == LOW && prevButtonVal == HIGH) {
    stepNumber ++;
    if (stepNumber > 2) {
      stepNumber = 0;


  doOneThingOfTask1();
    }
}
 if (stepNumber == 1) {///  need code when putton is pressed first time (do nothing)
 
 }
} 
 else if (stepNumber == 2) {  /// when button is pressed second time (start program)

void doOneThingOfTask1()
{
  if (task1Status==10)
    task1Status=0;

  switch (task1Status)          
  {
  case 0:

    {
      digitalWrite(Imupumppu,HIGH);


    }
  case 1:
    {
      delay(3000);
      digitalWrite(Nema42Pin,LOW);
      for(int n = 0; n <Nema42matka; n++) { 
        digitalWrite(Nema42Dir,LOW);
        delayMicroseconds(Nema42nopeus);
        digitalWrite(Nema42Dir,HIGH);

      }
    case 2:
      {

        digitalWrite(Nema34Pin,LOW);
        for(int n = 0; n <Nema34matka; n++) { 
          digitalWrite(Nema34Dir,LOW);
          delayMicroseconds(Nema34nopeus);
          digitalWrite(Nema34Dir,HIGH);

        }
      case 3:
        {
          delay(2000);
          digitalWrite(Imupumppu,LOW);



        }
      case 4:
        {
          delay(2000);
          digitalWrite(Nema34Pin,HIGH);
          for(int n = 0; n <Nema34matka; n++) { 
            digitalWrite(Nema34Dir,LOW);
            delayMicroseconds(Nema34nopeus);
            digitalWrite(Nema34Dir,HIGH);

          }
        case 9:
          {
            digitalWrite(Nema42Pin,HIGH);
            for(int n = 0; n <Nema42matka2; n++) { 
              digitalWrite(Nema42Dir,HIGH);
              delayMicroseconds(Nema42nopeus);
              digitalWrite(Nema42Dir,LOW);

            }
          }
        }
      }
    }
  }
}

Check and balance all your parentheses {}
Also, clean up the code layout, as it's too hard to see what's indented where!

Merry Christmas!

Merry Christmas!

My head feels like it will explod.

Do we have hire SANTAKLAS.

if you are hiding someway pleasee heeeeeeelp :o :o

So clean up your code, identify the unbalanced parentheses.
Then you'll have a happy Christmas!

If you've already done that - re-post the fixed code that still won't compile.
We're certainly not going to do your housekeeping!

Move only the { and keep the rest of the line as it was.

I cleaned up the code layout.

DrDiettric what you mean by " move only the { "

do i need move out off loop or what?

/////////////////////////////
int Nema42matka   = 100;   
int Nema42matka2  = 200;  // 2 x Nema42matka 
int Nema42nopeus  = 3000;  
//
int Nema34matka   = 10000;  
int Nema34nopeus  = 500;  
////////////////////////////


/////////////////////////
int Nema42Pin     = 3; //
int Nema42Dir     = 4; //
                       //
int Imupumppu     = 5; //
                       // 
int Nema34Pin     = 6; //
int Nema34Dir     = 7; //
int buttonPin     = 10;//
/////////////////////////

int task1Status=0;
int buttonVal     = 0;
int prevButtonVal = 0;
int stepNumber    = 0;


void setup()

{

pinMode(Nema42Pin,OUTPUT);
pinMode(Nema42Dir,OUTPUT);
pinMode(Nema34Pin,OUTPUT);
pinMode(Nema34Dir,OUTPUT);
pinMode(Imupumppu,OUTPUT);
pinMode(buttonPin, INPUT_PULLUP);

}

void loop()
{
  prevButtonVal = buttonVal;
  buttonVal = digitalRead(buttonPin);
  if (buttonVal == LOW && prevButtonVal == HIGH) 

{

stepNumber ++;
if (stepNumber > 2) {
stepNumber = 0;
doOneThingOfTask1();

}

}

if (stepNumber == 1) {   /// (do nothing)

} 

}
  
  else if (stepNumber == 2) /// when button is pressed second time (start program)
  
{  
 
void doOneThingOfTask1()

{

 else if (task1Status==10)

{
 
  task1Status=0;
 switch (task1Status)          

{

case 0:

{

  digitalWrite(Imupumppu,HIGH);


}
case 1:
{

  delay(3000);
digitalWrite(Nema42Pin,LOW);
for(int n = 0; n <Nema42matka; n++) { 
digitalWrite(Nema42Dir,LOW);
delayMicroseconds(Nema42nopeus);
digitalWrite(Nema42Dir,HIGH);

}
case 2:
{

digitalWrite(Nema34Pin,LOW);
for(int n = 0; n <Nema34matka; n++) { 
digitalWrite(Nema34Dir,LOW);
delayMicroseconds(Nema34nopeus);
digitalWrite(Nema34Dir,HIGH);

}
case 3:
{

delay(2000);
digitalWrite(Imupumppu,LOW);

}
case 4:
{
delay(2000);
digitalWrite(Nema34Pin,HIGH);
for(int n = 0; n <Nema34matka; n++) { 
digitalWrite(Nema34Dir,LOW);
delayMicroseconds(Nema34nopeus);
digitalWrite(Nema34Dir,HIGH);

}
case 9:
{
digitalWrite(Nema42Pin,HIGH);
for(int n = 0; n <Nema42matka2; n++) { 
digitalWrite(Nema42Dir,HIGH);
delayMicroseconds(Nema42nopeus);
digitalWrite(Nema42Dir,LOW);

}}}}}}}

Sorry - I'm still not going to reformat your code in order to look for structural errors.
Please check the stickies at the top of the forum on how to format & post code.

If you're using the Arduino IDE, try CTRL+T in the Tools menu before you cut & paste to the forum (between code tags).

As DrDiettrich and I have been suggesting - you need to look at the balanced number of { } braces.

If you move the cursor to a { or }, the IDE will highlight the matching brace. This way you can find out that the marked brace in my preceding post terminates the outer if-statement, inside which the counter value is updated, so that the following code executes when the value was not updated. When you move the brace down as indicated, the else-part applies to the case when stepNumber is not 1.

I know how to auto format , but getting error because: too many left curly braces.
And i am not so smart as you people and my lenguage is not so good.

i'm trying so hard 3 weeks my self make this push button code but no luck.
So i deside to ask for help.

If i remove left curly then i get so many errors.

Go back to your code in #7.

Then change the if-statements according to this pattern:

if (stepNumber ==1)
{
  //handle case 1
}
else if (stepNumber == 2)
{
  //handle case 2
}
else if (stepNumber == 3)
{
  //handle case 3
}

Take care to match the braces around every case handler.

Thank you for helping me.

yea i have this code hire for push button thanks to Robin. But i need code:

pushButton pressed first time (do nothing)

pushButton pressed second time start (case1,case2,cas3,case4,case9) :slight_smile: