Try to swtich cases with a button

Hi I am really new to Arduino so please bear with me :)

I am using a servo to control the speed of string. But I want to give it 2 speeds. Fast and Slow. I want to toggle between the speeds with a push button

How do i get about doing that?

Thanks so much for your help guys!

I am using a servo to control the speed of string

What does that mean, please?

We can’t see your code.

Can you get the two speeds to work one at a time?

Can you get the button to light an LED?

Do you know enough about variables to remember if the button was up or down in the previous loop?

Have you heard of debouncing? You will probably need some debouncing for your button.

AWOL: What does that mean, please?

We can't see your code.

Im sorry ignore that. I just want to make a toggle between fast and slow methods with a button press

MorganS: Can you get the two speeds to work one at a time?

Can you get the button to light an LED?

Do you know enough about variables to remember if the button was up or down in the previous loop?

Have you heard of debouncing? You will probably need some debouncing for your button.

yep i can.

Basically i have 2 methods

slow() and fast()

I want to create a method that would switch between them with a button press. I havent created any viable yet. I will go do some research about debouncing now.

First time the buttonPin goes from HIGH to LOW change variable speedState to 1 Next time the buttonPin goes from HIGH to LOW change speedState to 0 Allow a short period between the digitalRead()s

Select the servo speed depending on the value of speedState

Have a look at the demo several things at a time.

...R

Sorry i do not understand.

nerdkidleo: Sorry i do not understand.

If you can explain in more detail what you don't understand I will try to help. But it seems pointless for me to write a long essay that tries to anticipate you thought process.

Have you studied the code in several things at a time - both its content and its structure?

It may also be useful to look at planning and implementing a program

...R

something like this (untested):

boolean buttonFast = false;
int lastButtonState = LOW;

void loop()
{
    int currentButtonState = ReadButton();

    // check for button press: that is, transition LOW->HIGH
    if (currentButtonState == HIGH && lastButtonState == LOW)
    {
        // swap motor speed
        if (buttonFast == true)
        {
             buttonFast = false;
             slow();
        }
        else
        {
              buttonFast = true;
              fast();
         }
    }

    lastButtonState = currentButtonState;
}

Robin2: If you can explain in more detail what you don't understand I will try to help. But it seems pointless for me to write a long essay that tries to anticipate you thought process.

Have you studied the code in several things at a time - both its content and its structure?

It may also be useful to look at planning and implementing a program

...R

From what i can infer, i understand that in order to do alot of things at once you must have a timer, thats where the millis comes in. even that i couldn't get right.

nerdkidleo: From what i can infer, i understand that in order to do alot of things at once you must have a timer, thats where the millis comes in. even that i couldn't get right.

Yes, you are correct. In order to be able to control when and how often things happen in parallel, you often need to use millis().

However, for you, you are simply changing state using a button, so you do not need millis() for that.

arduinodlb: Yes, you are correct. In order to be able to control when and how often things happen in parallel, you often need to use millis().

However, for you, you are simply changing state using a button, so you do not need millis() for that.

sweet...millis() is cray cray. i may need it in the future. because i would like to put a delay after ever 45 turn on the servo(full rotation).

arduinodlb: something like this (untested):

boolean buttonFast = false;
int lastButtonState = LOW;

void loop() {     int currentButtonState = ReadButton();

    // check for button press: that is, transition LOW->HIGH     if (currentButtonState == HIGH && lastButtonState == LOW)     {         // swap motor speed         if (buttonFast == true)         {             buttonFast = false;             slow();         }         else         {               buttonFast = true;               fast();         }     }

    lastButtonState = currentButtonState; }

what is this method readbutton()?

#include <Servo.h>

// constants won't change. They're used here to
// set pin numbers:
const int buttonPin = 10;    // the number of the pushbutton pin
const int ledPin = 13;      // the number of the LED pin

// Variables will change:
int ledState = HIGH;         // the current state of the output pin
int buttonState;             // the current pinstate from the input pin
int lastButtonState = LOW;   // the previous pinstate from the input pin

// Stuff to mess with time.
long lastDebounceTime = 0;  // the last time the output pin was toggled
long debounceDelay = 50;    // the debounce time; increase if the output flickers

//servo stuff
Servo myservo;

int state = LOW;
int pos = 0;

int ledstate = digitalRead(13);

long previousMillis = 0;
long interval = 1000;

void setup() { 
  
  Serial.begin(9600); // display on serial monitor what is happening
  pinMode(buttonPin, INPUT); //button on input : pin 10
  pinMode(ledPin, OUTPUT); //LED on pin 13

  // set initial LED state
  digitalWrite(ledPin, ledState); //LED will start with HIGH

  //attach servo
  myservo.attach(9);  
  
  //set the digital pin as output
  pinMode(ledPin, OUTPUT);
  digitalWrite(13, LOW);
  
}

