Thank you for all help, i did accomplished what i wanted and learn some things abut EEPROM thanks to you!
I wanted to add to my remote controlled blinds ability to save up to 5 remotes ID and corresponding channels by pressing combinations of buttons witch are on the box, clear EEPROM memory if needed and all works as it should despite my very low ability to code!
I will post script here so maybe someone can point bad thinks and how to fix them or if someone want to see how i did it (probably everything is not how it should be coded but it works):
#include <EEPROM.h>
int rxPin = 2; // Input of 433 MHz receiver
int LED1 = 3; // LED for testing instead of servo commands
int buttonState1 = 0;
int buttonState2 = 0;
int button1 = 4;
int button2 = 5;
byte lastButtonState = HIGH;
byte buttonCounter = 0;
//Remotes ID
unsigned long Remote1;
unsigned long Remote2;
unsigned long Remote3;
unsigned long Remote4;
unsigned long Remote5;
//Channels:
byte Channel1;
byte Channel2;
byte Channel3;
byte Channel4;
byte Channel5;
// Buttons:
int UP = 3;
int Stop = 10;
int Down = 8;
void setup()
{
pinMode(rxPin, INPUT); // Input of 433 MHz receiver
Serial.begin(19200);
Serial.println ("Is on!");
pinMode(13, OUTPUT);
pinMode(LED1, OUTPUT);
pinMode(button1, INPUT);
pinMode(button2, INPUT);
}
void loop()
{
/////////////////////////////////////////////// read EEPROM
EEPROM.get(0, Remote1);
Channel1 = EEPROM.read(4);
EEPROM.get(5, Remote2);
Channel2 = EEPROM.read(9);
EEPROM.get(10, Remote3);
Channel3 = EEPROM.read(14);
EEPROM.get(15, Remote4);
Channel4 = EEPROM.read(19);
EEPROM.get(20, Remote5);
Channel5 = EEPROM.read(24);
///////////////////////////////////////////////Remote signal decoding
int i = 0;
unsigned long t = 0;
byte newBit = 0;
byte bit = 0;
unsigned long RemoteNr = 0;
unsigned long Channel = 0;
unsigned long Button = 0;
byte prevBit = 0;
buttonState1 = digitalRead(button1);
buttonState2 = digitalRead(button2);
while (i < 40)
{
t = pulseIn(rxPin, LOW, 1000000);
if (t > 620 && t < 700)
{ bit = 0;
}
else if (t > 300 && t < 450)
{ bit = 1;
}
else
{
i = 0;
break;
}
if (i >= 0 && i <= 17)
{
RemoteNr <<= 1;
RemoteNr |= prevBit;
}
else if (i >= 18 && i <= 21)
{
Channel <<= 1;
Channel |= prevBit;
}
else if (i >= 22 && i <= 25)
{
Button <<= 1;
Button |= prevBit;
}
prevBit = bit;
++i;
//Serial.print(prevBit); //Biniary print
}
if (i > 0)
{
printResult(RemoteNr, Channel, Button);
}
///////////////////////////////////////////////////////////////////////////////////Save Remote ID and Channel to EEPROM
checkButton(); //go to void checkButton()
if (buttonState1 == HIGH) //If button 1 is pressed...
{
if (buttonCounter == 1)// ...and button 2 i clicked once...
{
if (i > 0) // ...and remote button is click...
{
delay(10);
EEPROM.put(0, RemoteNr); // ...save remote ID and channel to EEPROM.
delay(50);
EEPROM.write(4, Channel);
delay(50);
buttonCounter = 0; //Clear button pressed count
}
}
if (buttonCounter == 2)
{
if (i > 0)
{
delay(10);
EEPROM.put(5, RemoteNr);
delay(50);
EEPROM.write(9, Channel);
delay(50);
buttonCounter = 0;
}
}
if (buttonCounter == 3)
{
if (i > 0)
{
delay(10);
EEPROM.put(10, RemoteNr);
delay(50);
EEPROM.write(14, Channel);
delay(50);
buttonCounter = 0;
}
}
if (buttonCounter == 4)
{
if (i > 0)
{
delay(10);
EEPROM.put(15, RemoteNr);
delay(50);
EEPROM.write(19, Channel);
delay(50);
buttonCounter = 0;
}
}
if (buttonCounter == 5)
{
if (i > 0)
{
delay(10);
EEPROM.put(20, RemoteNr);
delay(50);
EEPROM.write(24, Channel);
delay(50);
buttonCounter = 0;
}
}
}
if ((RemoteNr == Remote1 && Channel == Channel1) ||
(RemoteNr == Remote2 && Channel == Channel2) ||
(RemoteNr == Remote3 && Channel == Channel3) ||
(RemoteNr == Remote4 && Channel == Channel4) ||
(RemoteNr == Remote5 && Channel == Channel5))
{
if (UP)
{
digitalWrite(LED1, HIGH);
delay(500);
digitalWrite(LED1, LOW);
}
else if (Stop)
{
digitalWrite(LED1, HIGH);
delay(500);
digitalWrite(LED1, LOW);
}
else if (Down)
{
digitalWrite(LED1, HIGH);
delay(500);
digitalWrite(LED1, LOW);
}
}
if (buttonCounter == 10) //If button 1 is pressed and button 2 is clicked ten times clear EEPROM memory
{
for (int i = 0 ; i < EEPROM.length() ; i++)
{
EEPROM.write(i, 255);
}
}
}
///////////////////////////////////////////////////////////////////////////////////Count button presses
void checkButton()
{
if (buttonState1 == HIGH)
{
buttonState2 = digitalRead(button2);
if (lastButtonState != buttonState2)
{
lastButtonState = buttonState2;
if (buttonState2 == HIGH)
{
buttonCounter++;
Serial.println(buttonCounter);
}
}
}
if (buttonState1 == LOW) //Clear button pressed count if button 1 is not press
{
buttonCounter = 0;
}
}
///////////////////////////////////////////////////////////////////////////////////Print info to Serial Monitor
void printResult(unsigned long RemoteNr, unsigned long Channel, unsigned long Button)
{
Serial.print("Remote1: ");
Serial.println(Remote1);
Serial.print("Remote2: ");
Serial.println(Remote2);
Serial.print("Remote3: ");
Serial.println(Remote3);
Serial.print("Remote4: ");
Serial.println(Remote4);
Serial.print("Remote5: ");
Serial.println(Remote5);
//RemoteNr
Serial.print("RemoteNr: ");
Serial.println(RemoteNr);
//Channel;
Serial.print("Channel ");
switch (Channel) {
case 8:
Serial.println("1");
break;
case 4:
Serial.println("2");
break;
case 12:
Serial.println("3");
break;
case 2:
Serial.println("4");
break;
case 10:
Serial.println("5");
break;
case 6:
Serial.println("6");
break;
case 17:
Serial.println("7");
break;
case 1:
Serial.println("8");
break;
case 9:
Serial.println("9");
break;
case 5:
Serial.println("10");
break;
case 13:
Serial.println("11");
break;
case 3:
Serial.println("12");
break;
case 11:
Serial.println("13");
break;
case 7:
Serial.println("14");
break;
case 15:
Serial.println("All");
break;
}
//Button
Serial.print("Button ");
if (Button == 3) {
Serial.println("Up");
}
else if (Button == 10) {
Serial.println("Stop");
}
else if (Button == 8) {
Serial.println("Down");
}
Serial.println();
}
Serial Monitor output:
Remote1: 118317
Remote2: 118317
Remote3: 4294967295
Remote4: 4294967295
Remote5: 4294967295
RemoteNr: 82943
Channel 4
Button Stop