All submissions

LED toggle using SPI

Solving Approach

How do you plan to solve it?

 

Code

#include <SPI.h>

#define SS_PIN 10
#define BUTTON_PIN 2

bool ledState = false;
const uint8_t DEBOUNCE_DELAY = 50;

unsigned long lastDebounceTime = 0;
uint8_t lastStableState = HIGH;
uint8_t currentState = HIGH;

void setup() {
  pinMode(SS_PIN, OUTPUT);
  pinMode(BUTTON_PIN, INPUT_PULLUP);
  
  SPI.begin();
  digitalWrite(SS_PIN, HIGH);  
}

void loop() {
  if (debouncedPress()) {
    ledState = !ledState;

    digitalWrite(SS_PIN, LOW);              
    SPI.transfer(ledState ? 0x01 : 0x00);   
    digitalWrite(SS_PIN, HIGH);             
  }
}

bool debouncedPress() {
  uint8_t reading = digitalRead(BUTTON_PIN);

  if (reading != lastStableState) {
    lastDebounceTime = millis();
    lastStableState = reading;
  }

  if ((millis() - lastDebounceTime) > DEBOUNCE_DELAY && reading != currentState) {
    currentState = reading;
    return (currentState == LOW);
  }

  return false;
}

 

Output

Video

Add a video of the output (know more)

 

 

 

 

 

Photo of Output

Add a photo of your hardware showing the output.

 

 


 

Submit Your Solution

Note: Once submitted, your solution goes public, helping others learn from your approach!