Unable to make LED blink using multiple switches

Hello, this is my first post here so if I have done anything wrong forum-wise please let me know.

I am trying to learn how to code certain things and then put all my knowledge together for an assignment I've been given.

I am currently trying to figure out how to make an LED begin flashing using delay when I press a momentary switch and then turn off when i press another switch. After i figure this out I would like to learn how to make it flash using blink without delay code.

I've been stuck for about 3 days furiously googling everything i could to do with this and reading many other forum posts but I've not found anything that I can understand enough to apply to my problem with my current knowledge.

Here is my code

const int ledPin = LED_BUILTIN;
int StartButton = 9;
int StopButton = 8; 
boolean Blink = false;
boolean Off = false;

void setup() 
{
  pinMode(ledPin, OUTPUT);
  pinMode(StartButton, INPUT_PULLUP);
  pinMode(StopButton, INPUT_PULLUP);
  digitalWrite(ledPin, HIGH);
  delay(80);
  digitalWrite(ledPin, LOW);
  delay(80);
  digitalWrite(ledPin, HIGH);
  delay(80);
  digitalWrite(ledPin, LOW);
  delay(80);
}

void loop()
{
  if (digitalRead(StartButton)==LOW){
   Blink = true;
   Off = false;
   
  }
  
if (digitalRead(StopButton)==LOW){
  Blink = false; 
  Off = true;
 }
  }

void BlinkProgram() {
 if (Blink == true) {
  digitalWrite(ledPin, HIGH);
  delay(300);
  digitalWrite(ledPin, LOW);
  delay(300);
 }
}

void OffProgram() {
  if (Off == true) {
    digitalWrite(ledPin, LOW);
  }
}

Nothing at all happens when I try to run this code other than the led blinking twice as in the setup code, I would really appreciate any help I could get with this :slight_smile: thanks

Hi,
firstly, hats off to you for actually posting the code and working hard before turning to us for help. Now give me a few minutes to read it and help you out. :slight_smile: I'll edit this comment when I'm done. Just posting so others know the reply is in the making and don't spend their own time writing one down. :smiley:

Edit: oops, you aren't actually calling the function BlinkProgram() anywhere! It never runs. Same goes with the OffProgram() !

Also, why use two variables (Blink and Off) when they mean the same thing? Either blink is True or Off is true, so why not just use one variable for both scenarios and then set it to true when the thing should blink and to false otherwise?

Try something like this (not tested):

const int ledPin = LED_BUILTIN;
int StartButton = 9;
int StopButton = 8; 
boolean Blink = false;


void setup() 
{
  pinMode(ledPin, OUTPUT);
  pinMode(StartButton, INPUT_PULLUP);
  pinMode(StopButton, INPUT_PULLUP);

}

void loop()
{
  if (digitalRead(StartButton)==LOW){
   Blink = true;
   }
  

if (digitalRead(StopButton)==LOW){
  Blink = false; 
  }


  if(Blink)
  {
      BlinkProgram();
  }
   else
   {
      StopProgram();
   }
}

void BlinkProgram() {

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

void OffProgram() {
    digitalWrite(ledPin, LOW);
}

Edit2: This is just a basic example, though. This is not "blink without delay" and so you will need to hold the button for a longer time to change the state, because the buttons aren't being read while the thing is blinking.

wow, thank you very much! this all now works as expected. I'll try modifying the code to work with blink without delay but so far this is great! most progress I've made past turning the led on and off with the buttons haha.

thegoodhen:
I'll edit this comment when I'm done. Just posting so others know the reply is in the making and don't spend their own time writing one down.

Why should you being busy with your reply, preclude anyone else from working on theirs?

neiklot:
Why should you being busy with your reply, preclude anyone else from working on theirs?

At the time I was reading the post, it had 0 replies, so I wanted to save the valuable time of other people by noting that I am writing down the reply (as it's a simple problem and the solutions of others would likely be indentical, and thus superfluous).

But someone else might have another idea, or maybe there is another problem that you didn't address. :-\

Actually, when you go to post the reply, it tells you - and shows you - if someone else has replied in the meantime so you can amend yours to suit.

Paul__B:
Actually, when you go to post the reply, it tells you - and shows you - if someone else has replied in the meantime so you can amend yours to suit.

Yeah but tgh "booked" the first reply slot with an "I'll be back" and someone would have seen that if they were hot on his (edit... her? hens are female) heels and thg hadn't yet come back with a solution.