Hello, I am new to uControllers and I haven't program for a long time.
I am having problems with a counter that counts Up when button one is pressed, counts down when button 2 is pressed and stops when both buttons are pressed. I assume that the display shows nothing when both buttons are not pressed. (english is my second language)
the count is shown in a 7 Segments Display and just counts from 0 to 3 or vice-verse, my problem is that I cant make it stop in any number. It always finish the for loop, I mean that it stops in 3 when the count is going up and the second button is pressed, and in 0 when the count is going down and the second button is pressed.
I think that I understand why is doing that: the for loop must end and then repeat the loop, I just can't find the way of doing it in a different way. Already tried everything I could think of :). And I still don't understand why the problem is to build a 2 bits Up/Down counter and not to use single leds in BCD to show the count. Why I don't ask my instructor is long story
thank you guys in advance
this is my code:
//---------------------------------------
const int buttonOne = 10;
const int buttonTwo = 12;
const int ledPinA = 4;
const int ledPinB = 5;
const int ledPinC = 7;
const int ledPinD = 8;
const int ledPinE = 9;
const int ledPinF = 3;
const int ledPinG = 2;
const int ledPinDP = 6; //The order of the pins is to make it easier to connect
int buttonOneState = 0;
int buttonTwoState = 0;
int i;
void setup()
{
pinMode(ledPinA, OUTPUT);
pinMode(ledPinB, OUTPUT);
pinMode(ledPinC, OUTPUT);
pinMode(ledPinD, OUTPUT);
pinMode(ledPinE, OUTPUT);
pinMode(ledPinF, OUTPUT);
pinMode(ledPinG, OUTPUT);
pinMode(ledPinDP, OUTPUT);
pinMode(buttonOne, INPUT);
pinMode(buttonTwo, INPUT);
}
void loop()
{
buttonOneState = digitalRead(buttonOne);
buttonTwoState = digitalRead(buttonTwo);
for( i=0; i< 4; i++)
{
if (buttonOneState == HIGH && buttonTwoState == LOW) {
delay(500);
led(i); }
else if (buttonOneState == LOW && buttonTwoState == HIGH) {
delay(500);
led(3-i); }
else if (buttonOneState == HIGH && buttonTwoState == HIGH) {
break;
delay(500); } //here is where I don't know
else {
delay(500);
led(4); }
}
}
void led(int x)
{
if (x == 0) {
digitalWrite(ledPinA, LOW);
digitalWrite(ledPinB, LOW);
digitalWrite(ledPinC, LOW);
digitalWrite(ledPinD, LOW);
digitalWrite(ledPinE, LOW);
digitalWrite(ledPinF, LOW);
digitalWrite(ledPinG, HIGH);
digitalWrite(ledPinDP, LOW); }
else if (x == 1) {
digitalWrite(ledPinA, HIGH);
digitalWrite(ledPinB, LOW);
digitalWrite(ledPinC, LOW);
digitalWrite(ledPinD, HIGH);
digitalWrite(ledPinE, HIGH);
digitalWrite(ledPinF, HIGH);
digitalWrite(ledPinG, HIGH);
digitalWrite(ledPinDP, LOW); }
else if (x == 2) {
digitalWrite(ledPinA, LOW);
digitalWrite(ledPinB, LOW);
digitalWrite(ledPinC, HIGH);
digitalWrite(ledPinD, LOW);
digitalWrite(ledPinE, LOW);
digitalWrite(ledPinF, HIGH);
digitalWrite(ledPinG, LOW);
digitalWrite(ledPinDP, LOW); }
else if (x == 3) {
digitalWrite(ledPinA, LOW);
digitalWrite(ledPinB, LOW);
digitalWrite(ledPinC, LOW);
digitalWrite(ledPinD, LOW);
digitalWrite(ledPinE, HIGH);
digitalWrite(ledPinF, HIGH);
digitalWrite(ledPinG, LOW);
digitalWrite(ledPinDP, LOW); }
else {
digitalWrite(ledPinA, HIGH);
digitalWrite(ledPinB, HIGH);
digitalWrite(ledPinC, HIGH);
digitalWrite(ledPinD, HIGH);
digitalWrite(ledPinE, HIGH);
digitalWrite(ledPinF, HIGH);
digitalWrite(ledPinG, HIGH);
digitalWrite(ledPinDP, HIGH); }
}
// if there is a chance: how to post the code in a embedded window?