// Alle pinnen van de leds
const uint8_t ledPins[] = { 9, 11, 10, 6, 3, 5 };
// de pin van de potmeter
byte potPin = A3;
byte switchPin = 12;
// de waarde die de potmeter aangeeft
int potValue;
int switchValue;
bool LED0 = 0;
// geeft de richting aan waar de led moeten draaien.
int richting;
// houdt de index bij van de huidige brandende led
int currentLed = 0;
// houdt de index bij van de leds van de staart
byte trails[5] = {0, 0, 0, 0, 0};
String action;
int oldSwitchValue;
unsigned long previousMillis = 0;
long interval;
void setup() {
// Zet de seriele monitor aan
Serial.begin(115200);
for (uint8_t cnt = 0; cnt < sizeof(ledPins); cnt++) {
// Zet alle leds uit
analogWrite(ledPins[cnt], 0);
// Zet de pin mode op output
pinMode(ledPins[cnt], OUTPUT);
}
pinMode(switchPin, INPUT_PULLUP);
}
void debug(String text, int variable) {
Serial.print("Mode :");
Serial.print(action);
Serial.print('\t');
Serial.print('\t');
Serial.print("Led :");
Serial.print(currentLed);
Serial.print('\t');
Serial.print('\t');
Serial.print(text);
Serial.print(" : ");
Serial.print(variable);
Serial.print('\t');
Serial.print("Richting");
Serial.print(" : ");
Serial.println(richting);
}
void Collect() {
/**
Reads the value of the pot meter
@param values none
@return void because only a local variable is being changed
*/
potValue = analogRead(potPin);
switchValue = digitalRead(switchPin);
}
void reset() {
/**
reset all values to the starting values
*/
if (switchValue == 0) {
currentLed = -1;
}
for (int i = 0; i < 5; i++) {
trails[i] = 0;
}
for (uint8_t cnt = 0; cnt < sizeof(ledPins); cnt++) {
// Zet alle leds uit
analogWrite(ledPins[cnt], 0);
}
}
void Process() {
/**
Change value of currentled
Changes the value of some local variables.
@param values none
@return void because only a local variable is being changed
*/
if (switchValue != oldSwitchValue) {
reset();
oldSwitchValue = switchValue;
}
if (switchValue == 0) {
action = "staart";
currentLed += richting;
for (int i = 4; i > 0; i--) {
trails[i] = trails[i - 1];
}
trails[0] = currentLed;
}
if (switchValue == 1) {
action = "twee_keer";
LED0 = !LED0;
}
// Set to predefined values
richting = 0;
interval = 5000;
// 512 - 30 (middle value - offset)
if (potValue < 482)
{
// richting wordt linksom
richting = -1;
// berekenen van de nieuwe interval voor linksom
interval = map(potValue, 482, 0, 500, 75);
}
// 512 + 30 (middle value + offset)
if (potValue > 542) {
richting = 1;
// berekenen van de nieuwe interval voor rechtsom
interval = map(potValue, 1023, 542, 75, 500);
}
debug("switchValue", switchValue);
}
void display_leidend() {
digitalWrite(ledPins[currentLed], LED0);
}
void display_volgend() {
if (action == "staart") {
analogWrite(ledPins[trails[4]], 0);
analogWrite(ledPins[trails[3]], 25);
analogWrite(ledPins[trails[2]], 54);
analogWrite(ledPins[trails[1]], 117);
}
if (action == "twee_keer") {
digitalWrite(ledPins[1], LED0);
digitalWrite(ledPins[2], !LED0);
digitalWrite(ledPins[3], !LED0);
digitalWrite(ledPins[4], LED0);
digitalWrite(ledPins[5], LED0);
}
}
void Display() {
/**
takes care of de display of leds
@param values none
@return void because nothing changes
*/
display_volgend();
display_leidend();
}
void preventOverflow() {
/**
take care that there is no overflow
@param values none
@return void because only a local variable is being changed
*/
if (richting == 1) {
if (action == "staart") {
if (currentLed >= sizeof(ledPins) - 1) {
currentLed = -1;
}
}
}
if (richting == -1) {
if (action == "staart") {
if (currentLed <= 0) {
currentLed = sizeof(ledPins);
}
}
}
}
bool timer() {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
return true;
}
return false;
}
void loop() {
if (timer()) {
Collect();
Process();
Display();
preventOverflow();
}
}