Go Down

Topic: 7 Segment display (Read 877 times) previous topic - next topic

ariffamir

I have a question here. Is it possible to program both increment and decrease value on 7 segment display because i only be able to program the 7 segment display to increase value. Logic is my project have 3 push button switch, 1 push button is for increase value second is for decrease value and last one is for reset the value to zero. Im newbie in programming and my lecturer ask me to learn this by myself. sorry for the bad english because english is not my mother tongue.

Grumpy_Mike

#1
Jan 18, 2016, 04:10 pm Last Edit: Jan 18, 2016, 04:11 pm by Grumpy_Mike
Quote
Is it possible to program both increment and decrease value on 7 segment display
Yes.

Quote
and my lecturer ask me to learn this by myself.
So should you be asking here?

We need to see your code and a schematic (not a Fritzing physical layout diagram) showing how you have wired things up.
Please read this before posting code:-
How to use this forum

ariffamir

#2
Jan 18, 2016, 04:53 pm Last Edit: Jan 18, 2016, 04:58 pm by ariffamir
Yes.
So should you be asking here?

We need to see your code and a schematic (not a Fritzing physical layout diagram) showing how you have wired things up.
Please read this before posting code:-
How to use this forum

My lecturer said that we can ask around the web. So i did it.


this is the coding :

Code: [Select]
byte numbers[10] = {
                    B11111100, B01100000, B11011010, B11110010, B01100110,
                    B10110110, B10111110, B11100000, B11111110, B11100110
                    };
void setup() {
    for(int i = 2; i <= 8; i++) {
        pinMode(i, OUTPUT);
    }
    pinMode(9, INPUT);
}
 
int counter = 9;
bool go_by_switch = true;
int last_input_value = LOW;
 
void loop() {
    if(go_by_switch) {
        int switch_input_value = digitalRead(9);
        if(last_input_value == LOW && switch_input_value == HIGH) {
            counter = (counter + 1) % 10;
        }
        last_input_value = switch_input_value;
    } else {
        delay(500);
        counter = (counter + 1) % 10;
    }
    writeNumber(counter);
}
 
void writeNumber(int number) {
    if(number < 0 || number > 9) {
        return;
    }
    byte mask = numbers[number];
    byte currentPinMask = B10000000;
    for(int i = 2; i <= 8; i++) {
        if(mask & currentPinMask) digitalWrite(i,HIGH);
        else digitalWrite(i,LOW);
    currentPinMask = currentPinMask >> 1;
    }
}

Grumpy_Mike

You need a resistor in each anode not one in the common cathode. As you have it the brightness will change depending on the number of segments that are lit.

Quote
my project have 3 push button switch
There is only one button in that diagram.

What is that code doing that you don't want it to?

ariffamir


[/quote]
You need a resistor in each anode not one in the common cathode. As you have it the brightness will change depending on the number of segments that are lit.
There is only one button in that diagram.

Dear mike,
   
     I already put the resistor in each anode. The problem is, I do not know how to modify the circuit for it to have 3 input switch. I already done the reset button thing last night. I only be able to modify the coding to increase or decrease value one at a time.

What is that code doing that you don't want it to?
I need to modify the coding to have both increase and decrease value. The code i give to u in last post was working perfectly. I just dont get it how to modify the coding.




Grumpy_Mike

First you need to modify the diagram and add another button.
Then you need to test for that button like you did for the one you have. When you see it being pressed you call a new function that is almost identical to the one you have only this time decrement the number.

ariffamir

First you need to modify the diagram and add another button.
Then you need to test for that button like you did for the one you have. When you see it being pressed you call a new function that is almost identical to the one you have only this time decrement the number.
Dear Mike, i already done add one input for switch and already tested and it works. But now whenever i press my switches, the number change randomly. Below are my coding and i hope you can correct me.


Code: [Select]
byte numbers[10] = {
                    B11111100, B01100000, B11011010, B11110010, B01100110,
                    B10110110, B10111110, B11100000, B11111110, B11100110
                    };
