Good, I'm trying to do a lifetest programming (permanent ignition) age test (intermittent ignition) to test products with a relay connected to 230V. There are 2 buttons, I press 1st button and the relay turns on permanently, if I press button 1 again and the relay turns off. I press button 2 and the relay works intermittently, if I press it again it turns off.
PROBLEMS TO SOLVE: Button 2 does not work if it does not coincide with the beginning of the code reading
I need to insert a numerical cycle counter.
Código:
const int pulsador1=4;
const int pulsador2=7;
int val=0; valor actual del pulsador1
int val1=0;
int old_val=0;
int old_val1=0;
int estado=0;
int estado1=0;
void setup(){
pinMode(LED_BUILTIN,OUTPUT); pinMode(pulsador1,INPUT); pinMode(pulsador2,INPUT);
}
void loop(){
val=digitalRead(pulsador1); val1=digitalRead(pulsador2);
if ((val==HIGH)&& (old_val==LOW)){
state=1-state;
}
old_val = val;
if (state==1){
digitalWrite(LED_BUILTIN,HIGH);
}
else{
digitalWrite(LED_BUILTIN,LOW);
}
if (val1 && old_val1==0){ state1=
1-state1;
}
old_val1 = val1;
if (state1==1){
digitalWrite(LED_BUILTIN,HIGH);
delay(1000);
delay(1000);
}
else{
digitalWrite(LED_BUILTIN,LOW);
}
}
That code is very well done, but I prefer to use the structure of my code.
My idea is to make a language and structure as simple and basic as possible. In Void loop 2 blocks of conditions appear; the first block for 1 button (permanent on and off) and another block for another button (intermittent on and off).
const int pulsador1=4;
const int pulsador2=7;
int val1=0; //current value of button1
int val2=0;
int old_val1=0; //previous value button 1
int old_val2=0;
int state1=0; //output state when booting arduino
int state2=0;
void setup(){
pinMode(LED_BUILTIN,OUTPUT);
pinMode(pulsador1,INPUT);
pinMode(pulsador2,INPUT);
(pulsador1 && pulsador2 == LOW); //button 1 and 2 to low
if (digitalRead(pulsador1 && pulsador2 == LOW)) { // Here I would read the button and until button == LOW you don't start
digitalWrite(state1 && state2 == LOW);
}
else{}
// here I would put comparison loop
//serial println
void loop()
{
val1=digitalRead(pulsador1); //write to 0
val2=digitalRead(pulsador2); //read status
if ((val1==HIGH)&& (old_val1==LOW)){
state1=1-state1;
old_val1 = val1;
(state1==1);
digitalWrite(LED_BUILTIN,HIGH);
digitalWrite(pulsador1, HIGH);
}
else{
digitalWrite(LED_BUILTIN,LOW);
digitalWrite(pulsador2, HIGH);
}
if (val2 && old_val2==0){
state2=1-state2;
old_val2 = val2;
(state2==1);
digitalWrite(LED_BUILTIN,HIGH);
delay(1000);
digitalWrite(LED_BUILTIN,LOW);
delay(1000);
}
else{
digitalWrite(LED_BUILTIN,LOW);
}
}
This statement does nothing. If you meant it as a comment, an assertion of something true at this point in the code, you should use a traditional comment.
If on the other hand, you think it does something, you may want to change it so it actually does. Something.
We understand you are new; but every "help" forum generally has a basic set of general rules to follow to expedite your engagement with other forum members: here
For future inquiries/assistance, following the above will eliminate much of the static noise you received this time around.
Additionally since you corrected your own code, posting the working code is considered to be considerate and customary.