// Fnu Shoaib Ahmed, 652420658, fahme8
// Lab 5 - Multiple Inputs and Outputs
// Description - In this lab we are trying to do two unconnected things at the same time, using multiple inputs and outputs and be able to read multiple analog inputs.
// Assumptions - I expected the potentiometer to work as increase and decrease the value of photoresister, and the passive buzzer to work correctly.
// References - I used the prelab links to setup my photoresistor on breadboard and the setup of potentiometer and the passive buzzer.
// Demoed to: Daniel
// Define pin connections
const int photoResistorPin = A0; // Photoresistor pin
const int potentiometerPin = A1; // Potentiometer pin
const int buzzerPin = 6; // Buzzer pin
const int ledPins[] = {2, 3, 4, 5}; // LED pins
const int numLEDs = 4; // Number of LEDs
// Variables to store max and min light levels
int maxLightLevel = 0;
int minLightLevel = 1023;
// Timing variables for non-blocking behavior
unsigned long previousMillisLEDs = 0;
unsigned long previousMillisBuzzer = 0;
// Interval for checking sensor data (10 ms)
const unsigned long sensorInterval = 10;
void setup() {
Serial.begin(9600); // Start serial communication
for (int i = 0; i < numLEDs; i++) {
pinMode(ledPins[i], OUTPUT); // Initialize LED pins as outputs
}
pinMode(buzzerPin, OUTPUT); // Initialize the buzzer pin as output
}
void loop() {
unsigned long currentMillis = millis(); // Get current time
// Check and control LEDs periodically
if (currentMillis - previousMillisLEDs >= sensorInterval) {
previousMillisLEDs = currentMillis;
controlLEDs(); // Control LEDs based on the photoresistor
}
// Control the buzzer based on potentiometer value
controlBuzzer();
}
void controlLEDs() {
int lightLevel = analogRead(photoResistorPin); // Read the current light level
// Track max and min light levels
if (lightLevel > maxLightLevel) {
maxLightLevel = lightLevel;
}
if (lightLevel < minLightLevel) {
minLightLevel = lightLevel;
}
// Print max and min light levels for debugging
Serial.print("Light Level: ");
Serial.print(lightLevel);
Serial.print(" | Max: ");
Serial.print(maxLightLevel);
Serial.print(" | Min: ");
Serial.println(minLightLevel);
// Calculate the range and thresholds for LED control
int range = maxLightLevel - minLightLevel;
int step = range / 5; // Divide the range by 5
// Turn off all LEDs initially
for (int i = 0; i < numLEDs; i++) {
digitalWrite(ledPins[i], LOW);
}
// Determine the number of LEDs to turn on based on lightLevel
if (lightLevel < minLightLevel + step) {
// Very dark - turn on all LEDs
for (int i = 0; i < numLEDs; i++) {
digitalWrite(ledPins[i], HIGH);
}
} else if (lightLevel < minLightLevel + 2 * step) {
// Dark - turn on 3 LEDs
digitalWrite(ledPins[0], HIGH);
digitalWrite(ledPins[1], HIGH);
digitalWrite(ledPins[2], HIGH);
} else if (lightLevel < minLightLevel + 3 * step) {
// Moderate light - turn on 2 LEDs
digitalWrite(ledPins[0], HIGH);
digitalWrite(ledPins[1], HIGH);
} else if (lightLevel < minLightLevel + 4 * step) {
// Bright - turn on 1 LED
digitalWrite(ledPins[0], HIGH);
} else {
// Very bright - turn on all LEDs
for (int i = 0; i < numLEDs; i++) {
digitalWrite(ledPins[i], LOW);
}
}
}
void controlBuzzer() {
int potValue = analogRead(potentiometerPin); // Read the potentiometer value
unsigned long currentMillis = millis();
// Calculate delay time based on potentiometer value
unsigned long delayTime;
if (potValue < 146) {
// a. Buzzer is turned off in the lowest range
noTone(buzzerPin); // Ensure the buzzer is off
return; // Exit the function, no need to check further
} else if (potValue < 292) {
// b. 8 seconds between tones
delayTime = 8000;
} else if (potValue < 438) {
// c. 4 seconds between tones
delayTime = 4000;
} else if (potValue < 584) {
// d. 2 seconds between tones
delayTime = 2000;
} else if (potValue < 730) {
// e. 1 second between tones
delayTime = 1000;
} else if (potValue < 876) {
// f. 1/2 second between tones
delayTime = 500;
} else {
// g. Continuous tone for highest range
delayTime = 0;
}
// Non-blocking tone control
if (delayTime > 0) {
// Play the tone after the delay time has passed
if (currentMillis - previousMillisBuzzer >= delayTime) {
previousMillisBuzzer = currentMillis;
tone(buzzerPin, 440, 100); // Play the buzzer tone for 100 ms
}
} else {
// Continuous tone for the highest range
tone(buzzerPin, 440); // Play continuous tone
}
}