void loop() {

  debounce();
  
  Serial.println(ledstate);
  
  if(ledstate == LOW){
    slow();
    
  }
  if(ledstate == HIGH) {
    fast();
  }
  
}

void slow() {
  myservo.write(90);
  }


void fast(){
  myservo.write(180);
}

void debounce() {
  // read the state of the switch into a local variable:
  int pinstate = digitalRead(buttonPin); //pinstate pin 10
  // check to see if you just pressed the button
  // (i.e. the input went from LOW to HIGH),  and you've waited
  // long enough since the last press to ignore any noise:

  // If the switch changed, due to noise or pressing:
  if (pinstate != lastButtonState) { //if pin 10 (high or low) != last button state (high or low)
    // reset the debouncing timer
    lastDebounceTime = millis();
  }

  if ((millis() - lastDebounceTime) > debounceDelay) {
    // whatever the pinstate is at, it's been there for longer
    // than the debounce delay, so take it as the actual current state:

    // if the button state has changed:
    if (pinstate != buttonState) {
      buttonState = pinstate;

      // only toggle the LED if the new button state is HIGH
      if (buttonState == HIGH) {
        ledState = !ledState;
      }
    }
  }

  // set the LED:
  digitalWrite(ledPin, ledState);

  // save the pinstate.  Next time through the loop,
  // it'll be the lastButtonState:
  lastButtonState = pinstate;
}

This is what i have so far, the methods are not switching its on fast all the time.

I have cleaned up my code a little and this is the latest version.

#include <Servo.h>

Servo myservo;

//contants
const int buttonPin = 10;
const int ledPin = 13;

//variables
int ledState = HIGH;
int buttonState;
int lastButtonState = LOW;

//weird time stuff
long lastDebounceTime = 0;
long debounceDelay = 50;

//servo
int pos = 0;

//boolean
boolean buttonFast = false;

void setup() {
  myservo.attach(9);

  pinMode(buttonPin, INPUT);
  pinMode(ledPin, OUTPUT);

  digitalWrite(ledPin, ledState);
}

void loop()  {
  int currentButtonState = ReadButton();
  
  if (currentButtonState == HIGH && lastButtonState == LOW)
  {
    if(buttonFast == true)
    {
       buttonFast = false;
       slow();
    }
    else
    {
      buttonFast = true;
      fast();
    }
  }
  
  lastButtonState = currentButtonState;
}

void slow()  {
  for (pos = 100; pos <= 110; pos ++)  {
    myservo.write(pos);
  }
}

void fast()  {
  myservo.write(180);
}

void debounce()  {
  int reading = digitalRead(buttonPin);

  if (reading != lastButtonState) {
    // reset the debouncing timer
    lastDebounceTime = millis();
  }

  if ((millis() - lastDebounceTime) > debounceDelay) {
    if (reading != buttonState) {
      buttonState = reading;
      if (buttonState == HIGH) {
        ledState = !ledState;
      }
    }
  }

  digitalWrite(ledPin, ledState);

  lastButtonState = reading;
}

nerdkidleo:
From what i can infer, i understand that in order to do alot of things at once you must have a timer, thats where the millis comes in. even that i couldn’t get right.

The idea behind using millis() is very straightforward. The millis() function acts like a clock that gives you the number of millsecs since your Arduino started. To time something just record millis() at the start and then keep checking the difference between that value and the latest value. When the difference exceeds the required interval it is time to do something. Suppose you want a 2 second interval - 2000 millisecs.

Every time loop() repeats you do something like this

if (millis() - startMills >= 2000) {
    // this code will work when the 2000 millisecs have elapsed
}

Keep in mind that loop() will normally repeat hundreds or thousands of times per second.

Study the code in several things at a time until you understand it. Using millis() is an important technique to learn. Download and try the example. Make some changes to it so you can explore what happens. Take your time. Don’t expect to understand it after only 2 or 3 read-throughs.

…R

Robin2: The idea behind using millis() is very straightforward. The millis() function acts like a clock that gives you the number of millsecs since your Arduino started. To time something just record millis() at the start and then keep checking the difference between that value and the latest value. When the difference exceeds the required interval it is time to do something. Suppose you want a 2 second interval - 2000 millisecs.

Every time loop() repeats you do something like this

if (millis() - startMills >= 2000) {
    // this code will work when the 2000 millisecs have elapsed
}

Keep in mind that loop() will normally repeat hundreds or thousands of times per second.

Study the code in several things at a time until you understand it. Using millis() is an important technique to learn. Download and try the example. Make some changes to it so you can explore what happens. Take your time. Don't expect to understand it after only 2 or 3 read-throughs.

...R

Thank you Robin2. I will. This is really interesting.