Assignment: Make two buttons, one that makes an LED blink faster, and another that makes the LED blink slower

idk how to proceed. my code so far:

int ledPin = 3;
int but = 8;
int but2 =7;
int ledState = LOW;  
int slowState;  
int lastButtonState;    
int currentButtonState;
int dvalue = 1000;
int count = 0;
void setup()
{
   pinMode(ledPin, OUTPUT);
   pinMode(but,INPUT);
   pinMode(but2,INPUT);
  Serial.begin(9600);
  }

void loop()
{
  count++;
  lastButtonState    = currentButtonState;      
  currentButtonState = digitalRead(but);  
  slowState = digitalRead(but2);

  if(lastButtonState == LOW && currentButtonState == HIGH) {
    Serial.println("The button is pressed");
    ledState = !ledState;
    digitalWrite(ledPin, ledState);
    dvalue-=100;
    Serial.println(dvalue);
   
  }
  else if(lastButtonState == LOW && slowState == HIGH){
    Serial.println("The button is pressed");
    ledState = !ledState;
    digitalWrite(ledPin, ledState);
    dvalue+=100;
    Serial.println(dvalue);
  }

  digitalWrite(ledPin,HIGH);
  delay(dvalue);
  digitalWrite(ledPin,LOW);
  delay(dvalue);
 
}

Have you searched the forum?
I think I've seen this same assignment a few times just this semester.

This is how you should proceed: Continue to develop and test your code until it meets the requirements of the assignment.

If you need a more specific answer, try asking a more specific question.

Thanks for using code tags.

Seems you're trying to make it read a discrete click to do one increment or decrement, which might be over-complicating things unless your assignment specifically asks for that. I'd have gone for the simpler approach of just inc/decrementing while the button is held down.

Here is your project in Wokwi simulation:

It is your sketch in Wokwi, I have not changed it.
Do you have the pullup resistors to 5V ?

Tips:

  1. Make the text of the source code look better. It is not possible to make a good sketch when the text layout is not good. Put every space, every indent, every comma, every new line, at the right place.

For example:

void setup()
{
   pinMode(ledPin, OUTPUT);
   pinMode(but,INPUT);
   pinMode(but2,INPUT);
  Serial.begin(9600);
  }

Some lines have a indent of 3 spaces, some 2. The closing bracked '}' should not have a indent.

Another example:

    ledState = !ledState;
    digitalWrite(ledPin, ledState);
    dvalue-=100;

The first line has spaces around the "=". The third line has no spaces around "-=". That is not consistant.

  1. The names "but" and "but2" have no meaning. They can be called "slowerButtonPin" and "fasterButtonPin".

  2. In the setup(), you can use INPUT_PULLUP for extra safety. Together with the external pullup, they will keep the input HIGH when the button is not pressed.

  3. You have different code for the two buttons. The buttons should be treated in the same way.

  4. You use the 'lastButtonState' for both buttons. Then you mix things that should not be mixed. Better names will help, for example "lastSlowerButtonState" and "lastFasterButtonState". Rewrite that part of the code, it is not there yet.

  5. Set the initial value of those two variables to HIGH. That is the default when no button is pressed.

  6. Do as silvershovel wrote. Let the led blink at the end of the loop(), there is no need to do something with the "ledState" or "digitalWrite()" at the moment a button is pressed. Use the buttons to only change the delay.

  7. Start with a delay of 100ms or 200ms, then you see something happening faster.

This assignment SEEMS simple; however there are complications.
What happens if the button shows switch bounce?
What if both are pressed at once?
What are the limits of the "faster" or "slower"?

A VERY simple interpretation of the assignment would allow just two flash rates - "fast" and "slow". A bit like this:

int flashrate;
int fast = 200;
int slow = 500;

loop{
if (button a pressed) flashrate = fast:
if (button b pressed) flashrate = slow.
blink(flashrate);
}

and a simple routine using the "blink" program

void blink (int rate) {
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
delay(rate);
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(rate);
}

Hello wistar96
Take view to the BLINKWITHOUTDELAY example of the IDE and add the button action as needed.

Have a nice day and enjoy coding in C++.
Дайте миру шанс!

And there's the voice of experience- 80% of the time the actual coding is simpler than the effort spent trying to understand the requirement in the first place.

1 Like

Hello wistar96
Take a view in this small example:


// constants won't change. Used here to set a pin number:
const int ledPin =  LED_BUILTIN;// the number of the LED pin
const int Button1 =  A0;
const int Button2 =  A1;
// Variables will change:
int ledState = LOW;             // ledState used to set the LED
unsigned long interval = 1000;
// Generally, you should use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
unsigned long previousMillis = 0;        // will store last time LED was updated
// constants won't change:
const unsigned long Interval1 = 1000;           // interval at which to blink (milliseconds)
const unsigned long Interval2 = 100;           // interval at which to blink (milliseconds)
const unsigned long IntervalStep = 25;           
void setup() {
  // set the digital pin as output:
  pinMode(ledPin, OUTPUT);
  pinMode(Button1, INPUT_PULLUP);
  pinMode(Button2, INPUT_PULLUP);
  Serial.begin(9600);
}
void loop() {
  // here is where you'd put code that needs to be running all the time.
  // check to see if it's time to blink the LED; that is, if the difference
  // between the current time and last time you blinked the LED is bigger than
  // the interval at which you want to blink the LED.
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;
    // read button and make action
    if (!digitalRead(Button1) && interval< Interval1) interval +=IntervalStep;
    if (!digitalRead(Button2) && interval> Interval2) interval -=IntervalStep;
    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW) {
      ledState = HIGH;
    } else {
      ledState = LOW;
    }
    // set the LED with the ledState of the variable:
    digitalWrite(ledPin, ledState);
    Serial.println(interval);
  }
}

Have a nice day and enjoy coding in C++.
Дайте миру шанс!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.