Skip to content

Instantly share code, notes, and snippets.

@SelvinPL
Last active December 7, 2024 00:23
Show Gist options
  • Select an option

  • Save SelvinPL/f3898b8d20abe1efb7962a7b847c57b8 to your computer and use it in GitHub Desktop.

Select an option

Save SelvinPL/f3898b8d20abe1efb7962a7b847c57b8 to your computer and use it in GitHub Desktop.
#include <avr/power.h>
#include <avr/sleep.h>
#define CLOCK_PIN SCL
#define OUTPUT_PIN MISO
#define BUTTON_PIN SDA
#define POT_PIN A0
#define TIMEOUT 4800
#define IS_STARTUP (counter == 255)
uint8_t counter = 255;
uint8_t data[16];
bool buttonClicked = false;
inline void disableTimeout()
{
TIMSK1 = 0;
OCR1A = 0;
TCCR1A = 0;
TCCR1B = 0;
}
inline void startTimout(int value)
{
TCCR1A = 0;
TCCR1B = _BV(CS10);
OCR1A = value;
TIMSK1 |= _BV(OCIE1A);
}
void clockFalling() {
disableTimeout();
if(!IS_STARTUP)
{
digitalWrite(OUTPUT_PIN, !data[counter]);
counter++;
}
attachInterrupt(digitalPinToInterrupt(CLOCK_PIN), clockRising, RISING);
}
void clockRising() {
startTimout(TIMEOUT);
attachInterrupt(digitalPinToInterrupt(CLOCK_PIN), clockFalling, FALLING);
}
void buttonPressed() {
buttonClicked = !digitalRead(BUTTON_PIN);
}
ISR(TIMER1_COMPA_vect) {
disableTimeout();
digitalWrite(OUTPUT_PIN, HIGH);
if(IS_STARTUP) {
attachInterrupt(digitalPinToInterrupt(BUTTON_PIN), buttonPressed, CHANGE);
buttonClicked = false;
for(uint8_t i = 0; i < 16; i++) {
data[i] = 0;
}
}
counter = 0;
uint8_t value = map(analogRead(POT_PIN), 0, 1023, 0, 256);
for(uint8_t i = 0; i < 8; i++) {
data[i] = value & B10000000;
value <<= 1;
data[i + 8] = buttonClicked;
}
}
void setup() {
pinMode(OUTPUT_PIN, OUTPUT);
digitalWrite(OUTPUT_PIN, HIGH);
pinMode(CLOCK_PIN, INPUT_PULLUP);
pinMode(BUTTON_PIN, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(CLOCK_PIN), clockFalling, FALLING);
//look up!!!!! this will semi brick you arduino
//https://forum.arduino.cc/t/killed-arduino-leonardo-with-power_all_disable/229460/15
power_all_disable();
power_timer1_enable();
power_adc_enable();
set_sleep_mode(SLEEP_MODE_IDLE);
}
void loop() {
sleep_mode();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment