Trouble getting an LED to blink within an if statement

Hey all, I'm new to coding Arduinos, so it would be much appreciated if you could help me out. I'm trying to get the onboard LED to blink on and off for 100 ms each when I press one switch and then stop blinking and turn off when I press a second switch. I've figured out how to get the LED to turn on with the first switch and off with the second switch with this code:

//variables that don't change
const int Switch1=7;
const int Switch2=11;

//variables that do change
int currentS1state=0;
int lastS1state=0;
int currentS2state=0;
int lastS2state=0;

void setup (){
   pinMode(Switch1,INPUT);
   pinMode(Switch2,INPUT);
   pinMode(13,OUTPUT);
}

void loop(){
   currentS1state = digitalRead(Switch1);
   lastS1state = digitalRead(Switch1);
   currentS2state = digitalRead(Switch2);
   lastS2state = digitalRead(Switch2);

   if (currentS1state != lastS1state){
    if(currentS1state == HIGH){
        digitalWrite(13,HIGH);
        
    }
   }
   if (lastS1state == LOW){
    if (currentS2state == HIGH){
       digitalWrite(13,LOW);
    }
   }
}

When I add a delay to the LED to make it blink, it only turns on for 100 ms and then off, which I guess is due to the nature of delay. I've tried messing around with millis(), but I haven't been able to get it to work inside the if statement. Thanks for all the help.

Work through your code with paper and pen, and you will see what is happening. This execersie will give you a a lot of insight into you program Always do the paper and pen thing until you start to get things right first time

The Ardiono may be slow but it will run your loop 500,000 time a second!

Mark

jksistheman:
When I add a delay to the LED to make it blink, it only turns on for 100 ms and then off, which I guess is due to the nature of delay. I've tried messing around with millis(), but I haven't been able to get it to work inside the if statement. Thanks for all the help.

I don't see a delay in your code; you need to show the problematic code.

Are you using external pull-up resistors? If not, your inputs are floating and reading them can return any value (HIGH or LOW) at any time.

  currentS1state = digitalRead(Switch1);
  lastS1state = digitalRead(Switch1);

  ...
  ...

  if (currentS1state != lastS1state)
  {
...
...

Except for effects caused by bouncing, chances are slim that currentS1state and lastS1state will differ; same for the second switch.

   currentS1state = digitalRead(Switch1);
   lastS1state = digitalRead(Switch1);

What are the chances that the values in currentS1state and lastS1state will be different?

Look at the state change detection example (again). That is NOT how to detect a change.