/*
Forum: https://forum.arduino.cc/t/arduino-sketch-not-working-and-i-dont-know-why/1186468
Wokwi: https://wokwi.com/projects/381202038944483329
*/
#include<Arduino.h>
#include<Servo.h>
Servo servo;
constexpr int openAngle = 45;
constexpr int closedAngle = 90;
constexpr byte servoPin = 11;
// Analog Output
constexpr byte maxPWM = 255; // PWM value
constexpr byte interiorFloorPin = 6; // Interior Floor
constexpr byte interiorTopPin = 9; // Interior Top
// Digital Output
constexpr byte frontBottomPin = 4; // Front Bottom
constexpr byte leftBlinkerPin = 7; // Left Blinker
constexpr byte rightBlinkerPin = 8; // Right Blinker
constexpr byte frontTopLeftPin = 3; // Front Top Left
constexpr byte frontTopRightPin = 0; // Front Top Right
constexpr byte rearLeftPin = 5; // Rear Left
constexpr byte rearRightPin = 1; // Rear Right
constexpr byte interiorButtonPin = A3; // Interior Button
constexpr byte leftBlinkerButtonPin = A4; // Left Blinker Button
constexpr byte rightBlinkerButtonPin = A6; // Right Blinker Button
constexpr byte frontTopButtonPin = A0; // Front Top Button
constexpr byte frontBottomButtonPin = A1; // Front Bottom Button
constexpr byte rearButtonPin = A2; // Rear Button
struct lightType {
byte switchPin;
byte outPin;
boolean state;
boolean isOn;
boolean shallBlink;
boolean isAnalogIn;
boolean isAnalogOut;
void init(byte switchP, byte outP);
void off();
void on();
void toggle();
};
void lightType::init(byte switchP, byte outP) {
switchPin = switchP;
switch (switchPin) {
case A6:
case A7:
isAnalogIn = true;
break;
default:
isAnalogIn = false;
pinMode(switchPin, INPUT_PULLUP);
break;
}
outPin = outP;
pinMode(outPin, OUTPUT);
switch (outPin) {
// Uno, Nano, Mini PWM Pins
//case 3:
//case 5:
case 6:
case 9:
//case 10:
//case 11:
isAnalogOut = true;
break;
default:
isAnalogOut = false;
break;
}
off();
state = HIGH;
shallBlink = false;
};
void lightType::toggle() {
if (isOn) {
off();
} else {
on();
}
}
void lightType::off() {
if (isAnalogOut) {
analogWrite(outPin, 0);
} else {
digitalWrite(outPin, LOW);
}
isOn = false;
}
void lightType::on() {
if (isAnalogOut) {
analogWrite(outPin, maxPWM);
} else {
digitalWrite(outPin, HIGH);
}
isOn = true;
}
boolean buttonPressed(lightType &l) {
boolean actState;
if (l.isAnalogIn) {
int val = analogRead(l.switchPin);
actState = (val < 512);
} else {
actState = digitalRead(l.switchPin);
}
if (actState != l.state) {
l.state = actState;
delay(5); // a little bit of debouncing
return actState;
}
return false;
};
void handleBlinker(lightType &On, lightType &Off) {
if (buttonPressed(On)) {
Off.shallBlink = false;
Off.off();
On.off();
On.shallBlink = !On.shallBlink;
}
}
void Blinking(lightType &l, lightType &r) {
static unsigned long lastBlink = 0;
if (millis() - lastBlink > 300) {
lastBlink = millis();
if (r.shallBlink) {
r.toggle();
}
if (l.shallBlink) {
l.toggle();
}
}
}
lightType interiorFloor;
lightType interiorTop;
lightType frontTopLeft;
lightType frontTopRight;
lightType frontBottom;
lightType leftBlinker;
lightType rightBlinker;
lightType rearLeft;
lightType rearRight;
//*************************************************** void Setup ************************************************
void setup() {
interiorFloor.init(interiorButtonPin, interiorFloorPin);
interiorTop.init( interiorButtonPin, interiorTopPin);
frontTopLeft.init( frontTopButtonPin, frontTopLeftPin);
frontTopRight.init(frontTopButtonPin, frontTopRightPin);
frontBottom.init(frontBottomButtonPin, frontBottomPin);
leftBlinker.init( leftBlinkerButtonPin, leftBlinkerPin);
rightBlinker.init(rightBlinkerButtonPin, rightBlinkerPin);
rearLeft.init( rearButtonPin, rearLeftPin);
rearRight.init( rearButtonPin, rearRightPin);
servo.attach(servoPin);
servo.write(closedAngle);
}
void loop() {
if (buttonPressed(interiorFloor)) {
interiorFloor.toggle();
interiorTop.toggle();
}
if (buttonPressed(frontTopLeft)) {
frontTopLeft.toggle();
frontTopRight.toggle();
if (frontTopLeft.isOn) {
servo.write(openAngle);
} else {
servo.write(closedAngle);
}
}
if (buttonPressed(frontBottom)) {
frontBottom.toggle();
}
if (buttonPressed(rearRight)) {
rearRight.toggle();
rearLeft.toggle();
}
handleBlinker(rightBlinker, leftBlinker);
handleBlinker(leftBlinker, rightBlinker);
Blinking(leftBlinker, rightBlinker);
}
Interior
Blink Left
Blink Right
Front Top
Front Bottom
Rear