Help in Stepper motor with buttons

Hello I need personal help in code
I wrote all the code but could not put words to the button functions

Short he needs to do:
read button 1: If returning the stepper motor until the button is activated
if so read the button 2
read button 2: If so make the cycle in stepper motor
If no wait up to stay positive

follows the code I made before missing the description of LOW and HIGH words

thank longer anyone who can help me

#include //incluir a biblioteca do motor de passo

const int buttonPin = 0 ; //incluir o botao no pino 0

const int buttonPin1 = 1 ; //icluir o botao no pino 1

int buttonState = 0 ; //indicar o estado em que o botao deve ficar em v

int sensorPin = A0; //ativar o pino para leitura do potenciometro

int sensorValue = 0; //indicar o valor do potenciometro

const int stepsPerRevolution = 400 ; //indicar o numero de passo que o motor usa para dar 1 volta

Stepper myStepper (stepsPerRevolution , 6,7,8,9 ) ; //incluir os pinos que o motor de passo vai ultilizar

int stepCount = 0 ; //numero de passos que o motor deu

void setup() {

pinMode ( buttonPin , INPUT ) ; //avisando que o botao e para saida

pinMode ( buttonPin1 , INPUT ) ; //avisando que o botao e para saida

Serial.begin (9600) ; //porta serial 9600

myStepper.setSpeed ( 100 ) ;

}

