Skip to content

Instantly share code, notes, and snippets.

@iley
Last active October 23, 2019 14:38
Show Gist options
  • Save iley/9dab669023c205da6085ba454b51eb4b to your computer and use it in GitHub Desktop.
Save iley/9dab669023c205da6085ba454b51eb4b to your computer and use it in GitHub Desktop.
#include <LiquidCrystal.h>
#include <Wire.h>
const int speakerPin = 9;
const int lampPin = 8;
const int controlButtons = 3;
const int controlButtonPins[] = { 12, 13, 11 };
const int playerButtons = 6;
const int playerButtonPins[] = { 5, 4, 3, 2, 1, 0 };
LiquidCrystal lcd(7, 6, A0, A1, A2, A3);
void setup() {
Wire.begin();
pinMode(speakerPin, OUTPUT);
lcd.begin(20, 4);
lcd.write("Hello world!");
pinMode(lampPin, OUTPUT);
for (int i = 0; i < playerButtons; i++) {
pinMode(playerButtonPins[i], INPUT_PULLUP);
}
for (int i = 0; i < controlButtons; i++) {
pinMode(controlButtonPins[i], INPUT_PULLUP);
}
}
void flash() {
tone(speakerPin, 2000);
digitalWrite(lampPin, HIGH);
ExpanderWrite(0x20, 0xff);
delay(1000);
noTone(speakerPin);
digitalWrite(lampPin, LOW);
ExpanderWrite(0x20, 0x00);
}
void loop() {
while (true) {
for (int i = 0; i < playerButtons; i++) {
if (digitalRead(playerButtonPins[i]) == LOW) {
lcd.setCursor(0, 1);
lcd.write("PLR ");
lcd.write('0' + i);
flash();
}
}
for (int i = 0; i < controlButtons; i++) {
if (digitalRead(controlButtonPins[i]) == LOW) {
lcd.setCursor(0, 1);
lcd.write("CTL ");
lcd.write('0' + i);
flash();
}
}
}
}
void ExpanderWrite(byte address, byte value) {
Wire.beginTransmission(address);
Wire.write(value);
Wire.endTransmission();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment