#include <LiquidCrystal_I2C.h>
#define I2C_ADDR 0x27
#define LCD_COLUMNS 20
#define LCD_LINES 1
LiquidCrystal_I2C lcd(I2C_ADDR, LCD_COLUMNS, LCD_LINES);
int potPin = A0;
const int SPEAKER_PIN = A3;
int NUM_MODES = 3;
int MAX_POT_VAL = 1023;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
for (int pinNumber = 2; pinNumber <= 13; pinNumber++) {
pinMode(pinNumber, OUTPUT);
}
}
void loop() {
// put your main code here, to run repeatedly:
turnOffLeds();
if (inDeadband()) {
Serial.print("Analog Reading: ");
Serial.print(analogRead(potPin));
Serial.println(" is in deadband");
blinkStatusLed();
} else {
int modeNum = 0;
if(analogRead(potPin)>0 && analogRead(potPin) <=341) {
modeNum=0;
}
if(analogRead(potPin)>342 && analogRead(potPin) <=682) {
modeNum=1;
}
if(analogRead(potPin)>683 && analogRead(potPin) <=1023) {
modeNum=2;
}
Serial.print("Analog Reading: ");
Serial.print(analogRead(potPin));
Serial.print(" is equal to Mode: ");
Serial.println(modeNum);
if (modeNum == 0) {
turnOnLeds0();
}
if (modeNum == 1) {
turnOnLeds1();
}
if (modeNum == 2) {
turnOnLeds2();
}
if (modeNum == 0, 1, 2){
lcd.clear();
}
}
}
bool inDeadband() {
if(analogRead(potPin)>326 && analogRead(potPin) <=356 || analogRead(potPin) >667 && analogRead(potPin) <= 697) {
return true;
}
/*
In this function you will determine if your potentiometer reading is within a deadband range. You will
be using analogRead as well as if statements.
You should return true if you ARE in deadband and false otherwise.
*/
return false;
}
void turnOffLeds() {
for (int ledNumber = 2; ledNumber <= 13; ledNumber++) {
digitalWrite(ledNumber, LOW);
}
}
void turnOnLeds0() {
for (int ledNumber = 2; ledNumber <= 7; ledNumber++) {
digitalWrite(ledNumber, HIGH);
}
}
void turnOnLeds1() {
for (int ledNumber = 8; ledNumber <= 13; ledNumber++) {
digitalWrite(ledNumber, HIGH);
}
}
void turnOnLeds2() {
for (int ledNumber = 2; ledNumber <= 13; ledNumber++) {
digitalWrite(ledNumber, HIGH);
}
}
void blinkStatusLed() {
digitalWrite(13, HIGH);
delay(100);
digitalWrite(13, LOW);
delay(100);
tone(SPEAKER_PIN, 262, 250);
lcd.init();
lcd.backlight();
// Print something
lcd.setCursor(3, 0);
lcd.print("Deadband!");
}