Check an ending condition

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 :slight_smile:
Nathan

No point at all putting anything after a return.

Please remember to use code tags when posting code.

bool is_klaxon_on(){
if(digitalRead(btn_1) && digitalRead(btn_2)){
return(true);
}else{
return(false);
delay(200);
}
}

Becomes

bool is_klaxon_on(){
  return digitalRead(btn_1) && digitalRead(btn_2);
}

Tell your friend, that he needs to remove all the delay()s and other blocking structures in order to implement a responsive system.

Right now, the two pushbuttons may as well be labelled ‘ride’ and ‘die’.

ok i'll change that. Thnaks

Ok thanks

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.