Skip to content

Instantly share code, notes, and snippets.

@ishotjr
Last active April 8, 2021 01:05
Show Gist options
  • Save ishotjr/15b64e22467246c5372cf452ccba18e8 to your computer and use it in GitHub Desktop.
Save ishotjr/15b64e22467246c5372cf452ccba18e8 to your computer and use it in GitHub Desktop.
All Hands Active! ATMegaZero Workshop with ishotjr Learning Shield Demo
/*
* ATMegaZero Learning Shield Demo
* for the All Hands Active! ATMegaZero Workshop with ishotjr
* https://www.meetup.com/AllHandsActive/events/276805820/
*
* You can purchase the ATMegaZero Learning Shield from the ATMegaZero Online Store at:
* https://shop.atmegazero.com/products/atmegazero-learning-shield
*
* For full documentation please visit https://atmegazero.com
*/
// Learning Shield pins
const int BUZZER_PIN = 6;
const int GREEN_LED_PIN = A2;
const int YELLOW_LED_PIN = A1;
const int RED_LED_PIN = A0;
const int BUTTON_PIN = 7;
// button state variables
int lastState = HIGH;
int currentState;
// this function runs once when the board powers on
void setup() {
// set up pin modes
pinMode(BUZZER_PIN, OUTPUT);
pinMode(BUTTON_PIN, INPUT_PULLUP);
pinMode(GREEN_LED_PIN, OUTPUT);
pinMode(YELLOW_LED_PIN, OUTPUT);
pinMode(RED_LED_PIN, OUTPUT);
// turn all LEDs ON
digitalWrite(GREEN_LED_PIN, HIGH);
digitalWrite(YELLOW_LED_PIN, HIGH);
digitalWrite(RED_LED_PIN, HIGH);
// quick buzzer sweep
for (int note = 440; note <= 880; note += 20) {
// you can either use the third parameter or a delay to persist the tone
tone(BUZZER_PIN, note); //, 1000 / 10);
delay(100);
}
// silence please
noTone(BUZZER_PIN);
}
// after setup, this function runs over and over as long as the board has power
void loop() {
// read button state (high = pressed)
int currentState = digitalRead(BUTTON_PIN);
// if the button was not pressed, but is now
// *no debounce
if (lastState == LOW && currentState == HIGH) {
blink_em();
}
// save the current state for next time
lastState = currentState;
}
// this is a user-defined function that runs on button press
void blink_em() {
// turn all LEDs OFF, buzzer ON!
// 440 is an easy-to-remember Hz value corresponding to A above middle C
// 220 is A one octave down; 880 is A one octave up
tone(BUZZER_PIN, 220);
digitalWrite(GREEN_LED_PIN, LOW);
digitalWrite(YELLOW_LED_PIN, LOW);
digitalWrite(RED_LED_PIN, LOW);
// wait 1 second (total)
delay(900);
noTone(BUZZER_PIN);
delay(100);
// turn just red LED ON, buzzer ON!
tone(BUZZER_PIN, 220);
digitalWrite(GREEN_LED_PIN, LOW);
digitalWrite(YELLOW_LED_PIN, LOW);
digitalWrite(RED_LED_PIN, HIGH);
// wait 1 second (total)
delay(900);
noTone(BUZZER_PIN);
delay(100);
// turn yellow LED ON, buzzer ON!
tone(BUZZER_PIN, 220);
digitalWrite(YELLOW_LED_PIN, HIGH);
// wait 1 second (total)
delay(900);
noTone(BUZZER_PIN);
delay(100);
// turn green LED ON, others OFF, buzzer ON!
tone(BUZZER_PIN, 880, 1500); // note 3rd parameter vs. delay()
digitalWrite(GREEN_LED_PIN, HIGH);
digitalWrite(YELLOW_LED_PIN, LOW);
digitalWrite(RED_LED_PIN, LOW);
// note that we are repeating ourselves above with slightly different parameters;
// we could be more "DRY" and refactor to abstract these repetitions into their
// own function and just call it with the differing parameters
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment