Hi evreyone ! A friend of me want to create a motorcycle with only two buttons to control everything. Don't ask me why, it's his idea ^^. So the challenge is to control horn, turn signal, start engine and lights with only two buttons. I have a piece of code, that control both turn signal and the horn. But i'm facing a serious problem. How to turn something on without turning something else on. In my case, i'v choosed to control the left light with button 1 and right light with button 2. The horn is on with both buttons pressed. But, every time i turn the horn on, one or the other of the turn signal goes on and that's a problem. I tried to define a function to check if th klaxon was on just before but with non sucess. I put my code without that function. Can somebody help me please ?
const int left_blinker = 5;
const int right_blinker = 6;
const int klaxon = 7;
const int btn_1 = 2;
const int btn_2 = 3;
int left_blinker_state = LOW;
int right_blinker_state = LOW;
int btn_1_status = LOW;
int btn_2_status = LOW;
bool is_klaxon_on(){
if(digitalRead(btn_1) && digitalRead(btn_2)){
return(true);
}else{
return(false);
delay(200);
}
}
unsigned long left_blinker_previous_millis = 0;
unsigned long right_blinker_previous_millis = 0;
const long interval = 200;
void setup() {
pinMode(left_blinker,OUTPUT);
pinMode(right_blinker,OUTPUT);
pinMode(klaxon,OUTPUT);
pinMode(btn_1,INPUT);
pinMode(btn_2,INPUT);
Serial.begin(9600);
}
void loop() {
Serial.print(was_klaxon_on());
Serial.print("\n");
unsigned long current_millis = millis();
// klaxon
if(is_klaxon_on()){
digitalWrite(klaxon,HIGH);
delay(200);
}else{
digitalWrite(klaxon,LOW);
}
// lecture état bouton 1
if(digitalRead(btn_1)){
delay(200);
if(is_klaxon_on() == false){
if(btn_1_status == LOW){
btn_1_status = HIGH;
}else{
btn_1_status = LOW;
}
}else{
digitalWrite(left_blinker,LOW);
}
}
// lecture état bouton 2
if(digitalRead(btn_2)){
delay(200);
if(is_klaxon_on() == false){
if(btn_2_status == LOW){
btn_2_status = HIGH;
}else{
btn_2_status = LOW;
}
}else{
digitalWrite(right_blinker,LOW);
}
}
// cligno gauche
if(btn_1_status == HIGH && is_klaxon_on() == false){
if ((current_millis - left_blinker_previous_millis >= interval) && btn_1_status) {
left_blinker_previous_millis = current_millis;
if (left_blinker_state == LOW) {
left_blinker_state = HIGH;
} else {
left_blinker_state = LOW;
}
digitalWrite(left_blinker,left_blinker_state);
}
}
if(btn_1_status == LOW || is_klaxon_on()){
digitalWrite(left_blinker,LOW);
}
//cligno droit
if(btn_2_status == HIGH && is_klaxon_on() == false){
if ((current_millis - right_blinker_previous_millis >= interval) && btn_2_status) {
right_blinker_previous_millis = current_millis;
if (right_blinker_state == LOW) {
right_blinker_state = HIGH;
} else {
right_blinker_state = LOW;
}
digitalWrite(right_blinker,right_blinker_state);
}
}
if(btn_2_status == LOW || is_klaxon_on()){
digitalWrite(right_blinker,LOW);
}
}
Thanks in advance
Nathan