0

I'am trying to get the program to run continusly at the press of the button and only stops when i press the second button but the program only runs once at the press of the button. Can some one share some light on this, i'am really new on the arduino stuff!!!


#include <Stepper.h>
#include <Bounce2.h>

// Declare the used pins
int dirA = 4;
int dirB = 5;
int pwmA = 2;
int pwmB = 3;

int switchState;
long randNumber;
const int buttonPin = 6;
const int buttonPin1 = 7;
int counter = 0;
byte lastPressCount;

// Declare a Stepper motor with 200 steps 
Stepper stepper1(200, dirA, dirB);

Bounce button;

void setup() {
    
  // PWM pins require declaration when used as Digital
  pinMode(pwmA, OUTPUT);
  pinMode(pwmB, OUTPUT);
  
  // Set PWM pins as always HIGH
  digitalWrite(pwmA, HIGH);
  digitalWrite(pwmB, HIGH);
  Serial.begin(9600);
  
  //Button1
  pinMode(buttonPin, INPUT);
  pinMode(buttonPin1, INPUT);
  digitalWrite(buttonPin, HIGH);
  digitalWrite(buttonPin1, HIGH);
  
  // Set stepper motor speed
  //stepper1.setSpeed(3000);

}
void loop(){

  randNumber = random(3000, 7000);
  //Serial.println(randNumber); 

  int buttonState;
  int buttonState1;
  buttonState = digitalRead(buttonPin);
  buttonState1 = digitalRead(buttonPin1);

 button.update();
  if (buttonState == LOW) {
  delay(1000);
    stepper1.setSpeed(randNumber);
    stepper1.step(10000);
    delay(100);
    stepper1.setSpeed(randNumber);
    stepper1.step(-10000);
    buttonState = HIGH;
 }
 else {
    if (buttonState == LOW) {
      stepper1.step(0);
      buttonState = LOW;
    }
  }
 
}

Here's the second code!!

#include <Bounce2.h>

// Declare the used pins
int dirA = 4;
int dirB = 5;
int pwmA = 2;
int pwmB = 3;

long randNumber;
const int buttonPin = 6;
const int buttonPin1 = 7;


// Declare a Stepper motor with 200 steps 
Stepper stepper1(200, dirA, dirB);

Bounce2::Button button = Bounce2::Button();

void setup() {
    
  // PWM pins require declaration when used as Digital
  pinMode(pwmA, OUTPUT);
  pinMode(pwmB, OUTPUT);
  
  // Set PWM pins as always HIGH
  digitalWrite(pwmA, HIGH);
  digitalWrite(pwmB, HIGH);
  Serial.begin(9600);
  
  //Button1
  button.attach( buttonPin, INPUT ); // USE EXTERNAL PULL-UP

  // DEBOUNCE INTERVAL IN MILLISECONDS
  button.interval(5); 

  // INDICATE THAT THE LOW STATE CORRESPONDS TO PHYSICALLY PRESSING THE BUTTON
  button.setPressedState(LOW);
  
  // Set stepper motor speed
  //stepper1.setSpeed(3000);

}
void loop(){

  button.update();

  randNumber = random(3000, 7000);
  //Serial.println(randNumber); 

 
  if ( button.pressed() ) {
  delay(1000);
    stepper1.setSpeed(randNumber);
    stepper1.step(10000);
    delay(100);
    stepper1.setSpeed(randNumber);
    stepper1.step(-10000);
 }
 else {
    
      stepper1.step(0);
     
    
  }
 
}```
11
  • 1
    It's a diferent situation, shoul i posted on the other post??
    – Nuno Berga
    Nov 24 at 17:29
  • 1
    Already did but the strange is that even the examplo code is not working for me, since it's giving me always thus error - Class Bounce has no member named current Duration on this - if ( debouncedState == LOW && bounce.currentDuration() > 1000 ) { and i have the library installed
    – Nuno Berga
    Nov 24 at 20:04
  • 1
    maybe you do not have the library that you think you have
    – jsotola
    Nov 24 at 20:23
  • 1
    Just used an example from the arduino library and it worked perfect, let's see if i can get it to wok with my code
    – Nuno Berga
    Nov 24 at 20:53
  • 1
    I did post the coed but it was eliminated
    – Nuno Berga
    Nov 25 at 14:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.