Motorcycle Gear Indicator help

HI,

Newbie on the forum and looking for some help with some code.

I downloaded a sketch made by Filipe Dias that would display the gear my motorcycles is in. I have changed his code a bit (hope you don't mind Filipe . . ) and it does work. Changing up and down and neutral all work using tactile switches to simulate hall effect sensors.

My problem is: If the switch is pressed slowly it will rattle through the numbers to the max (in my case 5). Same going down. It does the same with the sensors. If the magnet is around for too long it thinks I've changed more than one gear.

I thought it was a debounce problem and fitted a debounce circuit on each input using R/C and a Schmitt inverter (x2).

I now believe the code is the problem.

Code:

/*
MOTORCYCLE SHIFT INDICATOR

( For Common Cathode Display )

by Filipe Dias
Pins:
D2 - 7 (A)
D3 - 6 (B)
D4 - 4 (C)
D5 - 2 (D)
D6 - 1 (E)
D7 - 9 (F)
D8 - 10 (G)
D10 - neutral
D11 - up
D12 - down
*/
byte change = 6;

#define segmenta 2
#define segmentb 3
#define segmentc 4
#define segmentd 5
#define segmente 6
#define segmentf 7
#define segmentg 8
#define neutral 10
#define up 11
#define down 12

void setup() {
// Define Pins
pinMode(segmenta, OUTPUT);
pinMode(segmentb, OUTPUT);
pinMode(segmentc, OUTPUT);
pinMode(segmentd, OUTPUT);
pinMode(segmente, OUTPUT);
pinMode(segmentf, OUTPUT);
pinMode(segmentg, OUTPUT);
pinMode(neutral, INPUT_PULLUP);
pinMode(up, INPUT_PULLUP);
pinMode(down, INPUT_PULLUP);

// All leds test
for (int x = 0; x < 3; x++){
digitalWrite(segmenta, 1);
delay(200);
digitalWrite(segmenta, 0);
digitalWrite(segmentb, 1);
delay(200);
digitalWrite(segmentb, 0);
digitalWrite(segmentg, 1);
delay(200);
digitalWrite(segmentg, 0);
digitalWrite(segmente, 1);
delay(200);
digitalWrite(segmente, 0);
digitalWrite(segmentd, 1);
delay(200);
digitalWrite(segmentd, 0);
digitalWrite(segmentc, 1);
delay(200);
digitalWrite(segmentc, 0);
digitalWrite(segmentg, 1);
delay(200);
digitalWrite(segmentg, 0);
digitalWrite(segmentf, 1);
delay(200);
digitalWrite(segmentf, 0);
}
}

void loop(){
if(change == 6){
//set LED values
if(digitalRead(neutral)==LOW){
change=0;
}
}else{
if(digitalRead(up)==LOW){
if(change==0){
change = 2; //shift up from neutral to 2nd
}else if(change == 1){
//do nothing check for neutral later
}else if(change<5){
change=change + 1; //shift up from any other gear
}
}else if(digitalRead(down)==LOW){
if(change ==2){
//do nothing check for neutral later
}else if(change == 0){
change = 1; //shift down from neutral into 1st
}else if(change>1){
change=change-1;
}else if(change == 1){
//do nothing check for neutral later
}
}else if(digitalRead(neutral)==LOW && (change == 2 || change == 1)){
change = 0;
}
updateLED();
delay(100);
}
}

void updateLED(){
switch(change){
case 0:
digitalWrite(segmenta, 0);
digitalWrite(segmentb, 0);
digitalWrite(segmentc, 1);
digitalWrite(segmentd, 0);
digitalWrite(segmente, 1);
digitalWrite(segmentf, 0);
digitalWrite(segmentg, 1);
break;
case 1:
digitalWrite(segmenta, 0);
digitalWrite(segmentb, 1);
digitalWrite(segmentc, 1);
digitalWrite(segmentd, 0);
digitalWrite(segmente, 0);
digitalWrite(segmentf, 0);
digitalWrite(segmentg, 0);
break;
case 2:
digitalWrite(segmenta, 1);
digitalWrite(segmentb, 1);
digitalWrite(segmentc, 0);
digitalWrite(segmentd, 1);
digitalWrite(segmente, 1);
digitalWrite(segmentf, 0);
digitalWrite(segmentg, 1);
break;
case 3:
digitalWrite(segmenta, 1);
digitalWrite(segmentb, 1);
digitalWrite(segmentc, 1);
digitalWrite(segmentd, 1);
digitalWrite(segmente, 0);
digitalWrite(segmentf, 0);
digitalWrite(segmentg, 1);
break;
case 4:
digitalWrite(segmenta, 0);
digitalWrite(segmentb, 1);
digitalWrite(segmentc, 1);
digitalWrite(segmentd, 0);
digitalWrite(segmente, 0);
digitalWrite(segmentf, 1);
digitalWrite(segmentg, 1);
break;
case 5:
digitalWrite(segmenta, 1);
digitalWrite(segmentb, 0);
digitalWrite(segmentc, 1);
digitalWrite(segmentd, 1);
digitalWrite(segmente, 0);
digitalWrite(segmentf, 1);
digitalWrite(segmentg, 1);
break;

 case 6:
      break;
 }

}

Any help would be much appreciated.

Cheers,

Andy

Hello Andy,

The code just goes round checking that state of an input, making a change, waiting a bit, check again and repeat. This is a classic beginner's mistake, checking the state of an input instead of checking if the state of the input has changed.

There are examples in the various tutorials on this website for detecting change of state of inputs and correctly dealing with the results. Here is mine Buttons and other electro-mechanical inputs (introduction) but there are plenty of others.

I note that the code is not yours, it's junk.

I also think that if the code is not yours and you can't see that it's junk then you are probably a beginner. Nothing wrong with being a beginner, that's what Arduino and this web site is about, are you ready to learn?

Hi Perry,
Thanks for replying. Yes, from an Arduino point of view I am a newbie, and I did not see the code was junk. I've programmed in other languages a long time ago (retired now) and what he was doing seemed to make sense.

Could I incorporate your state change code in to his as the rest of it seems to work (detecting neutral, changing up/down and displaying numbers) or would I be better off starting from fresh ?

Cheers,

Andy

Hi Andy,

I suggest you forget that code and learn how to do it yourself, in any case you can't really modify someone's code without understanding it, and if you understand it you can write your own.

My tutorial is not aimed at the absolute beginner, so if you don't understand my examples then go back and study the examples in the IDE for beginners. There is a second part to my tutorial with more complex examples as well.

There are lots of other tutorials on here that will also help you, have a read through some of them.

Enjoy.

Hi Perry,

Guess it's back to the drawing board then . . . .

Thanks for your help and advice.

Cheers,

Andy

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