Code cleanup

This commit is contained in:
2023-10-28 00:44:21 +02:00
parent 005c05ae61
commit 5be10b62c0
2 changed files with 54 additions and 48 deletions
-10
View File
@@ -1,13 +1,3 @@
;PlatformIO Project Configuration File
;
; Build options: build flags, source filter
; Upload options: custom upload port, speed and extra flags
; Library options: dependencies, extra library storages
; Advanced options: extra scripting
;
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html
[env:uno] [env:uno]
platform = atmelavr platform = atmelavr
board = uno board = uno
+45 -29
View File
@@ -1,23 +1,17 @@
/************************************* /*************************************
* Kerstboomshield voor Arduino + ESP8266 * arduino-kerstboomshield
* Author: Michel Bats * Author: Michel Bats
* Website: www.batssoft.nl * Website: www.batssoft.nl
* Version: 2.0.0
* Revision: 20191228
*************************************/ *************************************/
// Bibliotheken // Bibliotheken
#include <Arduino.h> #include <Arduino.h>
// Bepalingen // Bepalingen
int ledPin[] = { int ledPin[] = {6, 9, 10, 11}; // Uitgangen met PWM-mogelijkheden
6 ,9 ,10,11}; // Uitgangen met PWM-mogelijkheden int randomLed[] = {0, 0, 0, 0}; // Variabele om te gebruiken bij het aftellen tot het moment van omschakelen
int randomLed[] = { int countLedUp[] = {0, 0, 0, 0}; // Variabele waarbij bij 1 de LED feller moet gaan branden en bij 0 zwakker
0 ,0 ,0 ,0 }; // Variabele om te gebruiken bij het aftellen tot het moment van omschakelen int currentLedPwm[] = {0, 0, 0, 0}; // Variabele waarin de huidige PWM-waarde is opgeslagen (0-255)
int countLedUp[] = {
0 ,0 ,0 ,0 }; // Variabele waarbij bij 1 de LED feller moet gaan branden en bij 0 zwakker
int currentLedPwm[] = {
0 ,0 ,0 ,0 }; // Variabele waarin de huidige PWM-waarde is opgeslagen (0-255)
int ledCount = 4; // Aantal aangesloten LED's (of LED-arrays) int ledCount = 4; // Aantal aangesloten LED's (of LED-arrays)
int delayPot = A5; // Ingang potmeter int delayPot = A5; // Ingang potmeter
@@ -28,17 +22,22 @@ int sketchDelay = 0; // Om in plaats van een delay een aantal keer geen
String inputString = ""; // Een sting om inkomende data in weg te schrijven. String inputString = ""; // Een sting om inkomende data in weg te schrijven.
void serialEvent() { void serialEvent()
while (Serial.available()) { {
while (Serial.available())
{
// Haal de nieuwe byte op. // Haal de nieuwe byte op.
char inChar = (char)Serial.read(); char inChar = (char)Serial.read();
// Verander het in een string. // Verander het in een string.
inputString = inChar; inputString = inChar;
// Als de nieuwe waarde iets is om wat mee te doen pas state dan aan. // Als de nieuwe waarde iets is om wat mee te doen pas state dan aan.
if(inputString == "1"){ if (inputString == "1")
{
state = true; state = true;
Serial.println("ON"); Serial.println("ON");
} else if(inputString == "0"){ }
else if (inputString == "0")
{
state = false; state = false;
Serial.println("OFF"); Serial.println("OFF");
} }
@@ -46,9 +45,11 @@ void serialEvent() {
} }
} }
void setup() { void setup()
{
// Loop om door het aantal volgens ledCount te gaan // Loop om door het aantal volgens ledCount te gaan
for (int i=0; i <= (ledCount-1); i++){ for (int i = 0; i <= (ledCount - 1); i++)
{
// eerst initialisatie van de PWM-puls op de uitgangen // eerst initialisatie van de PWM-puls op de uitgangen
analogWrite(ledPin[i], currentLedPwm[i]); analogWrite(ledPin[i], currentLedPwm[i]);
} }
@@ -59,25 +60,33 @@ void setup() {
inputString.reserve(200); inputString.reserve(200);
} }
void loop() { void loop()
if (state == 1) { {
if (state == 1)
{
// Uitlezen potmeter voor instelling vertraging (delay()) // Uitlezen potmeter voor instelling vertraging (delay())
int delayPotValue = analogRead(delayPot); int delayPotValue = analogRead(delayPot);
// Gegevens potmeter omzetten naar 1 t/m 25ms voor de delay() // Gegevens potmeter omzetten naar 1 t/m 25ms voor de delay()
int delayPotValueMap = map(delayPotValue, 0, 1023, 1, 100); int delayPotValueMap = map(delayPotValue, 0, 1023, 1, 100);
// Loop om door het aantal volgens ledCount te gaan // Loop om door het aantal volgens ledCount te gaan
if (sketchDelay <= 0) { if (sketchDelay <= 0)
for (int i=0; i <= (ledCount-1); i++){ {
for (int i = 0; i <= (ledCount - 1); i++)
{
// Controleren of randomLed 0 of kleiner is zodat deze een nieuwe waarde kan krijgen (random() tussen max in de nieuwe richting en de helft van max en huidige waarde) // Controleren of randomLed 0 of kleiner is zodat deze een nieuwe waarde kan krijgen (random() tussen max in de nieuwe richting en de helft van max en huidige waarde)
if (randomLed[i] <= 0){ if (randomLed[i] <= 0)
{
// Waarde countLedUp omdraaien // Waarde countLedUp omdraaien
countLedUp[i] = map(countLedUp[i], 1, 0, 0, 1); countLedUp[i] = map(countLedUp[i], 1, 0, 0, 1);
// Controleren of countLedUp nu 1 of 0 is. // Controleren of countLedUp nu 1 of 0 is.
if (countLedUp[i] == 1) { if (countLedUp[i] == 1)
{
// In geval van een 1: temp0 vullen met de maximaal aantal stappen tussen currentLedPwm en de maximale PWM-puls-waarde (255). // In geval van een 1: temp0 vullen met de maximaal aantal stappen tussen currentLedPwm en de maximale PWM-puls-waarde (255).
temp0 = 255 - currentLedPwm[i]; temp0 = 255 - currentLedPwm[i];
} else { }
else
{
// In geval van een 0: temp0 vullen met currentLedPwm aangezien dat de stappen zijn tot de minimale waarde voor een PWM-puls (0). // In geval van een 0: temp0 vullen met currentLedPwm aangezien dat de stappen zijn tot de minimale waarde voor een PWM-puls (0).
temp0 = currentLedPwm[i]; temp0 = currentLedPwm[i];
} }
@@ -86,10 +95,13 @@ void loop() {
} }
// Controleren of de LED(-array) feller of zwakker moet gaan branden op basis van countLedUp // Controleren of de LED(-array) feller of zwakker moet gaan branden op basis van countLedUp
if (countLedUp[i] == 1) { if (countLedUp[i] == 1)
{
// currentLedPwm met 1 ophogen. // currentLedPwm met 1 ophogen.
currentLedPwm[i]++; currentLedPwm[i]++;
} else { }
else
{
// currentLedPwm met 1 verlagen. // currentLedPwm met 1 verlagen.
currentLedPwm[i]--; currentLedPwm[i]--;
} }
@@ -105,13 +117,17 @@ void loop() {
// delay(delayPotValueMap); // delay(delayPotValueMap);
sketchDelay = delayPotValueMap; sketchDelay = delayPotValueMap;
} else { }
else
{
sketchDelay--; sketchDelay--;
} }
} }
if(state == false) { if (state == false)
for (int i=0; i <= (ledCount-1); i++){ {
for (int i = 0; i <= (ledCount - 1); i++)
{
analogWrite(ledPin[i], 0); analogWrite(ledPin[i], 0);
} }
} }