/*
Forum: https://forum.arduino.cc/t/arduino-sketch-not-working-and-i-dont-know-why/1186468
Wokwi: https://wokwi.com/projects/380930243656770561
*/
#include<Arduino.h>
#include<Servo.h>
Servo servo;
constexpr int openAngle = 45;
constexpr int closedAngle = 90;
constexpr byte servoPin = 11;
// Analog Output
constexpr byte interiorFloorPin = 6; // Interior Floor
constexpr byte interiorTopPin = 9; // Interior Top
constexpr byte frontBottomPin = 4; // Front Bottom
// Digital Output
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 = A5; // 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;
byte value;
byte maxValue;
boolean state;
boolean shallBlink;
unsigned long lastBlinkTime;
void init(byte switchP, byte outP, byte maxV);
void off();
void on();
void handle();
boolean isOn();
void doBlink();
};
void lightType::init(byte switchP, byte outP, byte maxV) {
switchPin = switchP;
outPin = outP;
maxValue = maxV;
pinMode(outPin, OUTPUT);
if (maxValue > 0) {
analogWrite(outPin, 0);
} else {
off();
}
lastBlinkTime = 0;
state = LOW;
};
void lightType::doBlink() {
if (shallBlink)
if (millis() - lastBlinkTime >= 500) {
lastBlinkTime = millis();
byte actState = digitalRead(outPin);
digitalWrite(outPin, !actState);
}
}
void lightType::handle() {
if (isOn()) {
off();
} else {
on();
}
}
void lightType::off() {
digitalWrite(outPin, LOW);
shallBlink = false;
}
void lightType::on() {
digitalWrite(outPin, HIGH);
shallBlink = false;
}
boolean lightType::isOn() {
return (value == maxValue);
}
boolean analogButtonPressed(lightType &l) {
boolean actState = (analogRead(l.switchPin) < 512);
if (actState != l.state) {
l.state = actState;
delay(5); // a little bit of debouncing
return actState;
}
return false;
};
lightType interiorFloor;
lightType interiorTop;
lightType frontTopLeft;
lightType frontTopRight;
lightType frontBottom;
lightType leftBlinker;
lightType rightBlinker;
//*************************************************** void Setup ************************************************
void setup() {
interiorFloor.init(interiorButtonPin, interiorFloorPin, 127);
interiorTop.init( interiorButtonPin, interiorTopPin, 127);
frontTopLeft.init( frontTopButtonPin, frontTopLeftPin, 127);
frontTopRight.init(frontTopButtonPin, frontTopRightPin, 0);
frontBottom.init(frontBottomButtonPin, frontBottomPin, 0);
leftBlinker.init( leftBlinkerButtonPin, leftBlinkerPin, 0);
rightBlinker.init(rightBlinkerButtonPin, rightBlinkerPin, 0);
servo.attach(servoPin);
servo.write(closedAngle);
}
void loop() {
if (analogButtonPressed(interiorFloor)) {
interiorFloor.handle();
interiorTop.handle();
}
if (analogButtonPressed(frontTopLeft)) {
frontTopLeft.handle();
frontTopRight.handle();
frontBottom.handle();
if (frontTopLeft.isOn()) {
servo.write(openAngle);
} else {
servo.write(closedAngle);
}
}
if (analogButtonPressed(frontBottom)){
}
if (analogButtonPressed(leftBlinker)) {
leftBlinker.handle();
if (leftBlinker.shallBlink) {
rightBlinker.off();
}
}
if (analogButtonPressed(rightBlinker)) {
rightBlinker.handle();
if (rightBlinker.shallBlink) {
leftBlinker.off();
}
}
leftBlinker.doBlink();
rightBlinker.doBlink();
}