Trying to use a 7 segment display to count 0/9 with a potentiometer
up vote
2
down vote
favorite
im trying to switch number on the display using a
potentiometer, it is just counting from 0/9.
All help is much appreciated!
int SegmentA = 6;
int SegmentB = 7;
int SegmentC = 8;
int SegmentD = 9;
int SegmentE = 10;
int SegmentF = 11;
int SegmentG = 12;
const int PP = A0;
void setup()
{
pinMode (SegmentA, OUTPUT);
pinMode (SegmentB, OUTPUT);
pinMode (SegmentC, OUTPUT);
pinMode (SegmentD, OUTPUT);
pinMode (SegmentE, OUTPUT);
pinMode (SegmentF, OUTPUT);
pinMode (SegmentG, OUTPUT);
}
void loop() {
int POD = analogRead(PP);
int SM = map(POD, 0, 1023, 0 , 8);
switch (SM) {
case 0:
digitalWrite (SegmentA, HIGH);
digitalWrite (SegmentB, HIGH);
digitalWrite (SegmentC, HIGH);
digitalWrite (SegmentD, HIGH);
digitalWrite (SegmentE, HIGH); //0
digitalWrite (SegmentF, HIGH);
digitalWrite (SegmentG, LOW);
delay(1000);
case 1:
digitalWrite (SegmentA, LOW);
digitalWrite (SegmentB, HIGH);
digitalWrite (SegmentC, HIGH);
digitalWrite (SegmentD, LOW);
digitalWrite (SegmentE, LOW); //1
digitalWrite (SegmentF, LOW);
digitalWrite (SegmentG, LOW);
delay(1000);
case 2:
digitalWrite (SegmentA, HIGH);
digitalWrite (SegmentB, HIGH);
digitalWrite (SegmentC, LOW);
digitalWrite (SegmentD, HIGH);
digitalWrite (SegmentE, HIGH); //2
digitalWrite (SegmentF, LOW);
digitalWrite (SegmentG, HIGH);
delay(1000);
case 3:
digitalWrite (SegmentA, HIGH);
digitalWrite (SegmentB, HIGH);
digitalWrite (SegmentC, HIGH);
digitalWrite (SegmentD, HIGH);
digitalWrite (SegmentE, LOW); //3
digitalWrite (SegmentF, LOW);
digitalWrite (SegmentG, HIGH);
delay(1000);
case 4:
digitalWrite (SegmentA, LOW);
digitalWrite (SegmentB, HIGH);
digitalWrite (SegmentC, HIGH);
digitalWrite (SegmentD, LOW);
digitalWrite (SegmentE, LOW); //4
digitalWrite (SegmentF, HIGH);
digitalWrite (SegmentG, HIGH);
delay(1000);
case 5:
digitalWrite (SegmentA, HIGH);
digitalWrite (SegmentB, LOW);
digitalWrite (SegmentC, HIGH);
digitalWrite (SegmentD, HIGH);
digitalWrite (SegmentE, LOW); //5
digitalWrite (SegmentF, HIGH);
digitalWrite (SegmentG, HIGH);
delay(1000);
case 6:
digitalWrite (SegmentA, HIGH);
digitalWrite (SegmentB, LOW);
digitalWrite (SegmentC, HIGH);
digitalWrite (SegmentD, HIGH);
digitalWrite (SegmentE, HIGH); //6
digitalWrite (SegmentF, HIGH);
digitalWrite (SegmentG, HIGH);
delay(1000);
case 7:
digitalWrite (SegmentA, HIGH);
digitalWrite (SegmentB, HIGH);
digitalWrite (SegmentC, HIGH);
digitalWrite (SegmentD, LOW);
digitalWrite (SegmentE, LOW); //7
digitalWrite (SegmentF, LOW);
digitalWrite (SegmentG, LOW);
delay(1000);
case 8:
digitalWrite (SegmentA, HIGH);
digitalWrite (SegmentB, HIGH);
digitalWrite (SegmentC, HIGH);
digitalWrite (SegmentD, HIGH);
digitalWrite (SegmentE, HIGH); //8
digitalWrite (SegmentF, HIGH);
digitalWrite (SegmentG, HIGH);
}
}
arduino-uno
add a comment |
up vote
2
down vote
favorite
im trying to switch number on the display using a
potentiometer, it is just counting from 0/9.
All help is much appreciated!
int SegmentA = 6;
int SegmentB = 7;
int SegmentC = 8;
int SegmentD = 9;
int SegmentE = 10;
int SegmentF = 11;
int SegmentG = 12;
const int PP = A0;
void setup()
{
pinMode (SegmentA, OUTPUT);
pinMode (SegmentB, OUTPUT);
pinMode (SegmentC, OUTPUT);
pinMode (SegmentD, OUTPUT);
pinMode (SegmentE, OUTPUT);
pinMode (SegmentF, OUTPUT);
pinMode (SegmentG, OUTPUT);
}
void loop() {
int POD = analogRead(PP);
int SM = map(POD, 0, 1023, 0 , 8);
switch (SM) {
case 0:
digitalWrite (SegmentA, HIGH);
digitalWrite (SegmentB, HIGH);
digitalWrite (SegmentC, HIGH);
digitalWrite (SegmentD, HIGH);
digitalWrite (SegmentE, HIGH); //0
digitalWrite (SegmentF, HIGH);
digitalWrite (SegmentG, LOW);
delay(1000);
case 1:
digitalWrite (SegmentA, LOW);
digitalWrite (SegmentB, HIGH);
digitalWrite (SegmentC, HIGH);
digitalWrite (SegmentD, LOW);
digitalWrite (SegmentE, LOW); //1
digitalWrite (SegmentF, LOW);
digitalWrite (SegmentG, LOW);
delay(1000);
case 2:
digitalWrite (SegmentA, HIGH);
digitalWrite (SegmentB, HIGH);
digitalWrite (SegmentC, LOW);
digitalWrite (SegmentD, HIGH);
digitalWrite (SegmentE, HIGH); //2
digitalWrite (SegmentF, LOW);
digitalWrite (SegmentG, HIGH);
delay(1000);
case 3:
digitalWrite (SegmentA, HIGH);
digitalWrite (SegmentB, HIGH);
digitalWrite (SegmentC, HIGH);
digitalWrite (SegmentD, HIGH);
digitalWrite (SegmentE, LOW); //3
digitalWrite (SegmentF, LOW);
digitalWrite (SegmentG, HIGH);
delay(1000);
case 4:
digitalWrite (SegmentA, LOW);
digitalWrite (SegmentB, HIGH);
digitalWrite (SegmentC, HIGH);
digitalWrite (SegmentD, LOW);
digitalWrite (SegmentE, LOW); //4
digitalWrite (SegmentF, HIGH);
digitalWrite (SegmentG, HIGH);
delay(1000);
case 5:
digitalWrite (SegmentA, HIGH);
digitalWrite (SegmentB, LOW);
digitalWrite (SegmentC, HIGH);
digitalWrite (SegmentD, HIGH);
digitalWrite (SegmentE, LOW); //5
digitalWrite (SegmentF, HIGH);
digitalWrite (SegmentG, HIGH);
delay(1000);
case 6:
digitalWrite (SegmentA, HIGH);
digitalWrite (SegmentB, LOW);
digitalWrite (SegmentC, HIGH);
digitalWrite (SegmentD, HIGH);
digitalWrite (SegmentE, HIGH); //6
digitalWrite (SegmentF, HIGH);
digitalWrite (SegmentG, HIGH);
delay(1000);
case 7:
digitalWrite (SegmentA, HIGH);
digitalWrite (SegmentB, HIGH);
digitalWrite (SegmentC, HIGH);
digitalWrite (SegmentD, LOW);
digitalWrite (SegmentE, LOW); //7
digitalWrite (SegmentF, LOW);
digitalWrite (SegmentG, LOW);
delay(1000);
case 8:
digitalWrite (SegmentA, HIGH);
digitalWrite (SegmentB, HIGH);
digitalWrite (SegmentC, HIGH);
digitalWrite (SegmentD, HIGH);
digitalWrite (SegmentE, HIGH); //8
digitalWrite (SegmentF, HIGH);
digitalWrite (SegmentG, HIGH);
}
}
arduino-uno
1
I would shorten the delay (1000) to be much less - otherwise you turn the pot, nothing happens, you turn it some and finally something happens but now you've overshot what you wanted, and you overdo it the other way trying to get to where you wanted. Try a delay of 50 to 100, have it be much more reactive to turning the knob. You can then move on to using a rotary decoder with detents, one click for one number change. Here's a poorly lit example with 2 digits and rotary encoder. In this case, a shift register was used to drive each display (vs multiplexing).
– CrossRoads
Nov 13 at 17:53
youtube.com/watch?v=f51eSlcZt-g The link.
– CrossRoads
Nov 13 at 17:54
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
im trying to switch number on the display using a
potentiometer, it is just counting from 0/9.
All help is much appreciated!
int SegmentA = 6;
int SegmentB = 7;
int SegmentC = 8;
int SegmentD = 9;
int SegmentE = 10;
int SegmentF = 11;
int SegmentG = 12;
const int PP = A0;
void setup()
{
pinMode (SegmentA, OUTPUT);
pinMode (SegmentB, OUTPUT);
pinMode (SegmentC, OUTPUT);
pinMode (SegmentD, OUTPUT);
pinMode (SegmentE, OUTPUT);
pinMode (SegmentF, OUTPUT);
pinMode (SegmentG, OUTPUT);
}
void loop() {
int POD = analogRead(PP);
int SM = map(POD, 0, 1023, 0 , 8);
switch (SM) {
case 0:
digitalWrite (SegmentA, HIGH);
digitalWrite (SegmentB, HIGH);
digitalWrite (SegmentC, HIGH);
digitalWrite (SegmentD, HIGH);
digitalWrite (SegmentE, HIGH); //0
digitalWrite (SegmentF, HIGH);
digitalWrite (SegmentG, LOW);
delay(1000);
case 1:
digitalWrite (SegmentA, LOW);
digitalWrite (SegmentB, HIGH);
digitalWrite (SegmentC, HIGH);
digitalWrite (SegmentD, LOW);
digitalWrite (SegmentE, LOW); //1
digitalWrite (SegmentF, LOW);
digitalWrite (SegmentG, LOW);
delay(1000);
case 2:
digitalWrite (SegmentA, HIGH);
digitalWrite (SegmentB, HIGH);
digitalWrite (SegmentC, LOW);
digitalWrite (SegmentD, HIGH);
digitalWrite (SegmentE, HIGH); //2
digitalWrite (SegmentF, LOW);
digitalWrite (SegmentG, HIGH);
delay(1000);
case 3:
digitalWrite (SegmentA, HIGH);
digitalWrite (SegmentB, HIGH);
digitalWrite (SegmentC, HIGH);
digitalWrite (SegmentD, HIGH);
digitalWrite (SegmentE, LOW); //3
digitalWrite (SegmentF, LOW);
digitalWrite (SegmentG, HIGH);
delay(1000);
case 4:
digitalWrite (SegmentA, LOW);
digitalWrite (SegmentB, HIGH);
digitalWrite (SegmentC, HIGH);
digitalWrite (SegmentD, LOW);
digitalWrite (SegmentE, LOW); //4
digitalWrite (SegmentF, HIGH);
digitalWrite (SegmentG, HIGH);
delay(1000);
case 5:
digitalWrite (SegmentA, HIGH);
digitalWrite (SegmentB, LOW);
digitalWrite (SegmentC, HIGH);
digitalWrite (SegmentD, HIGH);
digitalWrite (SegmentE, LOW); //5
digitalWrite (SegmentF, HIGH);
digitalWrite (SegmentG, HIGH);
delay(1000);
case 6:
digitalWrite (SegmentA, HIGH);
digitalWrite (SegmentB, LOW);
digitalWrite (SegmentC, HIGH);
digitalWrite (SegmentD, HIGH);
digitalWrite (SegmentE, HIGH); //6
digitalWrite (SegmentF, HIGH);
digitalWrite (SegmentG, HIGH);
delay(1000);
case 7:
digitalWrite (SegmentA, HIGH);
digitalWrite (SegmentB, HIGH);
digitalWrite (SegmentC, HIGH);
digitalWrite (SegmentD, LOW);
digitalWrite (SegmentE, LOW); //7
digitalWrite (SegmentF, LOW);
digitalWrite (SegmentG, LOW);
delay(1000);
case 8:
digitalWrite (SegmentA, HIGH);
digitalWrite (SegmentB, HIGH);
digitalWrite (SegmentC, HIGH);
digitalWrite (SegmentD, HIGH);
digitalWrite (SegmentE, HIGH); //8
digitalWrite (SegmentF, HIGH);
digitalWrite (SegmentG, HIGH);
}
}
arduino-uno
im trying to switch number on the display using a
potentiometer, it is just counting from 0/9.
All help is much appreciated!
int SegmentA = 6;
int SegmentB = 7;
int SegmentC = 8;
int SegmentD = 9;
int SegmentE = 10;
int SegmentF = 11;
int SegmentG = 12;
const int PP = A0;
void setup()
{
pinMode (SegmentA, OUTPUT);
pinMode (SegmentB, OUTPUT);
pinMode (SegmentC, OUTPUT);
pinMode (SegmentD, OUTPUT);
pinMode (SegmentE, OUTPUT);
pinMode (SegmentF, OUTPUT);
pinMode (SegmentG, OUTPUT);
}
void loop() {
int POD = analogRead(PP);
int SM = map(POD, 0, 1023, 0 , 8);
switch (SM) {
case 0:
digitalWrite (SegmentA, HIGH);
digitalWrite (SegmentB, HIGH);
digitalWrite (SegmentC, HIGH);
digitalWrite (SegmentD, HIGH);
digitalWrite (SegmentE, HIGH); //0
digitalWrite (SegmentF, HIGH);
digitalWrite (SegmentG, LOW);
delay(1000);
case 1:
digitalWrite (SegmentA, LOW);
digitalWrite (SegmentB, HIGH);
digitalWrite (SegmentC, HIGH);
digitalWrite (SegmentD, LOW);
digitalWrite (SegmentE, LOW); //1
digitalWrite (SegmentF, LOW);
digitalWrite (SegmentG, LOW);
delay(1000);
case 2:
digitalWrite (SegmentA, HIGH);
digitalWrite (SegmentB, HIGH);
digitalWrite (SegmentC, LOW);
digitalWrite (SegmentD, HIGH);
digitalWrite (SegmentE, HIGH); //2
digitalWrite (SegmentF, LOW);
digitalWrite (SegmentG, HIGH);
delay(1000);
case 3:
digitalWrite (SegmentA, HIGH);
digitalWrite (SegmentB, HIGH);
digitalWrite (SegmentC, HIGH);
digitalWrite (SegmentD, HIGH);
digitalWrite (SegmentE, LOW); //3
digitalWrite (SegmentF, LOW);
digitalWrite (SegmentG, HIGH);
delay(1000);
case 4:
digitalWrite (SegmentA, LOW);
digitalWrite (SegmentB, HIGH);
digitalWrite (SegmentC, HIGH);
digitalWrite (SegmentD, LOW);
digitalWrite (SegmentE, LOW); //4
digitalWrite (SegmentF, HIGH);
digitalWrite (SegmentG, HIGH);
delay(1000);
case 5:
digitalWrite (SegmentA, HIGH);
digitalWrite (SegmentB, LOW);
digitalWrite (SegmentC, HIGH);
digitalWrite (SegmentD, HIGH);
digitalWrite (SegmentE, LOW); //5
digitalWrite (SegmentF, HIGH);
digitalWrite (SegmentG, HIGH);
delay(1000);
case 6:
digitalWrite (SegmentA, HIGH);
digitalWrite (SegmentB, LOW);
digitalWrite (SegmentC, HIGH);
digitalWrite (SegmentD, HIGH);
digitalWrite (SegmentE, HIGH); //6
digitalWrite (SegmentF, HIGH);
digitalWrite (SegmentG, HIGH);
delay(1000);
case 7:
digitalWrite (SegmentA, HIGH);
digitalWrite (SegmentB, HIGH);
digitalWrite (SegmentC, HIGH);
digitalWrite (SegmentD, LOW);
digitalWrite (SegmentE, LOW); //7
digitalWrite (SegmentF, LOW);
digitalWrite (SegmentG, LOW);
delay(1000);
case 8:
digitalWrite (SegmentA, HIGH);
digitalWrite (SegmentB, HIGH);
digitalWrite (SegmentC, HIGH);
digitalWrite (SegmentD, HIGH);
digitalWrite (SegmentE, HIGH); //8
digitalWrite (SegmentF, HIGH);
digitalWrite (SegmentG, HIGH);
}
}
arduino-uno
arduino-uno
edited Nov 13 at 17:39
JRobert
9,60811036
9,60811036
asked Nov 13 at 17:12
user51180
132
132
1
I would shorten the delay (1000) to be much less - otherwise you turn the pot, nothing happens, you turn it some and finally something happens but now you've overshot what you wanted, and you overdo it the other way trying to get to where you wanted. Try a delay of 50 to 100, have it be much more reactive to turning the knob. You can then move on to using a rotary decoder with detents, one click for one number change. Here's a poorly lit example with 2 digits and rotary encoder. In this case, a shift register was used to drive each display (vs multiplexing).
– CrossRoads
Nov 13 at 17:53
youtube.com/watch?v=f51eSlcZt-g The link.
– CrossRoads
Nov 13 at 17:54
add a comment |
1
I would shorten the delay (1000) to be much less - otherwise you turn the pot, nothing happens, you turn it some and finally something happens but now you've overshot what you wanted, and you overdo it the other way trying to get to where you wanted. Try a delay of 50 to 100, have it be much more reactive to turning the knob. You can then move on to using a rotary decoder with detents, one click for one number change. Here's a poorly lit example with 2 digits and rotary encoder. In this case, a shift register was used to drive each display (vs multiplexing).
– CrossRoads
Nov 13 at 17:53
youtube.com/watch?v=f51eSlcZt-g The link.
– CrossRoads
Nov 13 at 17:54
1
1
I would shorten the delay (1000) to be much less - otherwise you turn the pot, nothing happens, you turn it some and finally something happens but now you've overshot what you wanted, and you overdo it the other way trying to get to where you wanted. Try a delay of 50 to 100, have it be much more reactive to turning the knob. You can then move on to using a rotary decoder with detents, one click for one number change. Here's a poorly lit example with 2 digits and rotary encoder. In this case, a shift register was used to drive each display (vs multiplexing).
– CrossRoads
Nov 13 at 17:53
I would shorten the delay (1000) to be much less - otherwise you turn the pot, nothing happens, you turn it some and finally something happens but now you've overshot what you wanted, and you overdo it the other way trying to get to where you wanted. Try a delay of 50 to 100, have it be much more reactive to turning the knob. You can then move on to using a rotary decoder with detents, one click for one number change. Here's a poorly lit example with 2 digits and rotary encoder. In this case, a shift register was used to drive each display (vs multiplexing).
– CrossRoads
Nov 13 at 17:53
youtube.com/watch?v=f51eSlcZt-g The link.
– CrossRoads
Nov 13 at 17:54
youtube.com/watch?v=f51eSlcZt-g The link.
– CrossRoads
Nov 13 at 17:54
add a comment |
2 Answers
2
active
oldest
votes
up vote
9
down vote
accepted
You miss the break statement at the end of each case. e.g.:
digitalWrite (SegmentF, LOW);
digitalWrite (SegmentG, HIGH);
delay(1000);
break;
case 4:
This causes each case to perform, and continue with the next case. However, since there is a delay of 1 second, it looks like it is counting.
Actually, I would expect it counts from a certain value depending on the pot meter to 7, since there is no digit 9 implemented, and the delay for the 8 digit is missing too.
To further improve your code, you can add the delay not in every case but at the end, since I assume there always will be one digit selected.
Also you can make a function void showDigit with a parameter for each of the possible segment states and replace all (almost) duplicated code by that function call, or send the digit and use showDigit to set internally the states of each segment (by using a boolean array[nr_of_digits][nr_of_segments], somewhat like Nic Hartley proposes below.
Thank you, this fixes my problem!
– user51180
Nov 13 at 18:39
2
I would add that you don't even need thatswitch
-- you can easily have aconst bool digits[7 * 10]
, then passdigits[7 * digit]
, and in the function pull fromparam[0]
,param[1]
, etc.
– Nic Hartley
Nov 13 at 21:42
@NicHartley Thanks ... I think a two dimensional array is than even better, but the use of an array is better in general.
– Michel Keijzers
Nov 14 at 9:10
1
Actually, strictly speaking, you could also do aconst uint8_t digits[10];
, then access the individual segments withdigits[n] & (1 << segment)
, if you wanted it to be the least readable possible code. I wouldn't recommend that over the array (probably 2D array) approach, though, unless you have some extremely tight memory requirements.
– Nic Hartley
Nov 14 at 17:54
1
@MichelKeijzers I suggested it only for the very rare circumstances where you find yourself exhausting all 2KB and need to compact things down even more. It's definitely not a good thing to start with, I agree.
– Nic Hartley
Nov 14 at 21:08
|
show 5 more comments
up vote
3
down vote
You'll want a single delay (if any) at the bottom of the loop() function. You may not need it since it will keep displaying the current digit until you move the potentiometer. Exception: if the pot is near the edge between 2 digits, you could see some dithering.
Replace the delay-calls in the case
s with break;
statements. Otherwise whichever case you call will fall through into the case below it, and so on, displaying subsequent each digit in turn, which isn't what you wanted (and is what you are already seeing).
Change the map() call to map(POD, 0, 1024, 0 , 10)
. Otherwise there will be no '9' band on potentiometer and the '8' band will be only 1 value wide (out of 1023): exactly the value "1023". You'd like the '9' band to be about as wide as all of the others. It's even possible that a given potentiometer won't get close enough to zero resistance for you to get a 1023 reading.
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
9
down vote
accepted
You miss the break statement at the end of each case. e.g.:
digitalWrite (SegmentF, LOW);
digitalWrite (SegmentG, HIGH);
delay(1000);
break;
case 4:
This causes each case to perform, and continue with the next case. However, since there is a delay of 1 second, it looks like it is counting.
Actually, I would expect it counts from a certain value depending on the pot meter to 7, since there is no digit 9 implemented, and the delay for the 8 digit is missing too.
To further improve your code, you can add the delay not in every case but at the end, since I assume there always will be one digit selected.
Also you can make a function void showDigit with a parameter for each of the possible segment states and replace all (almost) duplicated code by that function call, or send the digit and use showDigit to set internally the states of each segment (by using a boolean array[nr_of_digits][nr_of_segments], somewhat like Nic Hartley proposes below.
Thank you, this fixes my problem!
– user51180
Nov 13 at 18:39
2
I would add that you don't even need thatswitch
-- you can easily have aconst bool digits[7 * 10]
, then passdigits[7 * digit]
, and in the function pull fromparam[0]
,param[1]
, etc.
– Nic Hartley
Nov 13 at 21:42
@NicHartley Thanks ... I think a two dimensional array is than even better, but the use of an array is better in general.
– Michel Keijzers
Nov 14 at 9:10
1
Actually, strictly speaking, you could also do aconst uint8_t digits[10];
, then access the individual segments withdigits[n] & (1 << segment)
, if you wanted it to be the least readable possible code. I wouldn't recommend that over the array (probably 2D array) approach, though, unless you have some extremely tight memory requirements.
– Nic Hartley
Nov 14 at 17:54
1
@MichelKeijzers I suggested it only for the very rare circumstances where you find yourself exhausting all 2KB and need to compact things down even more. It's definitely not a good thing to start with, I agree.
– Nic Hartley
Nov 14 at 21:08
|
show 5 more comments
up vote
9
down vote
accepted
You miss the break statement at the end of each case. e.g.:
digitalWrite (SegmentF, LOW);
digitalWrite (SegmentG, HIGH);
delay(1000);
break;
case 4:
This causes each case to perform, and continue with the next case. However, since there is a delay of 1 second, it looks like it is counting.
Actually, I would expect it counts from a certain value depending on the pot meter to 7, since there is no digit 9 implemented, and the delay for the 8 digit is missing too.
To further improve your code, you can add the delay not in every case but at the end, since I assume there always will be one digit selected.
Also you can make a function void showDigit with a parameter for each of the possible segment states and replace all (almost) duplicated code by that function call, or send the digit and use showDigit to set internally the states of each segment (by using a boolean array[nr_of_digits][nr_of_segments], somewhat like Nic Hartley proposes below.
Thank you, this fixes my problem!
– user51180
Nov 13 at 18:39
2
I would add that you don't even need thatswitch
-- you can easily have aconst bool digits[7 * 10]
, then passdigits[7 * digit]
, and in the function pull fromparam[0]
,param[1]
, etc.
– Nic Hartley
Nov 13 at 21:42
@NicHartley Thanks ... I think a two dimensional array is than even better, but the use of an array is better in general.
– Michel Keijzers
Nov 14 at 9:10
1
Actually, strictly speaking, you could also do aconst uint8_t digits[10];
, then access the individual segments withdigits[n] & (1 << segment)
, if you wanted it to be the least readable possible code. I wouldn't recommend that over the array (probably 2D array) approach, though, unless you have some extremely tight memory requirements.
– Nic Hartley
Nov 14 at 17:54
1
@MichelKeijzers I suggested it only for the very rare circumstances where you find yourself exhausting all 2KB and need to compact things down even more. It's definitely not a good thing to start with, I agree.
– Nic Hartley
Nov 14 at 21:08
|
show 5 more comments
up vote
9
down vote
accepted
up vote
9
down vote
accepted
You miss the break statement at the end of each case. e.g.:
digitalWrite (SegmentF, LOW);
digitalWrite (SegmentG, HIGH);
delay(1000);
break;
case 4:
This causes each case to perform, and continue with the next case. However, since there is a delay of 1 second, it looks like it is counting.
Actually, I would expect it counts from a certain value depending on the pot meter to 7, since there is no digit 9 implemented, and the delay for the 8 digit is missing too.
To further improve your code, you can add the delay not in every case but at the end, since I assume there always will be one digit selected.
Also you can make a function void showDigit with a parameter for each of the possible segment states and replace all (almost) duplicated code by that function call, or send the digit and use showDigit to set internally the states of each segment (by using a boolean array[nr_of_digits][nr_of_segments], somewhat like Nic Hartley proposes below.
You miss the break statement at the end of each case. e.g.:
digitalWrite (SegmentF, LOW);
digitalWrite (SegmentG, HIGH);
delay(1000);
break;
case 4:
This causes each case to perform, and continue with the next case. However, since there is a delay of 1 second, it looks like it is counting.
Actually, I would expect it counts from a certain value depending on the pot meter to 7, since there is no digit 9 implemented, and the delay for the 8 digit is missing too.
To further improve your code, you can add the delay not in every case but at the end, since I assume there always will be one digit selected.
Also you can make a function void showDigit with a parameter for each of the possible segment states and replace all (almost) duplicated code by that function call, or send the digit and use showDigit to set internally the states of each segment (by using a boolean array[nr_of_digits][nr_of_segments], somewhat like Nic Hartley proposes below.
edited Nov 14 at 9:09
answered Nov 13 at 17:38
Michel Keijzers
6,17341736
6,17341736
Thank you, this fixes my problem!
– user51180
Nov 13 at 18:39
2
I would add that you don't even need thatswitch
-- you can easily have aconst bool digits[7 * 10]
, then passdigits[7 * digit]
, and in the function pull fromparam[0]
,param[1]
, etc.
– Nic Hartley
Nov 13 at 21:42
@NicHartley Thanks ... I think a two dimensional array is than even better, but the use of an array is better in general.
– Michel Keijzers
Nov 14 at 9:10
1
Actually, strictly speaking, you could also do aconst uint8_t digits[10];
, then access the individual segments withdigits[n] & (1 << segment)
, if you wanted it to be the least readable possible code. I wouldn't recommend that over the array (probably 2D array) approach, though, unless you have some extremely tight memory requirements.
– Nic Hartley
Nov 14 at 17:54
1
@MichelKeijzers I suggested it only for the very rare circumstances where you find yourself exhausting all 2KB and need to compact things down even more. It's definitely not a good thing to start with, I agree.
– Nic Hartley
Nov 14 at 21:08
|
show 5 more comments
Thank you, this fixes my problem!
– user51180
Nov 13 at 18:39
2
I would add that you don't even need thatswitch
-- you can easily have aconst bool digits[7 * 10]
, then passdigits[7 * digit]
, and in the function pull fromparam[0]
,param[1]
, etc.
– Nic Hartley
Nov 13 at 21:42
@NicHartley Thanks ... I think a two dimensional array is than even better, but the use of an array is better in general.
– Michel Keijzers
Nov 14 at 9:10
1
Actually, strictly speaking, you could also do aconst uint8_t digits[10];
, then access the individual segments withdigits[n] & (1 << segment)
, if you wanted it to be the least readable possible code. I wouldn't recommend that over the array (probably 2D array) approach, though, unless you have some extremely tight memory requirements.
– Nic Hartley
Nov 14 at 17:54
1
@MichelKeijzers I suggested it only for the very rare circumstances where you find yourself exhausting all 2KB and need to compact things down even more. It's definitely not a good thing to start with, I agree.
– Nic Hartley
Nov 14 at 21:08
Thank you, this fixes my problem!
– user51180
Nov 13 at 18:39
Thank you, this fixes my problem!
– user51180
Nov 13 at 18:39
2
2
I would add that you don't even need that
switch
-- you can easily have a const bool digits[7 * 10]
, then pass digits[7 * digit]
, and in the function pull from param[0]
, param[1]
, etc.– Nic Hartley
Nov 13 at 21:42
I would add that you don't even need that
switch
-- you can easily have a const bool digits[7 * 10]
, then pass digits[7 * digit]
, and in the function pull from param[0]
, param[1]
, etc.– Nic Hartley
Nov 13 at 21:42
@NicHartley Thanks ... I think a two dimensional array is than even better, but the use of an array is better in general.
– Michel Keijzers
Nov 14 at 9:10
@NicHartley Thanks ... I think a two dimensional array is than even better, but the use of an array is better in general.
– Michel Keijzers
Nov 14 at 9:10
1
1
Actually, strictly speaking, you could also do a
const uint8_t digits[10];
, then access the individual segments with digits[n] & (1 << segment)
, if you wanted it to be the least readable possible code. I wouldn't recommend that over the array (probably 2D array) approach, though, unless you have some extremely tight memory requirements.– Nic Hartley
Nov 14 at 17:54
Actually, strictly speaking, you could also do a
const uint8_t digits[10];
, then access the individual segments with digits[n] & (1 << segment)
, if you wanted it to be the least readable possible code. I wouldn't recommend that over the array (probably 2D array) approach, though, unless you have some extremely tight memory requirements.– Nic Hartley
Nov 14 at 17:54
1
1
@MichelKeijzers I suggested it only for the very rare circumstances where you find yourself exhausting all 2KB and need to compact things down even more. It's definitely not a good thing to start with, I agree.
– Nic Hartley
Nov 14 at 21:08
@MichelKeijzers I suggested it only for the very rare circumstances where you find yourself exhausting all 2KB and need to compact things down even more. It's definitely not a good thing to start with, I agree.
– Nic Hartley
Nov 14 at 21:08
|
show 5 more comments
up vote
3
down vote
You'll want a single delay (if any) at the bottom of the loop() function. You may not need it since it will keep displaying the current digit until you move the potentiometer. Exception: if the pot is near the edge between 2 digits, you could see some dithering.
Replace the delay-calls in the case
s with break;
statements. Otherwise whichever case you call will fall through into the case below it, and so on, displaying subsequent each digit in turn, which isn't what you wanted (and is what you are already seeing).
Change the map() call to map(POD, 0, 1024, 0 , 10)
. Otherwise there will be no '9' band on potentiometer and the '8' band will be only 1 value wide (out of 1023): exactly the value "1023". You'd like the '9' band to be about as wide as all of the others. It's even possible that a given potentiometer won't get close enough to zero resistance for you to get a 1023 reading.
add a comment |
up vote
3
down vote
You'll want a single delay (if any) at the bottom of the loop() function. You may not need it since it will keep displaying the current digit until you move the potentiometer. Exception: if the pot is near the edge between 2 digits, you could see some dithering.
Replace the delay-calls in the case
s with break;
statements. Otherwise whichever case you call will fall through into the case below it, and so on, displaying subsequent each digit in turn, which isn't what you wanted (and is what you are already seeing).
Change the map() call to map(POD, 0, 1024, 0 , 10)
. Otherwise there will be no '9' band on potentiometer and the '8' band will be only 1 value wide (out of 1023): exactly the value "1023". You'd like the '9' band to be about as wide as all of the others. It's even possible that a given potentiometer won't get close enough to zero resistance for you to get a 1023 reading.
add a comment |
up vote
3
down vote
up vote
3
down vote
You'll want a single delay (if any) at the bottom of the loop() function. You may not need it since it will keep displaying the current digit until you move the potentiometer. Exception: if the pot is near the edge between 2 digits, you could see some dithering.
Replace the delay-calls in the case
s with break;
statements. Otherwise whichever case you call will fall through into the case below it, and so on, displaying subsequent each digit in turn, which isn't what you wanted (and is what you are already seeing).
Change the map() call to map(POD, 0, 1024, 0 , 10)
. Otherwise there will be no '9' band on potentiometer and the '8' band will be only 1 value wide (out of 1023): exactly the value "1023". You'd like the '9' band to be about as wide as all of the others. It's even possible that a given potentiometer won't get close enough to zero resistance for you to get a 1023 reading.
You'll want a single delay (if any) at the bottom of the loop() function. You may not need it since it will keep displaying the current digit until you move the potentiometer. Exception: if the pot is near the edge between 2 digits, you could see some dithering.
Replace the delay-calls in the case
s with break;
statements. Otherwise whichever case you call will fall through into the case below it, and so on, displaying subsequent each digit in turn, which isn't what you wanted (and is what you are already seeing).
Change the map() call to map(POD, 0, 1024, 0 , 10)
. Otherwise there will be no '9' band on potentiometer and the '8' band will be only 1 value wide (out of 1023): exactly the value "1023". You'd like the '9' band to be about as wide as all of the others. It's even possible that a given potentiometer won't get close enough to zero resistance for you to get a 1023 reading.
answered Nov 13 at 18:00
JRobert
9,60811036
9,60811036
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2farduino.stackexchange.com%2fquestions%2f57787%2ftrying-to-use-a-7-segment-display-to-count-0-9-with-a-potentiometer%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
I would shorten the delay (1000) to be much less - otherwise you turn the pot, nothing happens, you turn it some and finally something happens but now you've overshot what you wanted, and you overdo it the other way trying to get to where you wanted. Try a delay of 50 to 100, have it be much more reactive to turning the knob. You can then move on to using a rotary decoder with detents, one click for one number change. Here's a poorly lit example with 2 digits and rotary encoder. In this case, a shift register was used to drive each display (vs multiplexing).
– CrossRoads
Nov 13 at 17:53
youtube.com/watch?v=f51eSlcZt-g The link.
– CrossRoads
Nov 13 at 17:54