Offline
Newbie
Karma: 0
Posts: 13
|
 |
« on: December 24, 2012, 09:36:03 pm » |
Hello people! first of all a merry Christmas for every body! So i have made something for a 7segement display But i like to made in a compact way, i think, that is a array. here is my code: ps 'segmentlightModeP1' is not here declared in this scope !!!attention!!! greetings frederic code: // this is voor the bit const int Bin1 = 5; // These are all identifying the binary inputs of const int Bin2 = 6; // the 4511 Seven Segment Decoder const int Bin3 = 7;// const int Bin4 = 8;//
void setup() {
pinMode(Bin1, OUTPUT); // sets up binary output one as a digital output pinMode(Bin2, OUTPUT); //and so on... pinMode(Bin3, OUTPUT); pinMode(Bin4, OUTPUT);
}
void loop(){
//teller naar 4bit brengen if (segmentlightModeP1 == 0) { digitalWrite(Bin1, 0); //Write "0" to the display digitalWrite(Bin2, 0); digitalWrite(Bin3, 0); digitalWrite(Bin4, 0); }else{ if (segmentlightModeP1 == 1) { digitalWrite(Bin1, 1); //Write "1" to the display digitalWrite(Bin2, 0); digitalWrite(Bin3, 0); digitalWrite(Bin4, 0); }else{ if (segmentlightModeP1 == 2){ digitalWrite(Bin1, 0); //Write "2" to the display digitalWrite(Bin2, 1); digitalWrite(Bin3, 0); digitalWrite(Bin4, 0); }else{ if (segmentlightModeP1 == 3) { digitalWrite(Bin1, 1); //Write "3" to the display digitalWrite(Bin2, 1); digitalWrite(Bin3, 0); digitalWrite(Bin4, 0); } else { if (segmentlightModeP1 == 4) { digitalWrite(Bin1, 0); //Write "4" to the display digitalWrite(Bin2, 0); digitalWrite(Bin3, 1); digitalWrite(Bin4, 0); } else{ if (segmentlightModeP1 == 5) { digitalWrite(Bin1, 1); //Write "5" to the display digitalWrite(Bin2, 0); digitalWrite(Bin3, 1); digitalWrite(Bin4, 0); } else { if (segmentlightModeP1 == 6) { digitalWrite(Bin1, 0); //Write "6" to the display digitalWrite(Bin2, 1); digitalWrite(Bin3, 1); digitalWrite(Bin4, 0); }else { if (segmentlightModeP1 == 7) { digitalWrite(Bin1, 1); //Write "7" to the display digitalWrite(Bin2, 1); digitalWrite(Bin3, 1); digitalWrite(Bin4, 0); } else{ if (segmentlightModeP1 == 8) { digitalWrite(Bin1, 0); //Write "8" to the display digitalWrite(Bin2, 0); digitalWrite(Bin3, 0); digitalWrite(Bin4, 1); } else{ if (segmentlightModeP1 == 9) { digitalWrite(Bin1, 1); //Write "9" to the display digitalWrite(Bin2, 0); digitalWrite(Bin3, 0); digitalWrite(Bin4, 1); } }}}}}}}}}}
|
|
|
|
|
Logged
|
|
|
|
|
Massachusetts, USA
Offline
Tesla Member
Karma: 96
Posts: 6364
|
 |
« Reply #1 on: December 24, 2012, 09:40:28 pm » |
digitalWrite(Bin1, segmentlightModeP1 & 0b0001); digitalWrite(Bin2, segmentlightModeP1 & 0b0010); digitalWrite(Bin3, segmentlightModeP1 & 0b0100); digitalWrite(Bin4, segmentlightModeP1 & 0b1000);
|
|
|
|
|
Logged
|
|
|
|
|
Des Moines, WA - USA
Offline
God Member
Karma: 21
Posts: 703
|
 |
« Reply #2 on: December 24, 2012, 10:49:10 pm » |
Your code doesn't seem to match your stated goal. Rephrasing your posted code - #define ELEMENT_COUNT(ARRAY) (sizeof(ARRAY) / sizeof(ARRAY[0]))
const uint8_t pinBin1 = 5; const uint8_t pinBin2 = 6; const uint8_t pinBin3 = 7; const uint8_t pinBin4 = 8;
const uint8_t pins[] = { pinBin4, pinBin3, pinBin2, pinBin1 };
uint8_t segmentlightModeP1;
void loop() { for ( uint8_t * p = (uint8_t*)pins, mask = 0b00001000; mask; mask >>= 1 ) { digitalWrite(*p++, ((segmentlightModeP1 & mask) ? HIGH : LOW)); } }
void setup() { for ( int i = ELEMENT_COUNT(pins); i--; ) { pinMode(pins[i], OUTPUT); } }
|
|
|
|
« Last Edit: December 24, 2012, 11:10:42 pm by lloyddean »
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 13
|
 |
« Reply #3 on: December 25, 2012, 04:05:23 pm » |
Oo
ok , can somebody explain this?
|
|
|
|
|
Logged
|
|
|
|
|
Des Moines, WA - USA
Offline
God Member
Karma: 21
Posts: 703
|
 |
« Reply #4 on: December 25, 2012, 05:03:09 pm » |
What dont't you understand?
This examples make use of arrays, loops and pointers.
Will answer specific questions when I get home.
|
|
|
|
|
Logged
|
|
|
|
|
nr Bundaberg, Australia
Offline
Tesla Member
Karma: 71
Posts: 6815
Scattered showers my arse -- Noah, 2348BC.
|
 |
« Reply #5 on: December 25, 2012, 06:38:53 pm » |
for ( uint8_t * p = (uint8_t*)pins, mask = 0b00001000; mask; mask >>= 1 ) uint8_t * p = (uint8_t*)pins, // set a pointer to the pins array mask; // do until mask == 0 mask >>= 1 // right shift mask, so the loop will run 4 times digitalWrite(*p++, ((segmentlightModeP1 & mask) ? HIGH : LOW)); *p++ // get a value from the pins array to tell digitalWrite what pin to use, increment to the next array value (segmentlightModeP1 & mask) ? HIGH : LOW) // if the mask bit in segmentlightModeP1 is 1 set pin HIGH, else set pin LOW (look up the ?: ternary operator) ______ Rob
|
|
|
|
« Last Edit: December 25, 2012, 06:40:25 pm by Graynomad »
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 13
|
 |
« Reply #6 on: December 25, 2012, 08:04:24 pm » |
for ( uint8_t * p = (uint8_t*)pins, mask = 0b00001000; mask; mask >>= 1 ) uint8_t * p = (uint8_t*)pins, // set a pointer to the pins array mask; // do until mask == 0 mask >>= 1 // right shift mask, so the loop will run 4 times digitalWrite(*p++, ((segmentlightModeP1 & mask) ? HIGH : LOW)); *p++ // get a value from the pins array to tell digitalWrite what pin to use, increment to the next array value (segmentlightModeP1 & mask) ? HIGH : LOW) // if the mask bit in segmentlightModeP1 is 1 set pin HIGH, else set pin LOW (look up the ?: ternary operator) ______ Rob I work not so long in arduino.... is mask = to 0? what is uint8_t? 0b00001000 is this 8bits? (sizeof(ARRAY) / sizeof(ARRAY[0])) is this the name of it? what i see is it goos higher than 9 what i not want it must for 0 to 9
|
|
|
|
|
Logged
|
|
|
|
|
nr Bundaberg, Australia
Offline
Tesla Member
Karma: 71
Posts: 6815
Scattered showers my arse -- Noah, 2348BC.
|
 |
« Reply #7 on: December 25, 2012, 09:00:36 pm » |
is mask = to 0? At the end of the loop it is, as the loop iterates the mask values are 1000 0100 0010 0001 0000 The last value causes the loop to exit because the test was for ( uint8_t * p = (uint8_t*)pins, mask = 0b00001000; mask; mask >>= 1 ) which is a shorthand way of saying mask != 0 what is uint8_t? An unsigned integer 8 bits in size. It can therefore hold values from 0 to 0xFF (255). This format is the standard way of defining a variable in C, it's not used very often in Arduino world but arguably should be because there is no ambiguity. In this application "byte" or "int" will work as well but both those types are less portable to other architectures. 0b00001000 is this 8bits? Sort of, certainly it only defines 8 bits but if you assigned that value to an int another 8 0s would be added, for example byte b; int i; b = 0b00001000; i = 0b00001000; b = 0b00001000 or 8 dec. i = 0b0000000000001000 which is still 8 dec but it's been 0 padded to 16 bits. (sizeof(ARRAY) / sizeof(ARRAY[0])) This is the standard way of calculating the number of bytes in an array, ie number of elements / size of each element. So for these these two arrays int i[10]; byte b[10]; The value returned by that expression is 20 for the i array (because on an Arduino an int is two bytes in size) and 10 for the b array. In this case the word "ARRAY" is simply the name of the parameter in the macro, it has no special significance. what i see is it goos higher than 9 what i not want it must for 0 to 9 I think that lloyddean's code is correct so I suspect that has something to do with the rest of your code as we don't know what's happening to segmentlightModeP1 with this snippet. Can you post the entire code? ______ Rob
|
|
|
|
« Last Edit: December 25, 2012, 09:10:41 pm by Graynomad »
|
Logged
|
|
|
|
|
Des Moines, WA - USA
Offline
God Member
Karma: 21
Posts: 703
|
 |
« Reply #8 on: December 25, 2012, 10:05:04 pm » |
Thanks everyone for taking the time to explain that for me.
Again I must say that what you explained you wanted didn't match the code you posted so I simply posted an abbreviated version of what your code appeared to do.
Perhaps you could give us a better explanation of what you are doing?
For instance you make mention of a 7-segment display, upon which you wish to display what, the value of 'segmentlightModeP1'? Which probably hold values in the range of '0' - '9' and which you wish to convert to signals to turn on the appropriate segments of the 7-segment display in order to form the appropriate number.
Is this correct?
Please provide more explanation. As you can see people are will to help.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 13
|
 |
« Reply #9 on: December 26, 2012, 08:31:07 pm » |
Hello, thx altoe for helping me out, and with your patience
I try make a scoreboard with the arduino uno, for a football table what I have, I have 3 buttons, for player 1 and player 2 for the other is ofcourse a reset button. than i have 2 IC4511 drives BCD to 7 segment
this is my code, here we go :
|
|
|
|
|
Logged
|
|
|
|
|
Des Moines, WA - USA
Offline
God Member
Karma: 21
Posts: 703
|
 |
« Reply #10 on: December 26, 2012, 11:49:02 pm » |
this is my code
If that were the case you should've been able to cobble together the changes based upon my earlier example. My usual disclaimer - compiled but otherwise completely untested. /* State change detection (edge detection)
Often, you don't need to know the state of a digital input all the time, but you just need to know when the input changes from one state to another. For example, you want to know when a button goes from OFF to ON. This is called state change detection, or edge detection.
This example shows how to detect when a button or button changes from off to on and on to off.
The circuit: * drukknop1 is verbonden met pin 2 van +5V * weerstand van 10K verbonden met de andere kant van de drukknop ( pin 2 ) naar de massa ( pull down )
* drukknop2 is verbonden met pin 3 van +5V * weerstand van 10K verbonden met de andere kant van de drukknop ( pin 3 ) naar de massa ( pull down )
* drukknop3 is verbonden met pin 4 van +5V * weerstand van 10K verbonden met de andere kant van de drukknop ( pin 2 ) naar de massa ( pull down )
gemaakt op 25-11-2012 modified 25 nov 2012 by Federik also as This example code is in the public domain.
****KNOWN BUGS*** at the moment none
Basic : httpp://arduino.cc/en/Tutorial/ButtonStateChange
*/
// this constant won't change: const int buttonPin1 = 2; // the pin that the pushbutton is attached to const int buttonPin2 = 3; // the pin that the pushbutton is attached to const int buttonPin3 = 4; // the pin that the pushbutton is attached to
// this is for the bit > speler 1 // These are all identifying the binary inputs of the 4511 Seven Segment Decoder const int Bin1 = 5; const int Bin2 = 6; const int Bin3 = 7; const int Bin4 = 8;
// this is for the bit > speler2 // These are all identifying the binary inputs of the 4511 Seven Segment Decoder const int Bin5 = 9; const int Bin6 = 10; const int Bin7 = 11; const int Bin8 = 12;
const uint8_t pinsSpeler1[] = { Bin4, Bin3, Bin2, Bin1 }; const uint8_t pinsSpeler2[] = { Bin8, Bin7, Bin6, Bin5 };
// Variables will change: drukknop 1 int buttonPushCounter1 = 0; // counter for the number of button presses int buttonState1 = 0; // current state of the button int lastButtonState1 = 0; // previous state of the button int segmentlightModeP1 = 8;
// Variables will change2: drukknop2 int buttonPushCounter2 = 0; // counter for the number of button presses int buttonState2 = 0; // current state of the button int lastButtonState2 = 0; // previous state of the button int segmentlightModeP2 = 8;
//ariables will change2: drukknop3 ( reset ) int buttonPushCounter3 = 0; // counter for the number of button presses int buttonState3 = 0; // current state of the button int lastButtonState3 = 0; // previous state of the button
#define ELEMENT_COUNT(ARRAY) (sizeof(ARRAY) / sizeof(ARRAY[0]))
void setup() { // initialize the button pin as a input:
pinMode(buttonPin1, INPUT); pinMode(buttonPin2, INPUT); pinMode(buttonPin3, INPUT);
// speler1 & 2
for ( int i = ELEMENT_COUNT(pinsSpeler1); i--; ) { pinMode(pinsSpeler1[i], OUTPUT); pinMode(pinsSpeler2[i], OUTPUT); }
// initialize serial communication:
Serial.begin(9600); }
void speler1() { //Speler 1 // read the pushbutton input pin:
buttonState1 = digitalRead(buttonPin1);
// compare the buttonState to its previous state
if ( buttonState1 != lastButtonState1 ) { // if the state has changed, increment the counter
if ( buttonState1 == HIGH ) { // if the current state is HIGH then the button wend from off to on:
buttonPushCounter1++;
segmentlightModeP1 = buttonPushCounter1;// Het getal van de drukknop teller is gelijk aan segmentModeP1
Serial.println("ingedrukt"); Serial.print("Drukknop 1 is : "); Serial.print(buttonPushCounter1); Serial.println(" keer ingedrukt geweest"); Serial.println(segmentlightModeP1); } else { // if the current state is LOW then the button wend from on to off:
Serial.println("Drukknop 1 is gelost"); Serial.println(segmentlightModeP1); } }
// save the current state as the last state, for next time through the loop
lastButtonState1 = buttonState1; }
void speler2() //speler2 { buttonState2 = digitalRead(buttonPin2);
// compare the buttonState to its previous state
if ( buttonState2 != lastButtonState2 ) { // if the state has changed, increment the counter
if ( buttonState2 == HIGH ) { // if the current state is HIGH then the button wend from off to on:
buttonPushCounter2++;
segmentlightModeP2 = buttonPushCounter2;
Serial.println("ingedrukt"); Serial.print("Drukknop 2 is : "); Serial.print(buttonPushCounter2); Serial.println(" keer ingedrukt geweest"); Serial.println(segmentlightModeP2); } else { // if the current state is LOW then the button wend from on to off:
Serial.println("Drukknop 2 is gelost"); Serial.println(segmentlightModeP2); } }
// save the current state as the last state, for next time through the loop
lastButtonState2 = buttonState2; }
void reset() { // score resetten van speler 1 en 2 naar nul
buttonState3 = digitalRead(buttonPin3);
// compare the buttonState to its previous state
if ( buttonState3 != lastButtonState3 ) { // if the state has changed, increment the counter
if ( buttonState3 == HIGH ) { buttonPushCounter1 = 0; buttonPushCounter2 = 0;
Serial.println("Gereset"); Serial.print("De tellers van knop 1 staat op : "); Serial.println(buttonPushCounter1); Serial.print("De tellers van knop 2 staat op : "); Serial.println(buttonPushCounter2);
segmentlightModeP1 = buttonPushCounter1; segmentlightModeP2 = buttonPushCounter2; } else { // if the current state is LOW then the button wend from on to off:
Serial.println("Drukknop 3 is gelost"); } }
// save the current state as the last state, for next time through the loop
lastButtonState3 = buttonState3; }
void speler1bit() { // speler 2 // change the number to bits
for ( uint8_t * p = (uint8_t*)pinsSpeler1, mask = 0b00001000; mask; mask >>= 1 ) { digitalWrite(*p++, ((segmentlightModeP1 & mask) ? HIGH : LOW)); } }
void speler2bit() { // speler 2 // change the number to bits
for ( uint8_t * p = (uint8_t*)pinsSpeler2, mask = 0b00001000; mask; mask >>= 1 ) { digitalWrite(*p++, ((segmentlightModeP2 & mask) ? HIGH : LOW)); } }
void loop() { speler1(); speler2(); reset(); speler1bit(); speler2bit(); }
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 13
|
 |
« Reply #11 on: December 27, 2012, 07:15:48 am » |
@lloyddean well I upload it and there came smook out the arduino....
No, its works very well. Only I must repeat, to understanding, I beginning little to understand... Time gives advice.
greetings warpie
|
|
|
|
|
Logged
|
|
|
|
|
Des Moines, WA - USA
Offline
God Member
Karma: 21
Posts: 703
|
 |
« Reply #12 on: December 27, 2012, 02:15:55 pm » |
well I upload it and there came smook out the arduino....
I'm not sure if that was meant as "smoke" or wether I was just insulted - <http://www.urbandictionary.com/define.php?term=smook>
|
|
|
|
|
Logged
|
|
|
|
|
|