Go Down

Topic: blink a led with push button (Read 11062 times) previous topic - next topic

jameskirk

Oh Yeah...
That was it...
Here is the code for this step...
Code: [Select]
const byte ledPins[] = {10, 11, 12, 13};
const byte soundPins[] = {02, 03, 04, 05};
const byte buttonPins[] = {A1, A2, A3, A4};
const byte NUMBER_OF_LEDS = sizeof(ledPins) / sizeof(ledPins[0]);
boolean blinkStates[] = {false, false, false, false};
byte previousButtonStates[NUMBER_OF_LEDS];
unsigned long startTimes[4];
unsigned long periods[] = {300, 500, 700, 150};
byte ledStates[] = {HIGH, HIGH, HIGH, HIGH};

void setup()
{
  Serial.begin(115200);
  for (int p = 0; p < NUMBER_OF_LEDS; p++)
  {
    pinMode(ledPins[p], OUTPUT);
    pinMode(soundPins[p], OUTPUT);
    pinMode(buttonPins[p], INPUT_PULLUP);
  }
}

void loop()
{
  for (int button = 0; button < NUMBER_OF_LEDS; button++)
  {
    byte buttonState = digitalRead(buttonPins[button]);
    if (buttonState != previousButtonStates[button] && buttonState == LOW)
    {
      blinkStates[button] = !blinkStates[button];
      if (blinkStates[button] == false)
      {
        digitalWrite(ledPins[button], LOW);
        digitalWrite(soundPins[button], LOW);
        }
        else
    {
        digitalWrite(soundPins[button], HIGH);
      }
    }
    previousButtonStates[button] = buttonState;
  }

  for (byte led = 0; led < NUMBER_OF_LEDS; led++)
  {
    if (blinkStates[led])
    {
      unsigned long currentTime = millis();
      if (currentTime - startTimes[led] >= periods[led])
      {
        ledStates[led] = !ledStates[led];
        digitalWrite(ledPins[led], ledStates[led]);
        startTimes[led] = currentTime;
      }
    }
  }
}


Here is a Video
Thanks
Now on to the next step...
Capsense Touch Switch

Go Up