Skip to content

Commit

Permalink
Merge pull request #4 from makegosport/six_switch
Browse files Browse the repository at this point in the history
6 GPIO version
  • Loading branch information
krcb197 authored Apr 12, 2022
2 parents 6a4b829 + 56b9220 commit 895384e
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 25 deletions.
68 changes: 45 additions & 23 deletions cornhole_switch/cornhole_switch.ino
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ Ticker ticker;

int LED_BUILTIN = 2; // LED on the board connected to GPIO2
int BTN_BUILTIN = 3; // BTN on GPIO3
int SWITCHS[6] = {4, 5, 6, 7, 8, 10}; // GPIO SWITCH CHNs

Adafruit_NeoPixel pixel = Adafruit_NeoPixel(1, LED_BUILTIN, ORDER); // one pixel, on pin referenced by LED_BUILTIN

// flashes this colour when connecting to wifi:
Expand All @@ -38,7 +40,7 @@ static uint32_t current_LED = current_colour;


// broker connection information
char broker[] = "192.168.1.120";
char broker[] = "192.168.1.142";
int port = 1884;
char clientID[128];

Expand All @@ -47,12 +49,19 @@ char mac_string[20]; // mac address
enum e_hole_state {OFF = 0, HOLE_RED=1, HOLE_BLUE=2};
e_hole_state hole_state[6] = { OFF, OFF, OFF, OFF, OFF, OFF };

int current_button_state = 0;

void setup() {
// put your setup code here, to run once:

pinMode (LED_BUILTIN, OUTPUT);
Serial.begin(115200);
pinMode (BTN_BUILTIN, INPUT_PULLUP);

for(int hole_id=1; hole_id<=6; hole_id++)
{
pinMode (SWITCHS[hole_id-1], INPUT_PULLUP);
}

WiFi.mode(WIFI_STA); // explicitly set mode, esp defaults to STA+AP
// it is a good practice to make sure your code sets wifi mode how you want it.
Expand Down Expand Up @@ -88,6 +97,13 @@ void setup() {
set_colour(wifi_colour);
ticker.attach(1, tick);

// Uncomment and run it once, if you want to erase all the stored information
current_button_state = digitalRead(BTN_BUILTIN);
if (current_button_state == 0) {
Serial.println("BTN pressed during initialisation, clearing the wifi details");
wifiManager.resetSettings();
}

// call us back when it's connected so we can reset the pixel
wifiManager.setAPCallback(configModeCallback);

Expand Down Expand Up @@ -320,9 +336,9 @@ unsigned long time_last_heartbeat = millis();
unsigned long time_now;
char heatbeat_topic_name[20]="switch/heartbeat";
char heatbeat_payload[60];
unsigned long next_btn_time = millis();
int current_button_state = 0;
int previous_button_state = 0;
unsigned long next_switch_time[6] = {millis(), millis(), millis(), millis(), millis(), millis()};
int current_switch_state = 0;
int previous_switch_state[6] = {0, 0, 0, 0, 0, 0};
int hold_off_button = 0;
char btn_press_topic_name[20]="switch/1";
char btn_press_payload[60];
Expand All @@ -347,28 +363,34 @@ void loop() {
}

// check for changes in the button state
if (time_now > next_btn_time) {
current_button_state = digitalRead(BTN_BUILTIN);
if ((previous_button_state == 1) & (current_button_state == 0)) // button is pulled up and grounded when pressed
{
// button press detected
if (hole_state[0] == HOLE_RED) {
sprintf(btn_press_payload,"{\"time\":%u%, \"colour\": \"red\"}" , time_now);
Serial.println("Heatbeat sent");
}
else if (hole_state[0] == HOLE_BLUE) {
sprintf(btn_press_payload,"{\"time\":%u%, \"colour\": \"blue\"}" , time_now);
for(int hole_id=1; hole_id<=6; hole_id++)
{
if (time_now > next_switch_time[hole_id-1]) {
current_switch_state = digitalRead(SWITCHS[hole_id-1]);
if ((previous_switch_state[hole_id-1] == 0) & (current_switch_state == 1)) // button is pulled up and grounded when pressed
{
// button press detected
Serial.print("button press");
Serial.println(hole_id);

if (hole_state[hole_id-1] == HOLE_RED) {
sprintf(btn_press_payload,"{\"time\":%u%, \"colour\": \"red\"}" , time_now);
}
else if (hole_state[hole_id-1] == HOLE_BLUE) {
sprintf(btn_press_payload,"{\"time\":%u%, \"colour\": \"blue\"}" , time_now);
}
else {
sprintf(btn_press_payload,"{\"time\":%u%, \"colour\": \"off\"}" , time_now);
}
sprintf(btn_press_topic_name, "switch/%d", hole_id);
client.publish(btn_press_topic_name, btn_press_payload);
next_switch_time[hole_id-1] = time_now + 3000; // 3s hold off before next time
previous_switch_state[hole_id-1] = current_switch_state;
}
else {
sprintf(btn_press_payload,"{\"time\":%u%, \"colour\": \"off\"}" , time_now);
next_switch_time[hole_id-1] = time_now + 100; // 100ms next time round the loop
previous_switch_state[hole_id-1] = current_switch_state;
}
client.publish(btn_press_topic_name, btn_press_payload);
next_btn_time = time_now + 3000; // 3s hold off before next time
previous_button_state = current_button_state;
}
else {
next_btn_time = time_now + 100; // 100ms next time round the loop
previous_button_state = current_button_state;
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions game_sim/game_sim.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def on_message(self, client, userdata, message):

message_payload = message.payload.decode('utf-8')

print(message_payload)
print(f'{hole_id:d} : {message_payload}')

return None

Expand Down Expand Up @@ -114,7 +114,7 @@ def on_message(self, client, userdata, message):
parser = argparse.ArgumentParser(description='Python Code to generate a simulate the Cornhole game MQTT messages',
epilog='See: https://github.com/krcb197/CheerLightTwitterAPI '
'for more details')
parser.add_argument('--mqtt_server', '-a', dest='mqtt_server', type=str, default='192.168.1.120',
parser.add_argument('--mqtt_server', '-a', dest='mqtt_server', type=str, default='192.168.1.142',
help='address for the MQTT server')
parser.add_argument('--mqtt_port', '-p', dest='mqtt_port', type=int, default=1884,
help='port for the MQTT server')
Expand Down

0 comments on commit 895384e

Please sign in to comment.