[SOLVED] Program override with buttons

Hi people,

i have made a program for automatic controled roller blinds.

the program run well at the moment but i need to integrate 2 buttons for manual down and up

i have a program for this too, seprate but i cant combine this 2 programs

could somebody help me

thanks

the main program

void loop() {

  if ((analogRead(ldr) <= 500) && (digitalRead (home_switch) == LOW)) // if lightsensor value under 500 and home switch in position = active
  {
    long i = StepCounter;
    for (i = 0; i <= DISTANCE; i++) {

      digitalWrite(dir_pin, HIGH);    // turn counter clockwise shade moves down
      Stepping = true;                // turn Motor on

      if (Stepping == true)
      {
        digitalWrite(step_pin, HIGH);
        delayMicroseconds(1000);      // Motor speed > more delay = slow speed
        digitalWrite(step_pin, LOW);
        delayMicroseconds(1000);

        StepCounter = StepCounter + 1;
        if (StepCounter == DISTANCE)
        {
          Stepping = false;
        }
      }
    }
  }

  delay(2000); // wait 60sec





  while (analogRead(ldr) <= 500) {
    delay(2000); // wait 60sec
  }




  if ((analogRead(ldr) > 500) && (digitalRead (home_switch) == HIGH))  // if lightsensor value over 500 and home switch NOT in position = NOT active
  {
    digitalWrite(dir_pin, LOW); // turn clockwise shade moves up
    Stepping = true;            // turn Motor on

    if (Stepping == true)
    {
      while (digitalRead(home_switch)) {  // Do this until the switch is activated - no steps counted to trigger the home switch for home position
        digitalWrite(step_pin, HIGH);
        delayMicroseconds(1000);
        digitalWrite(step_pin, LOW);
        delayMicroseconds(1000);
      }

      {
        Stepping = false;
      }
    }
  }


  delay(2000); // wait 60sec



  while (analogRead(ldr) > 500) {
    delay(2000); // wait 60sec
  }

}

and the buttons

void loop() {
   if (digitalRead(button_down) == LOW && Stepping == false)
  {
    digitalWrite(dir_pin, HIGH);
    Stepping = true;
  }
  if (digitalRead(button_up) == LOW && Stepping == false)
  {
    digitalWrite(dir_pin, LOW);
    Stepping = true;
  }

  if (Stepping == true)
  {
    digitalWrite(step_pin, HIGH);
    delay(1);
    digitalWrite(step_pin, LOW);
    delay(1);

    StepCounter = StepCounter + 1;

    if (StepCounter == DISTANCE)
    {
      StepCounter = 0;
      Stepping = false;
    }
  }


}

Why can't you combine them? What have you tried?

im a beginner in arduino, thats the problem

i dont really know how to do this

You simply need to add buttons check to the condition for automatic movement in program 1

Try this code:

void Step()
{
    digitalWrite(step_pin, HIGH);
    delayMicroseconds(1000);      // Motor speed > more delay = slow speed
    digitalWrite(step_pin, LOW);
    delayMicroseconds(1000);
}

void MoveDown()
{
	// safety check, move down only if home_switch is low
	if (digitalRead (home_switch) != LOW)
		return;
	
    digitalWrite(dir_pin, HIGH);    // turn counter clockwise shade moves down

    for (long i = 0; i < DISTANCE; i++) 
		Step();
}

void MoveUp()
{
    digitalWrite(dir_pin, LOW);    // turn clockwise shade moves up

    while (digitalRead(home_switch) == HIGH) 
   		Step();
}

#define NO_MOVE   0
#define MOVE_DOWN 1
#define MOVE_UP   2

#define DELAY_AFTER_MOVE 20000 // 20 sec delay

int Command()
{
	if ( ( (analogRead(ldr) <= 500) || (digitalRead(button_down) == LOW) ) && 
	     (digitalRead (home_switch) == LOW) )
	     return(MOVE_DOWN);
	     
	if ( ( (analogRead(ldr) > 500) || (digitalRead(button_up) == LOW) ) && 
	     (digitalRead (home_switch) == HIGH) )
	     return(MOVE_UP);
	     
	delay(100);
	return(NO_MOVE);     
}

void loop()
{
	switch(Command())
	{
	case MOVE_DOWN:
		MoveDown();
		delay(DELAY_AFTER_MOVE);
		break;
	
	case MOVE_UP:	
		MoveUp();
		delay(DELAY_AFTER_MOVE);		
		break;
	}	
}

thank you it helped alot
but now there is a problem
if its manaul rolling up and the home position is active it dont hold the position it go down again if its dark
and if moved down it moves up again if its bright

somebody know how this can be fixed?

You have to post your code so we can see how to help.

Read the post "Read this before asking a programming question..."

This is the code updated by @vlc0617

the problem is
if its manaul rolling up and the home position is active, it dont hold the position it go down again if its dark outside
->because ldr trigger go down in automatic mode

and if moved down it moves up again if its bright outside
->because ldr trigger go up in automatic mode

automatic control by ldr works fine

#define DISTANCE 800   // 1 rotation = 400 steps

// Define the Pins used

#define sleep_pin 2    // Pin 2 connected to SLEEP pin
#define MS1 3          // Pin 3 connected to MS1 pin
#define MS2 4          // Pin 4 connected to MS2 pin
#define dir_pin 5      // Pin 5 connected to Direction pin
#define step_pin 6     // Pin 6 connected to Steps pin on EasyDriver
#define home_switch 10 // Pin 10 connected to Home Switch (ReedSwitch)
#define button_up 11   // Pin 11 connected to Button UP
#define button_down 12 // Pin 12 connected to Button DOWN


long StepCounter = 0;  // Stepcounter
int Stepping = false;  // Motor stop
int ldr = A0;          // Lightsensor connected to A0
int direction;         // Variable to set Rotation (CW-CCW) of the motor
int steps;             // Used to set HOME position after Homing is completed

void setup() {
  pinMode(MS1, OUTPUT);
  pinMode(MS2, OUTPUT);
  pinMode(dir_pin, OUTPUT);
  pinMode(step_pin, OUTPUT);
  pinMode(sleep_pin, OUTPUT);

  pinMode(home_switch, INPUT_PULLUP);
  pinMode(button_up, INPUT_PULLUP);
  pinMode(button_down, INPUT_PULLUP);


  digitalWrite(sleep_pin, HIGH);  // Wake up EasyDriver
  delay(5);                       // Wait for EasyDriver wake up


  /* Configure type of Steps on EasyDriver:
    // MS1 MS2
    //
    // LOW LOW = Full Step //
    // HIGH LOW = Half Step //
    // LOW HIGH = A quarter of Step //
    // HIGH HIGH = An eighth of Step //
  */

  digitalWrite(MS1, HIGH);   // Configures to Full Steps
  digitalWrite(MS2, LOW);    // Configures to Full Steps



  // Start Homing procedure of Stepper Motor at startup

  while (digitalRead(home_switch)) {  // Do this until the switch is activated
    digitalWrite(dir_pin, LOW);       // (HIGH = anti-clockwise / LOW = clockwise)
    digitalWrite(step_pin, HIGH);
    delay(5);                         // Delay to slow down speed of Stepper
    digitalWrite(step_pin, LOW);
    delay(5);
  }
  /*
    while (!digitalRead(home_switch)) { // Do this until the switch is not activated
     digitalWrite(dir_pin, LOW);
     digitalWrite(step_pin, HIGH);
     delay(10);                       // More delay to slow even more while moving away from switch
     digitalWrite(step_pin, LOW);
     delay(10);
    }  */

  steps = 0; // Reset position variable to zero

}

void Step()
{
  digitalWrite(step_pin, HIGH);
  delayMicroseconds(1000);      // Motor speed > more delay = slow speed
  digitalWrite(step_pin, LOW);
  delayMicroseconds(1000);
}

void MoveDown()
{
  // safety check, move down only if home_switch is low
  if (digitalRead (home_switch) != LOW)
    return;

  digitalWrite(dir_pin, HIGH);    // turn counter clockwise shade moves down

  for (long i = 0; i < DISTANCE; i++)
    Step();
}

void MoveUp()
{
  digitalWrite(dir_pin, LOW);    // turn clockwise shade moves up - no steps counted to trigger the home switch for home position

  while (digitalRead(home_switch) == HIGH)
    Step();
}

#define NO_MOVE   0
#define MOVE_DOWN 1
#define MOVE_UP   2

#define DELAY_AFTER_MOVE 5000 // 5 sec delay

int Command()
{
  if ( ( (analogRead(ldr) <= 500) || (digitalRead(button_down) == LOW) ) &&
       (digitalRead (home_switch) == LOW) )
    return (MOVE_DOWN);

  if ( ( (analogRead(ldr) > 500) || (digitalRead(button_up) == LOW) ) &&
       (digitalRead (home_switch) == HIGH) )
    return (MOVE_UP);

  delay(100);
  return (NO_MOVE);
}

void loop()
{
  switch (Command())
  {
    case MOVE_DOWN:
      MoveDown();
      delay(DELAY_AFTER_MOVE);
      break;

    case MOVE_UP:
      MoveUp();
      delay(DELAY_AFTER_MOVE);
      break;
  }
}

What are you using for your up and down buttons?
A single ON-OFF-ON toggle switch would work for you. That way one ON position would set it to DOWN, the other would be UP, and the center OFF would be automatic depending on light levels.

i use normal push butons

close on push and opens at release

the home switch is a reed switch

i try your suggestion

I believe if you do a site search on 'chicken coop door' you will find a lot of projects quite similar to yours.

evanmars:
A single ON-OFF-ON toggle switch would work for you.

does not work same problem
if i roll it down on day it wil go up after this

i got my origin program in 1st post to work without push button

i just switch of the ldr for night simulation and switch it on 5v for day simulation
for automatic i switch to the ldr

via 3 position switch

not the best solution but it works

i thought it could be done via push buttons

the working code

#define DISTANCE 800   // 1 rotation = 400 steps

// Define the Pins used

#define MS1 2          // Pin 2 connected to MS1 pin
#define sleep_pin 3    // Pin 3 connected to SLEEP pin
#define MS2 4          // Pin 4 connected to MS2 pin
#define dir_pin 5      // Pin 5 connected to Direction pin
#define step_pin 6     // Pin 6 connected to Steps pin on EasyDriver
#define home_switch 10 // Pin 10 connected to Home Switch (ReedSwitch)
//#define button_up 11   // Pin 11 connected to Button UP
//#define button_down 12 // Pin 12 connected to Button DOWN


long StepCounter = 0;  // Stepcounter
int Stepping = false;  // Motor stop
int ldr = A0;          // Lightsensor connected to A0
int direction;         // Variable to set Rotation (CW-CCW) of the motor
int steps;             // Used to set HOME position after Homing is completed

void setup() {
  pinMode(MS1, OUTPUT);
  pinMode(MS2, OUTPUT);
  pinMode(dir_pin, OUTPUT);
  pinMode(step_pin, OUTPUT);
  pinMode(sleep_pin, OUTPUT);

  pinMode(home_switch, INPUT_PULLUP);
  //pinMode(button_up, INPUT_PULLUP);
  //pinMode(button_down, INPUT_PULLUP);


  digitalWrite(sleep_pin, HIGH);  // Wake up EasyDriver
  delay(5);                       // Wait for EasyDriver wake up


  /* Configure type of Steps on EasyDriver:
    // MS1 MS2
    //
    // LOW LOW = Full Step //
    // HIGH LOW = Half Step //
    // LOW HIGH = A quarter of Step //
    // HIGH HIGH = An eighth of Step //
  */

  digitalWrite(MS1, HIGH);   // Configures to Full Steps
  digitalWrite(MS2, LOW);    // Configures to Full Steps



  // Start Homing procedure of Stepper Motor at startup

  while (digitalRead(home_switch)) {  // Do this until the switch is activated
    digitalWrite(dir_pin, LOW);       // (HIGH = anti-clockwise / LOW = clockwise)
    digitalWrite(step_pin, HIGH);
    delay(5);                         // Delay to slow down speed of Stepper
    digitalWrite(step_pin, LOW);
    delay(5);
  }
  /*
    while (!digitalRead(home_switch)) { // Do this until the switch is not activated
     digitalWrite(dir_pin, LOW);
     digitalWrite(step_pin, HIGH);
     delay(10);                       // More delay to slow even more while moving away from switch
     digitalWrite(step_pin, LOW);
     delay(10);
    }  */

  steps = 0; // Reset position variable to zero

}


void loop() {

  if ((analogRead(ldr) <= 500) && (digitalRead (home_switch) == LOW)) // if lightsensor value under 500 and home switch in position = active
  {
    long i = StepCounter;
    for (i = 0; i <= DISTANCE; i++) {

      digitalWrite(dir_pin, HIGH);    // turn counter clockwise shade moves down
      Stepping = true;                // turn Motor on

      if (Stepping == true)
      {
        digitalWrite(step_pin, HIGH);
        delayMicroseconds(1000);      // Motor speed > more delay = slow speed
        digitalWrite(step_pin, LOW);
        delayMicroseconds(1000);

        StepCounter = StepCounter + 1;
        if (StepCounter == DISTANCE)
        {
          Stepping = false;
        }
      }
    }
  }

  delay(2000); // wait 60sec


  


  while (analogRead(ldr) <= 500) {
    delay(2000); // wait 60sec
  }




  if ((analogRead(ldr) > 500) && (digitalRead (home_switch) == HIGH))  // if lightsensor value over 500 and home switch NOT in position = NOT active
  {
    digitalWrite(dir_pin, LOW); // turn clockwise shade moves up
    Stepping = true;            // turn Motor on

    if (Stepping == true)
    {
      while (digitalRead(home_switch)) {  // Do this until the switch is activated - no steps counted to trigger the home switch for home position
        digitalWrite(step_pin, HIGH);
        delayMicroseconds(1000);
        digitalWrite(step_pin, LOW);
        delayMicroseconds(1000);
      }

      {
        Stepping = false;
      }
    }
  }


  delay(2000); // wait 60sec

  


  while (analogRead(ldr) > 500) {
    delay(2000); // wait 60sec
  }

}

and the breadboard

Well, if you want to use push buttons, I think you need a 2 position switch to commute between automatic and manual

Then change the Command function in my code this way:

int Command()
{
    if (digitalRead(automanual_switch) == LOW)
    {
        // automatic functioning

       if ( (analogRead(ldr) <= 500)  && (digitalRead (home_switch) == LOW) )
           return(MOVE_DOWN);
    
       if ( (analogRead(ldr) > 500) &&  (digitalRead (home_switch) == HIGH) )
           return(MOVE_UP);
    }

    else
    {
       // manual functioning

       if (  (digitalRead(button_down) == LOW) && (digitalRead (home_switch) == LOW) )
	     return(MOVE_DOWN);
	     
	if ( (digitalRead(button_up) == LOW) && (digitalRead (home_switch) == HIGH) )
	     return(MOVE_UP);
    }
    
    delay(100);
    return(NO_MOVE);     
}

thanks

this work good

now the code is

#define DISTANCE 800   // 1 rotation = 400 steps modify for custom blind length

// Define the Pins used

#define sleep_pin 2          // Pin 2 connected to SLEEP pin
#define MS1 3                // Pin 3 connected to MS1 pin
#define MS2 4                // Pin 4 connected to MS2 pin
#define dir_pin 5            // Pin 5 connected to Direction pin
#define step_pin 6           // Pin 6 connected to Steps pin on EasyDriver
#define mode_switch 7        // Pin 7 connected to 2 position Switch (Manaul/Auto mode)
#define home_switch 10       // Pin 10 connected to Home Switch (ReedSwitch)
#define button_up 11         // Pin 11 connected to Button UP
#define button_down 12       // Pin 12 connected to Button DOWN


long StepCounter = 0;  // Stepcounter
int Stepping = false;  // Motor stop
int ldr = A0;          // Lightsensor connected to A0
int direction;         // Variable to set Rotation (CW-CCW) of the motor
int steps;             // Used to set HOME position after Homing is completed

void setup() {
  pinMode(MS1, OUTPUT);
  pinMode(MS2, OUTPUT);
  pinMode(dir_pin, OUTPUT);
  pinMode(step_pin, OUTPUT);
  pinMode(sleep_pin, OUTPUT);

  pinMode(home_switch, INPUT_PULLUP);
  pinMode(button_up, INPUT_PULLUP);
  pinMode(button_down, INPUT_PULLUP);
  pinMode(mode_switch, INPUT_PULLUP);


  digitalWrite(sleep_pin, HIGH);  // Wake up EasyDriver
  delay(5);                       // Wait for EasyDriver wake up


  /* Configure type of Steps on EasyDriver:
    // MS1 MS2
    //
    // LOW LOW = Full Step //
    // HIGH LOW = Half Step //
    // LOW HIGH = A quarter of Step //
    // HIGH HIGH = An eighth of Step //
  */

  digitalWrite(MS1, HIGH);   // Configures to Full Steps
  digitalWrite(MS2, LOW);    // Configures to Full Steps



  // Start Homing procedure of Stepper Motor at startup

  while (digitalRead(home_switch)) {  // Do this until the switch is activated
    digitalWrite(dir_pin, LOW);       // (HIGH = anti-clockwise / LOW = clockwise)
    digitalWrite(step_pin, HIGH);
    delay(5);                         // Delay to slow down speed of Stepper
    digitalWrite(step_pin, LOW);
    delay(5);
  }
  /*
    while (!digitalRead(home_switch)) { // Do this until the switch is not activated
     digitalWrite(dir_pin, LOW);
     digitalWrite(step_pin, HIGH);
     delay(10);                       // More delay to slow even more while moving away from switch
     digitalWrite(step_pin, LOW);
     delay(10);
    }  */

  steps = 0; // Reset position variable to zero

}

void Step()
{
  digitalWrite(step_pin, HIGH);
  delayMicroseconds(1000);      // Motor speed > more delay = slow speed
  digitalWrite(step_pin, LOW);
  delayMicroseconds(1000);
}

void MoveDown()
{
  // safety check, move down only if home_switch is low
  if (digitalRead (home_switch) != LOW)
    return;

  digitalWrite(dir_pin, HIGH);    // turn counter clockwise shade moves down

  for (long i = 0; i < DISTANCE; i++)
    Step();
}

void MoveUp()
{
  digitalWrite(dir_pin, LOW);    // turn clockwise shade moves up - no steps counted to trigger the home switch for home position

  while (digitalRead(home_switch) == HIGH)
    Step();
}

#define NO_MOVE   0
#define MOVE_DOWN 1
#define MOVE_UP   2

#define DELAY_AFTER_MOVE 5000 // 5 sec delay

int Command()
{
  if (digitalRead(mode_switch) == HIGH)
  {
    // automatic functioning

    if ( (analogRead(ldr) <= 500)  && (digitalRead (home_switch) == LOW) )
      return (MOVE_DOWN);

    if ( (analogRead(ldr) > 500) &&  (digitalRead (home_switch) == HIGH) )
      return (MOVE_UP);
  }

  else
  {
    // manual functioning

    if (  (digitalRead(button_down) == LOW) && (digitalRead (home_switch) == LOW) )
      return (MOVE_DOWN);

    if ( (digitalRead(button_up) == LOW) && (digitalRead (home_switch) == HIGH) )
      return (MOVE_UP);
  }

  delay(100);
  return (NO_MOVE);
}

void loop()
{
  switch (Command())
  {
    case MOVE_DOWN:
      MoveDown();
      delay(DELAY_AFTER_MOVE);
      break;

    case MOVE_UP:
      MoveUp();
      delay(DELAY_AFTER_MOVE);
      break;
  }
}