Autoscoring Air Hockey Table
Convert an old, run-down air hockey table to a modern IOT project.
Devices & Components
1
Arduino® UNO R4 WiFi
1
wires
1
DIYables Limit Switch
1
Power Strip
1
iPad
Hardware & Tools
1
Tape, Electrical
1
Wire cutters/stripper
Software & Tools
1
Arduino Cloud
Project description
Code
Air Hockey Table Uno R4 Wifi Code
cpp
1#include "thingProperties.h" 2 3//Variables for switch states and debouncing 4int switchOneValue, switchTwoValue; 5unsigned long lastScore1Time = 0, lastScore2Time = 0; 6const unsigned long debounceDelay = 4000; //4 seconds between goals 7 8//Pin assignments 9const int playerOneSwitch = 2; 10const int playerTwoSwitch = 4; 11 12//Variable for Score Reset 13int runs = 0; 14unsigned long readyTimer = 0; 15const unsigned long readyDelay = 2000; 16 17void setup() { 18 //Serial Stuff 19 Serial.begin(9600); 20 delay(1500); 21 22 //Cloud Stuff 23 initProperties(); 24 ArduinoCloud.begin(ArduinoIoTPreferredConnection); 25 setDebugMessageLevel(2); 26 ArduinoCloud.printDebugInfo(); 27 while(!ArduinoCloud.connected()) { 28 ArduinoCloud.update(); 29 delay(500); 30 Serial.println("Waiting for cloud connection..."); 31 } 32 ArduinoCloud.update(); 33 Serial.println("Cloud connected!"); 34 delay(2000); 35 36 //Pin Assignments 37 pinMode(playerOneSwitch, INPUT_PULLUP); 38 pinMode(playerTwoSwitch, INPUT_PULLUP); 39 40 //Set Up Play Till 41 playTill = 10; 42 43 //Alert Not Ready 44 readyToPlay = false; 45 46 //Don't Reset 47 resetGame = false; 48 49 //Nobody's Won Yet 50 playerOneWins = false; 51 playerTwoWins = false; 52} 53 54void loop() { 55 //Standard Cloud Update 56 ArduinoCloud.update(); 57 58 //Read Switch Values 59 switchOneValue = digitalRead(playerOneSwitch); 60 switchTwoValue = digitalRead(playerTwoSwitch); 61 62 //For Debugging 63 unsigned long currentTime = millis(); 64 65 // Player 1 scoring with proper debouncing 66 if(switchOneValue == LOW && (currentTime - lastScore1Time) > debounceDelay) { 67 playerOneScore++; 68 lastScore1Time = currentTime; 69 Serial.println("Player 1 GOAL!"); 70 readyToPlay = false; 71 readyTimer = currentTime; 72 } 73 74 // Player 2 scoring with proper debouncing 75 if(switchTwoValue == LOW && (currentTime - lastScore2Time) > debounceDelay) { 76 playerTwoScore++; 77 lastScore2Time = currentTime; 78 Serial.println("Player 2 GOAL!"); 79 readyToPlay = false; 80 readyTimer = currentTime; 81 } 82 83 //Checker for resetting score because wasn't working in setup function (quick fix) 84 if(runs < 75) { 85runs++; 86 } else if (runs == 75) { 87 playerOneScore = 0; 88 playerTwoScore = 0; 89 prepareIpad = true; 90 ArduinoCloud.update(); 91 delay(1000); 92 prepareIpad = false; 93 readyToPlay = true; 94 runs++; 95 } 96 97 if(playerOneScore >= playTill) { 98 playerOneWins = true; 99 ArduinoCloud.update(); 100 delay(3000); 101 playerOneWins = false; 102 resetGame = true; 103 } 104 if(playerTwoScore >= playTill) { 105 playerTwoWins = true; 106 ArduinoCloud.update(); 107 delay(3000); 108 playerTwoWins = false; 109 resetGame = true; 110 } 111 112 if(resetGame == true) { 113 newGame(); 114 } 115 116 if(!readyToPlay && (currentTime - readyTimer) > readyDelay) { 117 readyToPlay = true; 118 } 119} 120 121void newGame() { 122 readyToPlay = false; 123 playerOneScore = 0; 124 playerTwoScore = 0; 125 resetGame = false; 126 readyToPlay = true; 127} 128 129void onPlayerOneScoreChange() {} 130void onPlayerTwoScoreChange() {} 131void onReadyToPlayChange() {} 132void onResetChange() {} 133void onResetGameChange() {} 134void onPlayTillChange() {} 135/* 136 Since PlayerOneWins is READ_WRITE variable, onPlayerOneWinsChange() is 137 executed every time a new value is received from IoT Cloud. 138*/ 139void onPlayerOneWinsChange() { 140 // Add your code here to act upon PlayerOneWins change 141} 142/* 143 Since PlayerTwoWins is READ_WRITE variable, onPlayerTwoWinsChange() is 144 executed every time a new value is received from IoT Cloud. 145*/ 146void onPlayerTwoWinsChange() { 147 // Add your code here to act upon PlayerTwoWins change 148} 149/* 150 Since PrepareIpad is READ_WRITE variable, onPrepareIpadChange() is 151 executed every time a new value is received from IoT Cloud. 152*/ 153void onPrepareIpadChange() { 154 // Add your code here to act upon PrepareIpad change 155}
Downloadable files
Air Hockey Goal STL File
Air Hockey Goal.stl
Comments
Only logged in users can leave comments