I am up to make an RGBW controller that will power the RGBW LED strip. The MCU is GD32F330F8P6. It has a button which is intended to be used as a select button. You have to hold it pressed for a while to switch the case, and in a small window of time you need to press it to select the desired color (1 - 10). Every time you press the button, the LED strip should light the desired color just for you to know what you have selected, and not just count the presses of the button. The selected number is stored in EEPROM. After the last press, and a few seconds later, the MCU will restart itself and load the stored data, thus light the LED strip according to your selection.
I use this Arduino support:
Here is the code:
#include <Arduino.h>
#include <EEPROM.h>
#define led PA13
#define btn PA4
#define ledG PA5
#define ledR PA6
#define ledB PA7
#define ledW PB1
#define longTime 5000
int lastState = 0;
int currentState;
int caseSet = 0;
int countPress = 0;
unsigned long pressedTime = 0;
unsigned long releasedTime = 0;
unsigned long previousPressTime = 0;
int lastButtonState = 1;
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 50;
int buttonState;
int eepromValue;
int cnt = 15000;
void setup(){
Serial.begin(9600);
EEPROM.begin();
eepromValue = EEPROM.read(0);
Serial.println(eepromValue);
delay(2000);
pinMode(led, OUTPUT);
pinMode(btn, INPUT);
}
void loop(){
if(caseSet == 0){
grbwOutputFunc();
int currentState = digitalRead(btn);
if(lastState == HIGH && currentState == LOW){
pressedTime = millis();
}else if(lastState == LOW && currentState == HIGH){
releasedTime = millis();
}
long pressDuration = releasedTime - pressedTime;
if(pressDuration > longTime){
digitalWrite(led, HIGH);
caseSet = 1;
}else{
digitalWrite(led, LOW);
caseSet = 0;
}
lastState = currentState;
}
if(caseSet == 1){
int reading = digitalRead(btn);
if (reading != lastButtonState) {
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
if (reading != buttonState) {
buttonState = reading;
if (buttonState == LOW) {
countPress++;
if(countPress > 10){
countPress = 1;
}
EEPROM.write(0, countPress);
EEPROM.commit();
Serial.println((String)"broj: "+countPress);
switch (countPress){
case 1:
analogWriteColor1();
break;
case 2:
analogWriteColor2();
break;
case 3:
analogWriteColor3();
break;
case 4:
analogWriteColor4();
break;
case 5:
analogWriteColor5();
break;
case 6:
analogWriteColor6();
break;
case 7:
analogWriteColor7();
break;
case 8:
analogWriteColor8();
break;
case 9:
analogWriteColor9();
break;
case 10:
analogWriteColor10();
break;
}
}// if buttonState is low
}// if reading is not buttonState
if((millis() - lastDebounceTime) > cnt){
NVIC_SystemReset();
}
}
lastButtonState = reading;
}//caseSet 1
}
void grbwOutputFunc(){
switch (eepromValue){
case 1:
analogWriteColor1();
break;
case 2:
analogWriteColor2();
break;
case 3:
analogWriteColor3();
break;
case 4:
analogWriteColor4();
break;
case 5:
analogWriteColor5();
break;
case 6:
analogWriteColor6();
break;
case 7:
analogWriteColor7();
break;
case 8:
analogWriteColor8();
break;
case 9:
analogWriteColor9();
break;
case 10:
analogWriteColor10();
break;
}
}
void analogWriteColor1(){
analogWrite(ledG, 255);
analogWrite(ledR, 0);
analogWrite(ledB, 0);
analogWrite(ledW, 0);
}
void analogWriteColor2(){
analogWrite(ledG, 0);
analogWrite(ledR, 255);
analogWrite(ledB, 0);
analogWrite(ledW, 0);
}
void analogWriteColor3(){
analogWrite(ledG, 0);
analogWrite(ledR, 0);
analogWrite(ledB, 255);
analogWrite(ledW, 0);
}
void analogWriteColor4(){
analogWrite(ledG, 0);
analogWrite(ledR, 0);
analogWrite(ledB, 0);
analogWrite(ledW, 255);
}
void analogWriteColor5(){
analogWrite(ledG, 150);
analogWrite(ledR, 150);
analogWrite(ledB, 0);
analogWrite(ledW, 0);
}
void analogWriteColor6(){
analogWrite(ledG, 150);
analogWrite(ledR, 0);
analogWrite(ledB, 150);
analogWrite(ledW, 0);
}
void analogWriteColor7(){
analogWrite(ledG, 0);
analogWrite(ledR, 150);
analogWrite(ledB, 150);
analogWrite(ledW, 0);
}
void analogWriteColor8(){
analogWrite(ledG, 100);
analogWrite(ledR, 255);
analogWrite(ledB, 50);
analogWrite(ledW, 0);
}
void analogWriteColor9(){
analogWrite(ledG, 20);
analogWrite(ledR, 120);
analogWrite(ledB, 200);
analogWrite(ledW, 0);
}
void analogWriteColor10(){
analogWrite(ledG, 180);
analogWrite(ledR, 60);
analogWrite(ledB, 30);
analogWrite(ledW, 0);
}
Now the problem.
When I hold the button pressed for 5 sec, it change the case and the small LED on the board is lit so I can know I entered the "programming" state. If I press the button three times, I can select the third color (blue), and I can see I have selected it. The board will reset and the blue will stay. Same goes for the 1, 2, 3, 4 cases. But, when I select, say case 9, it will show the selected color while I am in the "programming" mode, but when it restarts, I will get case 8. The bug is in cases 5, 8, 9.
So, it shows the correct color while the button is pressed, while in the "programming" mode. After reset, it shows the correct color only for some cases, not for all, and I don't see the pattern why is it happening. The same happen no matter if I let the board reset itself after a time set, or I reset it manually.
I had a similar problem with a GD32F130F8P6, so I switch to this one.
Now, I do know that in the datasheet there is not an E of EEPROM.
But, the package has its EEPROM example, so it ... "works".
Any suggestion? I am all ears.