Lack of practice I forgot many function of C & C++... Can anyone help me with this coding Please...
I think this is The Main coding: Topic: Gear indicator
I first found this, Gear_Indicator_Raw
But, There was many problem in those sketch,
- Those sketches work just like simple counter,
- Can't Show "2" from "0"
- No Neutral Detection,
As Bike Gear, (1 N 2 3 4 5) One Down Four up, I made some Changes, But can't add the Neutral part smoothly...
And here, My code:
#define BUTTON_DOWN 13
#define BUTTON_UP 4
#define BUTTON_N 11
int ledPinA = 7;
int ledPinB = 12;
int ledPinC = 10;
int ledPinD = 9;
int ledPinE = 8;
int ledPinF = 6;
int ledPinG = 5;
int val_up = 0;
int val_down = 0;
int val_n = 0;
int old_val_n = 0;
int old_val_down = 0;
int old_val_up = 0;
int gear = 0;
// The setup() method runs once, when the sketch starts
void setup()
{
pinMode(BUTTON_N, INPUT);
pinMode(BUTTON_UP, INPUT);
pinMode(BUTTON_DOWN, INPUT);
pinMode(ledPinA, OUTPUT);
pinMode(ledPinB, OUTPUT);
pinMode(ledPinC, OUTPUT);
pinMode(ledPinD, OUTPUT);
pinMode(ledPinE, OUTPUT);
pinMode(ledPinF, OUTPUT);
pinMode(ledPinG, OUTPUT);
}
void led0()
{
digitalWrite(ledPinA, LOW);
digitalWrite(ledPinB, LOW);
digitalWrite(ledPinC, LOW);
digitalWrite(ledPinD, LOW);
digitalWrite(ledPinE, LOW);
digitalWrite(ledPinF, LOW);
digitalWrite(ledPinG, HIGH);
}
void led1()
{
digitalWrite(ledPinA, HIGH);
digitalWrite(ledPinB, LOW);
digitalWrite(ledPinC, LOW);
digitalWrite(ledPinD, HIGH);
digitalWrite(ledPinE, HIGH);
digitalWrite(ledPinF, HIGH);
digitalWrite(ledPinG, HIGH);
}
void led2()
{
digitalWrite(ledPinA, LOW);
digitalWrite(ledPinB, LOW);
digitalWrite(ledPinC, HIGH);
digitalWrite(ledPinD, LOW);
digitalWrite(ledPinE, LOW);
digitalWrite(ledPinF, HIGH);
digitalWrite(ledPinG, LOW);
}
void led3()
{
digitalWrite(ledPinA, LOW);
digitalWrite(ledPinB, LOW);
digitalWrite(ledPinC, LOW);
digitalWrite(ledPinD, LOW);
digitalWrite(ledPinE, HIGH);
digitalWrite(ledPinF, HIGH);
digitalWrite(ledPinG, LOW);
}
void led4()
{
digitalWrite(ledPinA, HIGH);
digitalWrite(ledPinB, LOW);
digitalWrite(ledPinC, LOW);
digitalWrite(ledPinD, HIGH);
digitalWrite(ledPinE, HIGH);
digitalWrite(ledPinF, LOW);
digitalWrite(ledPinG, LOW);
}
void led5()
{
digitalWrite(ledPinA, LOW);
digitalWrite(ledPinB, HIGH);
digitalWrite(ledPinC, LOW);
digitalWrite(ledPinD, LOW);
digitalWrite(ledPinE, HIGH);
digitalWrite(ledPinF, LOW);
digitalWrite(ledPinG, LOW);
}
void led6()
{
digitalWrite(ledPinA, LOW);
digitalWrite(ledPinB, HIGH);
digitalWrite(ledPinC, LOW);
digitalWrite(ledPinD, LOW);
digitalWrite(ledPinE, LOW);
digitalWrite(ledPinF, LOW);
digitalWrite(ledPinG, LOW);
}
void led7()
{
digitalWrite(ledPinA, LOW);
digitalWrite(ledPinB, LOW);
digitalWrite(ledPinC, LOW);
digitalWrite(ledPinD, HIGH);
digitalWrite(ledPinE, HIGH);
digitalWrite(ledPinF, HIGH);
digitalWrite(ledPinG, HIGH);
}
void led8()
{
digitalWrite(ledPinA, LOW);
digitalWrite(ledPinB, LOW);
digitalWrite(ledPinC, LOW);
digitalWrite(ledPinD, LOW);
digitalWrite(ledPinE, LOW);
digitalWrite(ledPinF, LOW);
digitalWrite(ledPinG, LOW);
}
void led9()
{
digitalWrite(ledPinA, LOW);
digitalWrite(ledPinB, LOW);
digitalWrite(ledPinC, LOW);
digitalWrite(ledPinD, LOW);
digitalWrite(ledPinE, HIGH);
digitalWrite(ledPinF, LOW);
digitalWrite(ledPinG, LOW);
}
void setLed(int number)
{
switch (number)
{
case 0:
led0();
break;
case 1:
led1();
break;
case 2:
led2();
break;
case 3:
led3();
break;
case 4:
led4();
break;
case 5:
led5();
break;
case 6:
led6();
break;
case 7:
led7();
break;
case 8:
led8();
break;
case 9:
led9();
break;
}
}
// the loop() method runs over and over again,
// as long as the Arduino has power
void oldloop()
{
led0();
led1();
led2();
led3();
led4();
led5();
led6();
led7();
led8();
led9();
}
void loop()
{
//neutral
val_n = digitalRead(BUTTON_N);
if ((val_n == HIGH) &&gear<=2)
{
gear = 0; //Posted wrong "gear = HIGH"
delay(100);
}
// Gear Up
val_up = digitalRead(BUTTON_UP);
if ((val_up == HIGH) && (old_val_up == LOW) &&(gear<6) && (gear>0))
{
gear += 1;
delay(100);
}
else if ((val_up == HIGH) && (old_val_up == LOW) &&(gear==0))
{
gear += 2;
delay(100);
}
old_val_up = val_up;
// Gear Down
val_down = digitalRead(BUTTON_DOWN);
if ((val_down == HIGH) && (old_val_down == LOW)&& (gear>1))
{
gear -= 1;
delay(100);
}
else if ((val_down == HIGH) && (old_val_down == LOW) && (gear==0))
{
gear += 1;
delay(100);
}
old_val_down = val_down;
// Max Limit
if (gear >= 5)
{
gear = 5;
}
setLed(gear);
}
The Problem:
- Without the "// neutral" part it's work OK, but can't show "0" without reset.
- With the "// neutral" part, While gear up, instead of increasing, display shows "0" after "1".
- If I press pin "4" very quickly, then it's increasing to Max gear.
I think there is some problem in this part:
//neutral
val_n = digitalRead(BUTTON_N);
if ((val_n == HIGH) &&gear<=2)
{
gear = HIGH;
delay(100);
}
And, Please, If anyone can tell me about "Void oldloop()", what this is for?
2 Answers 2
Likely problem
I'm not sure how to solve the problem, however, I see at least one item that looks strange in the //neutral part:
gear = HIGH;
This seems gear
is a pin status (LOW/HIGH), but two lines above:
if ((val_n == HIGH) &&gear<=2)
Here gear
is an integer value. This seems quite inconsequent to me.
Pin 4
The reason is you do not have a debouncing algorithm, check for a Debounce Arduino example. When a button is pressed or depressed, it switches sometimes very fast a few times; this might result in multiple increases/decreases of the gear.
oldLoop
The function oldLoop
is not used, but if you swap the names of loop
and oldLoop
you can use the 'test' scenario which is contained in oldLoop
.
Some other tips
- Use spacing consequently
- You can refactor the LED code a lot.
- You can use
gear++;
to increase orgear--;
to decrease.
Update
(Changed debouncing problem for pin 4).
You changed in your neutral code:
gear = 0;
Shouldn't that be gear = 1;
? As gear 1 means Neutral (gear 0 is 1).
-
Oops, sorry, I actually I used "gear=0", But maybe It's a mistake while I was editing it to post here..!!!BLUE– BLUE2020年05月27日 09:53:27 +00:00Commented May 27, 2020 at 9:53
-
1No, That does not works for me... But I guess I worked it out with
if ((val_up == HIGH) && (val_n == LOW) && (gear>0))
... The "oldloop" was confusing me... Let me check couple of times...BLUE– BLUE2020年05月27日 11:23:52 +00:00Commented May 27, 2020 at 11:23 -
1kay, after hours I was testing the circuit...The problem raises again...!!! Also change the whole pin set up, then the gear-up running it's own... I think there is issue on pin sensitivity...!! (checked with other boards) Maybe I should use some pull-up resistors...Or use analog pins for Input,,,BLUE– BLUE2020年05月28日 08:12:23 +00:00Commented May 28, 2020 at 8:12
-
1No digital pins are ok, but you should indeed use pull-up or pull-down resistors in case the button is depressed.Michel Keijzers– Michel Keijzers2020年05月28日 08:15:05 +00:00Commented May 28, 2020 at 8:15
-
1Yes, I used 10k pull-down... It's working now... Let me check my final & actual project, so I can mark this as solved... :)BLUE– BLUE2020年05月28日 08:31:25 +00:00Commented May 28, 2020 at 8:31
Your problem is that you are confused about the meaning of gear
.
One possible meaning could be 0 = neutral, 1 .. 5 = gear. But then, when changing gears you can't simply do + or -, as the sequence is 1 - 0 - 2 - 3 - ...
The other interpretation is: gear-- from 2 means neutral and you display something that indicates neutral ( e.g. a '0' or a '-' ) whenever gear==1, and you display a '1' when you do another gear--, and have a numeric value of 0 in gear
.
Your decision.
-
Actually I was confused about the "oldloop" part, and the original code was very complicated... I kind of shorted it... I think for the 7-segment, it could be done more simple...BLUE– BLUE2020年05月27日 18:38:42 +00:00Commented May 27, 2020 at 18:38
-
Easiest is to change your
switch (number) { case 0: led1(); break; case 1: ledneutral(); break;
DataFiddler– DataFiddler2020年05月27日 18:54:51 +00:00Commented May 27, 2020 at 18:54 -
All I understand after lots of failing... That, the problem is with Arduino pins sensitivity...!!! -_- There should be a full list of all common problems..!!!BLUE– BLUE2020年05月28日 08:08:06 +00:00Commented May 28, 2020 at 8:08