Jump to:
Session 1 – Combo Sensor Project
Session 2 – Keypad-Controlled Monitoring System with Sensors
Session 3 – Keypad-Controlled Smart Lighting with Motion Sensor & Auto-Off
Session 4 – Sound Detector with Arduino
Session 5 – Arduino Call and Response Game
Session 6 – Arduino Game Console
Session 1 – Combo Sensor Project
Session 1:
Worksheet
Session 2 – Keypad-Controlled Monitoring System with Sensors
Session 2:
Worksheet
Code
Session 3 – Keypad-Controlled Smart Lighting with Motion Sensor & Auto-Off
Session 3:
Worksheet
Code
Session 4 – Sound Detector with Arduino
Session 4:
Worksheet
Session 5 – Arduino Call and Response
Session 5:
Worksheet
Session 6 – Arduino Game Console
Session 6:
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
const int micDigitalPin = 2;
const int micAnalogPin = A2;
const int greenButtonPin = 4;
const int redButtonPin = 6;
const int blueLedPin = 10;
const int greenLedPin = 8;
const int redLedPin = 7;
int score = 0;
bool gameRunning = false;
unsigned long gameStartTime = 0;
const unsigned long gameDuration = 60000;
const unsigned long eventInterval = 6000;
unsigned long lastEventTime = 0;
bool greenButtonPrevState = LOW;
bool redButtonPrevState = LOW;
bool blueLedActive = false;
void setup() {
Serial.begin(9600);
lcd.init();
lcd.backlight();
lcd.print("Game Loading...");
pinMode(micDigitalPin, INPUT);
pinMode(micAnalogPin, INPUT);
pinMode(greenButtonPin, INPUT);
pinMode(redButtonPin, INPUT);
pinMode(blueLedPin, OUTPUT);
pinMode(greenLedPin, OUTPUT);
pinMode(redLedPin, OUTPUT);
delay(3000);
gameStart();
}
void loop() {
if (gameRunning) {
if (millis() - gameStartTime >= gameDuration) {
gameOver();
}
checkButtonPress(greenButtonPin, greenLedPin, greenButtonPrevState);
checkButtonPress(redButtonPin, redLedPin, redButtonPrevState);
checkMicrophone();
if (millis() - lastEventTime >= eventInterval) {
lastEventTime = millis();
int event = random(0, 3);
if (event == 0) {
digitalWrite(blueLedPin, HIGH);
blueLedActive = true;
} else if (event == 1) {
digitalWrite(greenLedPin, HIGH);
} else {
digitalWrite(redLedPin, HIGH);
}
}
}
}
void checkButtonPress(int buttonPin, int ledPin, bool &prevState) {
bool currentState = digitalRead(buttonPin);
if (currentState == HIGH && prevState == LOW) { // Detect state change
score++;
lcd.clear();
lcd.print("Score: ");
lcd.print(score);
Serial.println("Button Pressed! Score: " + String(score));
digitalWrite(ledPin, LOW);
}
prevState = currentState;
}
void checkMicrophone() {
if (blueLedActive) {
int soundLevel = analogRead(micAnalogPin);
if (soundLevel > 200) { // Adjust threshold as needed
score++;
lcd.clear();
lcd.print("Score: ");
lcd.print(score);
Serial.println("Sound Detected! Score: " + String(score));
digitalWrite(blueLedPin, LOW);
blueLedActive = false;
}
}
}
void gameStart() {
gameRunning = true;
score = 0;
gameStartTime = millis();
lastEventTime = millis();
lcd.clear();
lcd.print("Score: ");
lcd.print(score);
delay(2000);
}
void gameOver() {
lcd.clear();
lcd.print("Game Over!");
lcd.setCursor(0, 1);
lcd.print("Final Score: ");
lcd.print(score);
gameRunning = false;
delay(5000);
}
Worksheet