void loop () {

buttonState = digitalRead ( buttonPin ) ; //ler o botao

if ( buttonState ==LOW ) {
digitalWrite ( , LOW ) ; //se o botao estiver desligado deve girar anti-hotario ate ativar o botao
Serial.println ( "couterclockwise" )
myStepper.step ( -1 )
delay ( 50 )
}

else {
digitalWrite ( , HIGH ) ; //se o botao estiver ativado efetuar a leitura do proximo botao
}

buttonState = digitalRead ( buttonPin1 ) ; //ler o botao

if ( buttonState ==HIGH ) {
digitalWrite ( , LOW ) ; //se o botao estiver ativado proseguir com os comandos seguintes

{
int sensorReading = analogRead(A0); //ler o portenciometro

int motorSpeed = map(sensorReading, 0, 1023, 0, 100); //mapear e alterar dados do potenciometro

if (motorSpeed > 0) { //ditura da velocidade
myStepper.setSpeed(motorSpeed);

Serial.println("clockwise"); //girar em sentido horario na quantidade de passos definida
myStepper.step(1600);
delay(500); //tempo de espera

myStepper.setSpeed(100); //velocidade fixa para retorno

Serial.println("counterclockwise"); //gira em sentido anti-horario na quantidade de passos definida
myStepper.step(-1600);

Serial.println ( "couterclockwise" )
myStepper.step ( -1 )
delay ( 50 )

else {
digitalWrite ( , LOW ) ; //se o botao estiver desativado efetuar a leitura novamente ate ele estar ativo
}

juniormontagner:
Hello I need personal help in code
I wrote all the code but could not put words to the button functions

Short he needs to do:
read button 1: If returning the stepper motor until the button is activated
if so read the button 2
read button 2: If so make the cycle in stepper motor
If no wait up to stay positive

follows the code I made before missing the description of LOW and HIGH words

thank longer anyone who can help me

I don't understand what you want the program to do.

You have 2 buttons, and you read the value of an Analog input to set the motor speed?

What does each button do?

Are they directions? Button1 = forward, Button2 = backward?

Or does Button1 alternate direction at each press (forward,backward,forward...) and is Button 2 used to force a Read of Analog input for Speed?

Chuck.

I'm sorry I explained in a confusing way

I will try to explain the complete process

the 1 button will be a switch sensor
to read whether this in the correct position to not leave the rail out

if it is in the wrong position (LOW) should return 1 in 1 step up to touch the switch and he positive (HIGH)

if the switch is positive (HIGH) read button 2

the second button will be a switch that activates the machine cycle
(Revolves clockwise at the speed potentiometer set the "A0" and back in counterclockwise direction at speed of 100 rpm to the starting point and waits for the next activation of the button 2)

2 button if it is positive (HIGH) makes the cycle
if it is negative (LOW) makes reading again until positive(HIGH)

I appreciate the help

thanks

juniormontagner:
I'm sorry I explained in a confusing way

I will try to explain the complete process

the 1 button will be a switch sensor
to read whether this in the correct position to not leave the rail out

if it is in the wrong position (LOW) should return 1 in 1 step up to touch the switch and he positive (HIGH)

if the switch is positive (HIGH) read button 2

the second button will be a switch that activates the machine cycle
(Revolves clockwise at the speed potentiometer set the "A0" and back in counterclockwise direction at speed of 100 rpm to the starting point and waits for the next activation of the button 2)

2 button if it is positive (HIGH) makes the cycle
if it is negative (LOW) makes reading again until positive(HIGH)

I appreciate the help

thanks

Ok, here is my interpretation of what you said.

  • Button 1 activation to enter calibration mode:
    If Button 1 is pressed then switch from 'run' mode to 'calibration' mode
    If (while Button 1 is pressed) and (while Button 2 is pressed), move counter clockwise 1 step.
    if (while Button 1 is pressed) and (while Button 2 is Not Pressed) move clockwise 1 step.
    When Button 1 is released, record new calibration position( count of steps taken). Return to Start Position.

  • button 2 activation to cycle system or position arm in calibration mode:
    If (Button 1 is NOT pressed) and (Button 2 is pressed) (run mode) move to Calibrated position, return to Start position.

#define button1Pin 2 // Button1 is connect to Pin 2 of the Arduino, change to your actual hardware configuration
#define button2Pin 3 // Button2 is connect to Pin 3 of the Arduino, change to your actual hardware configuration
#define buttonTimeOutPeriod 150 // 150 milliseconds switch must be held to activate

static int finalPosition= -200; // default number of step to move arm to activated position
static int currentPosition =0; // current position in number of steps from 0 (home)

static bool button1Pressed = false; // current state of button 1
static bool button2Pressed = false; // current state of button 2

void setup(){
pinMode(Button1Pin,INPUT_PULLUP);  //normally Closed switch, remove jumper from ground to activate
pinMode(Button2Pin,INPUT_PULLUP);//normally Closed switch, remove jumper from ground to activate
// setup Stepper

}

static unsigned long button1DebounceTimeout = 0; // counter to filter out switch bounce of buttons
static unsigned long button2DebounceTimeout = 0; // counter to filter out switch bounce of buttons

bool readButtons( bool b1pressed, bool b2pressed){
/* b1pressed and b2pressed with cause readButtons to return state of pressed buttons
*/
if(digitalRead(button1Pin)) { // button 1 is activated
  if(!button1Pressed){ // need to wait a least 
    if(button1DebounceTimeout ==0) button1DebounceTimeout = millis(); // start debounce cycle
    if(millis()-button1DebounceTimeout > buttonTimeoutPeriod){ // debounce period has expired
      button1Pressed = true;
      }
    }
  }
else { // button1 is not pressed
  button1Pressed = false;
  button1DebounceTimeout = 0;
  }

if(digitalRead(button2Pin)) { // button 2 is activated
  if(!button2Pressed){ // need to wait a least 
    if(button2DebounceTimeout ==0) button2DebounceTimeout = millis(); // start debounce cycle
    if(millis()-button2DebounceTimeout > buttonTimeoutPeriod){ // debounce period has expired
      button2Pressed = true;
      }
    }
  }
else { // button1 is not pressed
  button2Pressed = false;
  button2DebounceTimeout = 0;
  }
bool result = true; // default return

if(b1pressed)
  result = result & button1Pressed;

if(b2pressed)
  result = result & button2Pressed;

return result;
}
 
void moveTo(int newpos){
if(newpos!=currentPosition){ // move stepper
  stepper.step(newpos-currentPosition);
  currentPosition = newpos;
  }
}

void calibratePosition(){
while(readButtons(true,false){ // still in calibration mode
  if(readButtons(true,true)) { // both buttons are pressed
    finalPosition--; 
    }
  else if(!readButtons(true,true)&&readButtons(true,false)) { // button1 is pressed, button2 is NOT pressed
    finalPosition++;
    }

  stepper.setSpeed(10); // slow
  moveTo(finalPosition);
  delay(100); // wait 1/10 sec at new position
  }
stepper.setSpeed(100);
moveTo(0); // return home
}

void loop(){
if(readButtons(true,false)) { // Calibration mode
  calibratePosition();
  delay(1000);  // delay to allow fingers time to release button 2
  } 
else { // run mode
  if(readButtons(false,true){ // move to position
    // set speed 
    int rawSpeed = analogRead(A0);
    uint8_t speed = map(rawSpeed,0,1023,0,100);
    stepper.setSpeed(speed);
    moveTo(finalPosition);
    delay(10000);  // hold at position for 10 seconds;
    stepper.setSpeed(100);
    moveTo(0);  // move back to home
  }
}
}

This code is not great, but it should show you how to debounce switches

Chuck.

chucktodd:
Ok, here is my interpretation of what you said.

  • Button 1 activation to enter calibration mode:
    If Button 1 is pressed then switch from 'run' mode to 'calibration' mode
    If (while Button 1 is pressed) and (while Button 2 is pressed), move counter clockwise 1 step.
    if (while Button 1 is pressed) and (while Button 2 is Not Pressed) move clockwise 1 step.
    When Button 1 is released, record new calibration position( count of steps taken). Return to Start Position.

  • button 2 activation to cycle system or position arm in calibration mode:
    If (Button 1 is NOT pressed) and (Button 2 is pressed) (run mode) move to Calibrated position, return to Start position.

#define button1Pin 2 // Button1 is connect to Pin 2 of the Arduino, change to your actual hardware configuration

#define button2Pin 3 // Button2 is connect to Pin 3 of the Arduino, change to your actual hardware configuration
#define buttonTimeOutPeriod 150 // 150 milliseconds switch must be held to activate

static int finalPosition= -200; // default number of step to move arm to activated position
static int currentPosition =0; // current position in number of steps from 0 (home)

static bool button1Pressed = false; // current state of button 1
static bool button2Pressed = false; // current state of button 2

void setup(){
pinMode(Button1Pin,INPUT_PULLUP);  //normally Closed switch, remove jumper from ground to activate
pinMode(Button2Pin,INPUT_PULLUP);//normally Closed switch, remove jumper from ground to activate
// setup Stepper

}

static unsigned long button1DebounceTimeout = 0; // counter to filter out switch bounce of buttons
static unsigned long button2DebounceTimeout = 0; // counter to filter out switch bounce of buttons

bool readButtons( bool b1pressed, bool b2pressed){
/* b1pressed and b2pressed with cause readButtons to return state of pressed buttons
*/
if(digitalRead(button1Pin)) { // button 1 is activated
  if(!button1Pressed){ // need to wait a least
    if(button1DebounceTimeout ==0) button1DebounceTimeout = millis(); // start debounce cycle
    if(millis()-button1DebounceTimeout > buttonTimeoutPeriod){ // debounce period has expired
      button1Pressed = true;
      }
    }
  }
else { // button1 is not pressed
  button1Pressed = false;
  button1DebounceTimeout = 0;
  }

if(digitalRead(button2Pin)) { // button 2 is activated
  if(!button2Pressed){ // need to wait a least
    if(button2DebounceTimeout ==0) button2DebounceTimeout = millis(); // start debounce cycle
    if(millis()-button2DebounceTimeout > buttonTimeoutPeriod){ // debounce period has expired
      button2Pressed = true;
      }
    }
  }
else { // button1 is not pressed
  button2Pressed = false;
  button2DebounceTimeout = 0;
  }
bool result = true; // default return

if(b1pressed)
  result = result & button1Pressed;

if(b2pressed)
  result = result & button2Pressed;

return result;
}

void moveTo(int newpos){
if(newpos!=currentPosition){ // move stepper
  stepper.step(newpos-currentPosition);
  currentPosition = newpos;
  }
}

void calibratePosition(){
while(readButtons(true,false){ // still in calibration mode
  if(readButtons(true,true)) { // both buttons are pressed
    finalPosition--;
    }
  else if(!readButtons(true,true)&&readButtons(true,false)) { // button1 is pressed, button2 is NOT pressed
    finalPosition++;
    }

stepper.setSpeed(10); // slow
  moveTo(finalPosition);
  delay(100); // wait 1/10 sec at new position
  }
stepper.setSpeed(100);
moveTo(0); // return home
}

void loop(){
if(readButtons(true,false)) { // Calibration mode
  calibratePosition();
  delay(1000);  // delay to allow fingers time to release button 2
  }
else { // run mode
  if(readButtons(false,true){ // move to position
    // set speed
    int rawSpeed = analogRead(A0);
    uint8_t speed = map(rawSpeed,0,1023,0,100);
    stepper.setSpeed(speed);
    moveTo(finalPosition);
    delay(10000);  // hold at position for 10 seconds;
    stepper.setSpeed(100);
    moveTo(0);  // move back to home
  }
}
}




This code is not great, but it should show you how to debounce switches

Chuck.

Thanks
I will try this