Components and supplies
2
Push Button
1
Resistor 100 Ohm ¼ w
3
10k Resistors
1
piezo speaker
1
Panel Mount 1K potentiometer (Breadboard Friendly) - 1K Linear
1
160 Ohm resistor
1
LCD 16x2 Backlit Display
22
ELEGOO Jumper Wires
1
Arduino Uno Rev3
Apps and platforms
1
Tinkercad
Project description
Code
Desk Buddy Code
cpp
1#include <LiquidCrystal.h> 2 3 4const int BUTTON = 7; 5const int PR = A5; 6const int PIEZO = 2; 7const int GAME = 3; 8 9const int RS = 8; 10const int E = 9; 11const int DB4 = 10; 12const int DB5 = 11; 13const int DB6 = 12; 14const int DB7 = 13; 15 16 17int prValue; 18int buttonState; 19int gameState; 20int buttonCount = 0; 21 22int laughNotes[] = { 23 440, 523, 587, 659, 587, 523 24}; 25int laughDurations[] = { 26 100, 100, 100, 100, 100, 200 27}; 28int laughLength = 6; 29 30int annoyedNotes[] = { 31 600, 520, 450, 400 32}; 33 34int annoyedDurations[] = { 35 180, 200, 230, 320 36}; 37 38int annoyedLength = 4; 39 40int sleepNotes[] = { 41 523, 494, 440, 392, 330 42}; 43 44int sleepDurations[] = { 45 200, 220, 250, 300, 500 46}; 47 48int sleepLength = 5; 49 50int singNotes[] = { 659, 784, 523, 587 }; 51int singDurations[] = { 150, 150, 150, 300 }; 52int singLength = 4; 53 54int sleepSingNotes[] = { 55 440, 494, 523, 494, 56 440, 392, 440, 392 57}; 58 59int sleepSingDurations[] = { 60 180, 180, 220, 180, 61 180, 200, 180, 220 62}; 63 64int sleepSingLength = 8; 65 66 67int awakeNotes[] = { 68 523, 659, 784, 988, 784, 659 69}; 70 71int awakeDurations[] = { 72 120, 120, 150, 170, 150, 200 73}; 74 75int awakeLength = 6; 76 77int startGameNotes[] = { 78 659, 784, 988, 1319 79}; 80 81int startGameDurations[] = { 82 120, 120, 150, 200 83}; 84 85int startGameLength = 4; 86 87int obsPos; 88bool jumping; 89unsigned long jumpTimer = 0; 90unsigned long jumpDuration = 500; 91int score; 92 93LiquidCrystal lcd(RS, E, DB4, DB5, DB6, DB7); 94 95void setup() 96{ 97 Serial.begin(9600); 98 pinMode(BUTTON, INPUT); 99 pinMode(GAME, INPUT); 100 pinMode(PR, INPUT); 101 pinMode(PIEZO, OUTPUT); 102 lcd.begin(16, 2); 103} 104 105void loop() 106{ 107 prValue = analogRead(PR); 108 if(prValue < 932){ 109 while(prValue < 932){ 110 sleep(); 111 } 112 } 113 else{ 114 playMelody(awakeNotes, awakeDurations, awakeLength); 115 lcd.setCursor(6,0); 116 lcd.print("- -"); 117 lcd.setCursor(7, 1); 118 lcd.print("O "); 119 delay(1000); 120 lcd.setCursor(6,0); 121 lcd.print("^ ^ "); 122 lcd.setCursor(7, 1); 123 lcd.print("-"); 124 delay(1000); 125 lcd.setCursor(6,0); 126 lcd.print("^ ^"); 127 lcd.setCursor(6, 1); 128 lcd.print("\\_/"); 129 delay(1000); 130 lcd.setCursor(6,0); 131 lcd.print("^ ^"); 132 lcd.setCursor(6, 1); 133 lcd.print("\\_/"); 134 while (prValue > 932){ 135 awake(); 136 } 137 playMelody(sleepNotes, sleepDurations, sleepLength); 138 lcd.setCursor(6,0); 139 lcd.print("- -"); 140 lcd.setCursor(6, 1); 141 lcd.print("\\_/ "); 142 delay(1000); 143 lcd.setCursor(6,0); 144 lcd.print("- - "); 145 lcd.setCursor(6, 1); 146 lcd.print(" O "); 147 delay(1000); 148 lcd.setCursor(6,0); 149 lcd.print("- -"); 150 lcd.setCursor(6, 1); 151 lcd.print(" o "); 152 delay(1000); 153 } 154 155} 156 157void awake() { 158 buttonState = digitalRead(BUTTON); 159 while (buttonState == 1){ 160 lcd.setCursor(6, 0); 161 lcd.print("> < HAHA!"); 162 lcd.setCursor(6, 1); 163 lcd.print("\\_/"); 164 playMelody(laughNotes, laughDurations, laughLength); 165 lcd.setCursor(6,0); 166 lcd.print("^ ^ "); 167 lcd.setCursor(6, 1); 168 lcd.print("\\_/"); 169 buttonState = digitalRead(BUTTON); 170 buttonCount++; 171 if(buttonCount > 3){ 172 while(buttonState == 1){ 173 lcd.setCursor(6, 0); 174 lcd.print("> < STOP! "); 175 lcd.setCursor(6, 1); 176 lcd.print(" ^ "); 177 playMelody(annoyedNotes, annoyedDurations, annoyedLength); 178 delay(1000); 179 buttonState = digitalRead(BUTTON); 180 } 181 lcd.setCursor(6,0); 182 lcd.print("^ ^ "); 183 lcd.setCursor(6, 1); 184 lcd.print("\\_/"); 185 } 186 187 188 } 189 gameState = digitalRead(GAME); 190 if (gameState == 1){ 191 runGame(); 192 } 193 buttonCount = 0; 194 unsigned int chance = random(1, 65535); 195 if (chance == 10){ 196 lcd.setCursor(6, 1); 197 lcd.print(" O LA LA"); 198 playMelody(singNotes, singDurations, singLength); 199 lcd.setCursor(6,0); 200 lcd.print("^ ^"); 201 lcd.setCursor(6, 1); 202 lcd.print("\\_/ "); 203 chance = 1; 204 } 205 prValue = analogRead(PR); 206 } 207void sleep() { 208 lcd.setCursor(6,0); 209 lcd.print("- -"); 210 lcd.setCursor(0, 1); 211 lcd.print(" "); 212 lcd.setCursor(7, 1); 213 lcd.print("- "); 214 delay(1000); 215 lcd.setCursor(6,0); 216 lcd.print("- -"); 217 lcd.setCursor(7, 1); 218 lcd.print("o zZ"); 219 lcd.setCursor(6,0); 220 lcd.print("- -"); 221 unsigned int chance = random(1, 65535); 222 if (chance == 10){ 223 playMelody(sleepSingNotes, sleepSingDurations, sleepSingLength); 224 } 225 delay(1000); 226 prValue = analogRead(PR); 227} 228 229 230 231void playMelody(int notes[], int durations[], int length) { 232 for (int i = 0; i < length; i++) { 233 tone(PIEZO, notes[i], durations[i]); 234 delay(durations[i] + 20); 235 } 236 noTone(PIEZO); 237} 238 239 240 241void runGame() { 242 lcd.clear(); 243 244 obsPos = 15; 245 jumping = false; 246 score = 0; 247 248 delay(500); 249 playMelody(startGameNotes, startGameDurations, startGameLength); 250 while (true) { 251 lcd.clear(); 252 if (digitalRead(GAME) == HIGH && !jumping) { 253 jumping = true; 254 jumpTimer = millis(); 255 } 256 257 if (jumping && millis() - jumpTimer > jumpDuration) { 258 jumping = false; 259 } 260 261 if (jumping) { 262 lcd.setCursor(2, 0); 263 lcd.print("O"); 264 } else { 265 lcd.setCursor(2, 1); 266 lcd.print("O"); 267 } 268 269 if (obsPos >= 0) { 270 lcd.setCursor(obsPos, 1); 271 lcd.print("/\\"); 272 } 273 274 if (!jumping && obsPos == 2) { 275 lcd.clear(); 276 lcd.setCursor(4, 0); 277 lcd.print("GAME OVER"); 278 lcd.setCursor(4, 1); 279 lcd.print("Score: "); 280 lcd.print(score); 281 playMelody(sleepNotes, sleepDurations, sleepLength); 282 delay(2500); 283 lcd.clear(); 284 lcd.setCursor(6,0); 285 lcd.print("^ ^ "); 286 lcd.setCursor(6, 1); 287 lcd.print("\\_/"); 288 return; 289 } 290 291 292 obsPos--; 293 294 295 if (obsPos < -1) { 296 obsPos = 15; 297 score++; 298 } 299 300 delay(120); 301 } 302}
Comments
Only logged in users can leave comments