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]
platform = atmelavr
board = uno
+47 -31
View File
@@ -1,23 +1,17 @@
/*************************************
* Kerstboomshield voor Arduino + ESP8266
* arduino-kerstboomshield
* Author: Michel Bats
* Website: www.batssoft.nl
* Version: 2.0.0
* Revision: 20191228
*************************************/
// Bibliotheken
#include <Arduino.h>
// Bepalingen
int ledPin[] = {
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 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 ledPin[] = {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 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 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.
void serialEvent() {
while (Serial.available()) {
void serialEvent()
{
while (Serial.available())
{
// Haal de nieuwe byte op.
char inChar = (char)Serial.read();
// Verander het in een string.
inputString = inChar;
// Als de nieuwe waarde iets is om wat mee te doen pas state dan aan.
if(inputString == "1"){
if (inputString == "1")
{
state = true;
Serial.println("ON");
} else if(inputString == "0"){
}
else if (inputString == "0")
{
state = false;
Serial.println("OFF");
}
@@ -46,9 +45,11 @@ void serialEvent() {
}
}
void setup() {
void setup()
{
// 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
analogWrite(ledPin[i], currentLedPwm[i]);
}
@@ -59,37 +60,48 @@ void setup() {
inputString.reserve(200);
}
void loop() {
if (state == 1) {
void loop()
{
if (state == 1)
{
// Uitlezen potmeter voor instelling vertraging (delay())
int delayPotValue = analogRead(delayPot);
// Gegevens potmeter omzetten naar 1 t/m 25ms voor de delay()
int delayPotValueMap = map(delayPotValue, 0, 1023, 1, 100);
// Loop om door het aantal volgens ledCount te gaan
if (sketchDelay <= 0) {
for (int i=0; i <= (ledCount-1); i++){
if (sketchDelay <= 0)
{
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)
if (randomLed[i] <= 0){
if (randomLed[i] <= 0)
{
// 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.
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).
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).
temp0 = currentLedPwm[i];
}
// radom() waarde aanmaken tussen het maximale verschil en de helft daarvan.
randomLed[i] = random(temp0/2, temp0);
randomLed[i] = random(temp0 / 2, temp0);
}
// 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[i]++;
} else {
}
else
{
// currentLedPwm met 1 verlagen.
currentLedPwm[i]--;
}
@@ -105,13 +117,17 @@ void loop() {
// delay(delayPotValueMap);
sketchDelay = delayPotValueMap;
} else {
}
else
{
sketchDelay--;
}
}
if(state == false) {
for (int i=0; i <= (ledCount-1); i++){
if (state == false)
{
for (int i = 0; i <= (ledCount - 1); i++)
{
analogWrite(ledPin[i], 0);
}
}