/* * Demo program for ATtiny45 Hello Board * Program reads pushbutton switch, sends * appropriate message through FTDI and * controls LEDs (ON/OFF/SLOW Blink/Fast Blink * * Uses software serial for TXD, RXD * Connections: * LED - PB4 (4) * PB - PB3 (3), Internal Pull-up resistor * TXD - PB2 (2), t45 rx * RXD - RB1 (1), t45 tx * * States: * 0 - LED off * 1 - LED on * 2 - SLOW Blink * 3 - FAST Blink * * Steven Chew 2020-03-10 * * This work may be reproduced, modified, distributed, * performed, and displayed for any purpose, but must * acknowledge this project. Copyright is retained and * must be preserved. The work is provided as is; no * warranty is provided, and users accept all liability. */ #include #define LED_pin 4 #define PB_pin 3 #define TX 1 #define RX 2 #define END_STATE 4 #define SLOW_DLY 500 #define FAST_DLY 150 SoftwareSerial mySerial(RX, TX); // ATtiny45 rx, tx uint8_t state = 0; // initial state LED off uint8_t ledStatus = 0; // tracks led status, 0 - off, 1 - on uint16_t dlyInterval; // blink interval void setup() { // put your setup code here, to run once: pinMode(LED_pin, OUTPUT); pinMode(PB_pin, INPUT_PULLUP); digitalWrite(LED_pin, LOW); // led off at start mySerial.begin(9600); // transmit at 9600 bps mySerial.println("Start state: LED off"); } void loop() { // put your main code here, to run repeatedly: // check for pushbutton press if (digitalRead(PB_pin) == LOW){ // pb pressed delay(10); // switch debounce while (digitalRead(PB_pin) == LOW); // wait for pb release state++; // transition to next state if (state == END_STATE) // check of last state state = 0; // reset to start state switch (state){ case 0: mySerial.println("State 0: LED off"); break; case 1: mySerial.println("State 1: LED on"); break; case 2: mySerial.println("State 2: SLOW blink"); break; case 3: mySerial.println("State 3: FASET blink"); break; default: mySerial.println("Error! Unknown stte"); } } // end if switch (state){ case 0: // LED off // mySerial.println("State 0: LED off"); ledStatus = 0; digitalWrite(LED_pin, LOW); break; case 1: // LED on // mySerial.println("State 1: LED on"); ledStatus = 1; digitalWrite(LED_pin, HIGH); break; case 2: // slow blink case 3: // fast blink if (state == 2){ // mySerial.println("State 2: Slow blink"); dlyInterval = SLOW_DLY; } else { // mySerial.println("State 3: Fast blink"); dlyInterval = FAST_DLY; } if (ledStatus == 0){ digitalWrite(LED_pin, LOW); // turn off LED ledStatus = 1; } else { digitalWrite(LED_pin, HIGH); // turn on LED ledStatus = 0; } delay(dlyInterval); break; } // end switch-case }