int potPin = A0; // TODO: Replace this value with the pin number associated with the SIG pin on the potentiometer
int NUM_MODES = 3;
int MAX_POT_VAL = 1023; // TODO: Replace this value with the maximum value reading from the potentiometer
#include <LiquidCrystal_I2C.h>
#define I2C_ADDR 0x27
#define LCD_COLUMNS 20
#define LCD_LINES 4
LiquidCrystal_I2C lcd(I2C_ADDR, LCD_COLUMNS, LCD_LINES);
#include <Servo.h>
Servo myservo; // create servo object to control a servo
// twelve servo objects can be created on most boards
int pos = 0; // variable to store the servo position
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
for (int pinNumber = 2; pinNumber <= 13; pinNumber++) {
pinMode(pinNumber, OUTPUT);
}
lcd.init();
lcd.backlight();
// Print something
lcd.setCursor(3, 0);
//lcd.print("In Deadband!");
// you can now interact with the LCD, e.g.:
//lcd.print("In Deadband!");
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}
}
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();
lcd.print("In Deadband!");
for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(10); // waits 15ms for the servo to reach the position
}
for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(10); // waits 15ms for the servo to reach the position
}
}
else {
int modeNum;
if (analogRead(potPin) >= 0 && 341 >= analogRead(potPin)){ // range for mode 0
lcd.clear();
modeNum = 0;
if (modeNum == 0) {
turnOnLeds();
}
}
if (analogRead(potPin) >= 342 && 682 >= analogRead(potPin)){ // range for mode 1
lcd.clear();
modeNum = 1;
if (modeNum == 1) {
digitalWrite(12,HIGH);
digitalWrite(10,HIGH);
digitalWrite(8,HIGH);
digitalWrite(6,HIGH);
digitalWrite(4,HIGH);
digitalWrite(2,HIGH);
}
}
//TODO: Replace this with the correct mode number based on the potentiometer reading
if (analogRead(potPin) >= 683 && 1023 >= analogRead(potPin)){ // range for mode 2
lcd.clear();
modeNum = 2;
if (modeNum == 2) {
digitalWrite(12,HIGH);
digitalWrite(11,HIGH);
digitalWrite(8,HIGH);
digitalWrite(7,HIGH);
digitalWrite(4,HIGH);
digitalWrite(3,HIGH);
}
}
Serial.print("Analog Reading: ");
Serial.print(analogRead(potPin));
Serial.print(" is equal to Mode: ");
Serial.println(modeNum);
}
}
bool inDeadband() {
/*
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.
*/
if (analogRead(potPin) >= 311 && 341 >= analogRead(potPin)) {
return true;}
if (analogRead(potPin) >= 682 && 712 >= analogRead(potPin)) {
return true;}
return false;
}
void turnOffLeds() {
for (int ledNumber = 2; ledNumber <= 13; ledNumber++) {
digitalWrite(ledNumber, LOW);
}
}
void turnOnLeds() {
for (int ledNumber = 2; ledNumber <= 13; ledNumber++) {
digitalWrite(ledNumber, HIGH);
}
}
void blinkStatusLed() {
digitalWrite(13, HIGH);
delay(100);
digitalWrite(13, LOW);
delay(100);
}