void setup() {
    for(int i = 2; i <= 8; i++) {
        pinMode(i, OUTPUT);
    }
    pinMode(10, INPUT);
    pinMode(13, INPUT);
}
 
int counter = 9;
bool go_by_switch = true;
int last_input_value = LOW;
 
void loop() {
    if(go_by_switch) {
        int switch_input_value = digitalRead(13);
        if(last_input_value == LOW && switch_input_value == HIGH) {
            counter = (counter + 1) % 10;
        }
        last_input_value = switch_input_value;
    } else {
        delay(500);
        counter = (counter + 1) % 10;
    }
    writeNumber(counter);
   
    if(go_by_switch) {
        int switch_input_value = digitalRead(10);
        if(last_input_value == LOW && switch_input_value == HIGH) {
            counter = (counter - 1) % 10;
        }
        last_input_value = switch_input_value;
    } else {
        delay(500);
        counter = (counter - 1) % 10;
    }
    writeNumber(counter);
}
 
void writeNumber(int number) {
    if(number < 0 || number > 9) {
        return;
    }
    byte mask = numbers[number];
    byte currentPinMask = B10000000;
    for(int i = 2; i <= 8; i++) {
        if(mask & currentPinMask) digitalWrite(i,HIGH);
        else digitalWrite(i,LOW);
    currentPinMask = currentPinMask >> 1;
    }
}

Grumpy_Mike

You are using the same variable for the last value for both switches. These need to be a separate variable for each switch.

ariffamir

You are using the same variable for the last value for both switches. These need to be a separate variable for each switch.
Dear Mike,

   I do not know how to separate the variable. Already try google about debounce but i still do not understand how the coding works. Sorry for being so dumb at coding.

Grumpy_Mike

Quote
I do not know how to separate the variable.
I did not say separate the variables, I said use separate variables.

So instead of using last_input_value for both buttons you use last_input_value_up for the up button and last_input_value_down for the down button.

You do not need any debounce with your delay( 500 ), in fact that could be delay( 50 )

ariffamir

I did not say separate the variables, I said use separate variables.

So instead of using last_input_value for both buttons you use last_input_value_up for the up button and last_input_value_down for the down button.

You do not need any debounce with your delay( 500 ), in fact that could be delay( 50 )
Thank you so much random guy. I have succeed by your help. My the god bless your soul. Thanks again for being such a big help. You're my hero. And below is my new code. Thanks again sir.

Code: [Select]
byte numbers[10] = {
                    B11111100, B01100000, B11011010, B11110010, B01100110,
                    B10110110, B10111110, B11100000, B11111110, B11100110
                    };
void setup() {
    for(int i = 2; i <= 8; i++) {
        pinMode(i, OUTPUT);
    }
    pinMode(10, INPUT);
    pinMode(13, INPUT);
}
 
int counter = 9;
bool go_by_switch = true;
int last_input_up = LOW;
int last_input_down = LOW;

void loop() {
    if(go_by_switch) {
        int switch_input_value = digitalRead(13);
        if(last_input_up == LOW && switch_input_value == HIGH) {
            counter = (counter + 1) % 10;
        }
        last_input_up = switch_input_value;
    } else {
        delay(500);
        counter = (counter + 1) % 10;
    }
    writeNumber(counter);
   
    if(go_by_switch) {
        int switch_input_value = digitalRead(10);
        if(last_input_down == LOW && switch_input_value == HIGH) {
            counter = (counter - 1) % 10;
        }
        last_input_down = switch_input_value;
    } else {
        delay(500);
        counter = (counter - 1) % 10;
    }
    writeNumber(counter);
}
 
void writeNumber(int number) {
    if(number < 0 || number > 9) {
        return;
    }
    byte mask = numbers[number];
    byte currentPinMask = B10000000;
    for(int i = 2; i <= 8; i++) {
        if(mask & currentPinMask) digitalWrite(i,HIGH);
        else digitalWrite(i,LOW);
    currentPinMask = currentPinMask >> 1;
    }
}

Grumpy_Mike

Quote
Thank you so much random guy.
Hey that's fine, glad you got it going. You show the right attitude to learning stuff, well done.

Go Up