7-segment display

Arduino specifieke Software
Gebruikers-avatar
Berichten: 631
Geregistreerd: 15 Nov 2015, 11:54

7-segment display

Berichtdoor Gij Kieken » 29 Dec 2016, 20:02

Ik heb een stukje rechtuit rechtaan code geschreven voor de bediening van een groot zeven segment display.(Arduino UnoR3 of Nano)
Met een 10-positie draaischakelaar selecteer je het gewenst nummer van 0 tot 9 (telkens slechts 1 positie mogelijk).
Ik heb er een Error melding (<-> teken) in gestoken ingeval van kabelbreuk.
Hoe zou ik eventueel dezelfde Error melding kunnen geven ingeval er toch (door welke reden dan ook) twee ingangen bediend zijn?
Bijgevoegd mijn voorlopige code.
Alle tips zijn welkom.
Code: Alles selecteren
/*
   @  Control 7-segment display via relay's.
   @  7-segment display's made of individual Led stripes.
   @  Input is controlled via 10-pos.rotary-switch,common to ground.
   @  All inputs connected with 10k pull-up resistor to 5v.
   @  Connect Arduino inputs  DI9..DI13 and A0..A5 to rotary-switch pos.1--10.
   @  Notice that analog pin's can be addressed as digitalRead(14)--digitalRead(19).
   @  Led stripes connected in common Anode (common Kathode is also possible).
   @  Sketch is written for common anode because of relay-card is active low.
   @  0=Led-On, 1=Led-Off
   @  7-segment display :  a,b,c,d ,e ,f ,g
   @  Arduino pin numbers: 2,3,4,5,6,7,8                   
*/

//  Setup an 2-dimensional array, ten numbers 0-9, 7-segments
/*
byte seven_seg_display[11][7]={  //  Setup for common Kathode or Arduino relay-card
  { 0,0,0,0,0,0,1 }  ,  // 0,  switch off the bits for number 0  (a,b,c,d,e,f,g)
  { 1,0,0,1,1,1,1 }  ,  // 1,  switch off the bits for number 1  (a,b,c,d,e,f,g)
  { 0,0,1,0,0,1,0 }  ,  // 2,  switch off the bits for number 2  (a,b,c,d,e,f,g)
  { 0,0,0,0,1,1,0 }  ,  // 3,  switch off the bits for number 3  (a,b,c,d,e,f,g)
  { 1,0,0,1,1,0,0 }  ,  // 4,  switch off the bits for number 4  (a,b,c,d,e,f,g)
  { 0,1,0,0,1,0,0 }  ,  // 5,  switch off the bits for number 5  (a,b,c,d,e,f,g)
  { 0,1,0,0,0,0,0 }  ,  // 6,  switch off the bits for number 6  (a,b,c,d,e,f,g)
  { 0,0,0,1,1,1,1 }  ,  // 7,  switch off the bits for number 7  (a,b,c,d,e,f,g)
  { 0,0,0,0,0,0,0 }  ,  // 8,  switch off the bits for number 8  (a,b,c,d,e,f,g)
  { 0,0,0,0,1,0,0 }  ,  // 9,  switch off the bits for number 9  (a,b,c,d,e,f,g)
  { 1,1,1,1,1,1,0 }  ,  // error, just switch off segment g
};
*/
byte totalVal = 0;
byte seven_seg_display[11][7]={  //  Test is being rund with common Kathode display
  { 1,1,1,1,1,1,0 }  ,  // 0,  highlight the bits for number 0  (a,b,c,d,e,f,g)
  { 0,1,1,0,0,0,0 }  ,  // 1,  highlight the bits for number 1  (a,b,c,d,e,f,g)
  { 1,1,0,1,1,0,1 }  ,  // 2,  highlight the bits for number 2  (a,b,c,d,e,f,g)
  { 1,1,1,1,0,0,1 }  ,  // 3,  highlight the bits for number 3  (a,b,c,d,e,f,g)
  { 0,1,1,0,0,1,1 }  ,  // 4,  highlight the bits for number 4  (a,b,c,d,e,f,g)
  { 1,0,1,1,0,1,1 }  ,  // 5,  highlight the bits for number 5  (a,b,c,d,e,f,g)
  { 1,0,1,1,1,1,1 }  ,  // 6,  highlight the bits for number 6  (a,b,c,d,e,f,g)
  { 1,1,1,0,0,0,0 }  ,  // 7,  highlight the bits for number 7  (a,b,c,d,e,f,g)
  { 1,1,1,1,1,1,1 }  ,  // 8,  highlight the bits for number 8  (a,b,c,d,e,f,g)
  { 1,1,1,1,0,1,1 }  ,  // 9,  highlight the bits for number 9  (a,b,c,d,e,f,g)
  { 0,0,0,0,0,0,1 }  ,  // error, just highlight segment g
};

void setup(){                   
  Serial.begin(9600);
//  Setup Arduino pin configuration
  pinMode(9 ,INPUT);    //  Dec input 0    All Input Pin's are external 10k Pull-up
  pinMode(10,INPUT);    //  Dec input 1
  pinMode(11,INPUT);    //  Dec input 2
  pinMode(12,INPUT);    //  Dec input 3
  pinMode(13,INPUT);    //  Dec input 4
  pinMode(A0,INPUT);    //  Dec input 5    Same as digitalRead(14)
  pinMode(A1,INPUT);    //  Dec input 6    Same as digitalRead(15)
  pinMode(A2,INPUT);    //  Dec input 7    Same as digitalRead(16)
  pinMode(A3,INPUT);    //  Dec input 8    Same as digitalRead(17)
  pinMode(A4,INPUT);    //  Dec input 9    Same as digitalRead(18)
  pinMode(2,OUTPUT);    //  Display segment a       
  pinMode(3,OUTPUT);    //  Display segment b   
  pinMode(4,OUTPUT);    //  Display segment c   
  pinMode(5,OUTPUT);    //  Display segment d   
  pinMode(6,OUTPUT);    //  Display segment e   
  pinMode(7,OUTPUT);    //  Display segment f     
  pinMode(8,OUTPUT);    //  Display segment g
 
//  Only runs one time for test purpose 
    for(byte downCount = 10; downCount > 0; downCount --){
    delay(1000);
    sevenSeg_write(downCount-1);
  }
delay(1000);            //  Give the system time to stabilize
}

void sevenSeg_write(byte digit){
  byte pin=2;            //  Display segments, segDisplay(0)=a,..., segDisplay(6)=g
  for(byte segDisplay = 0; segDisplay < 7; segDisplay ++){
    digitalWrite(pin, seven_seg_display[digit][segDisplay]);
    pin ++;
  }
}

byte readSwitch(){    //  Function to read the state of the Inputs
    totalVal = 0;     //  If all Inputs are High,(wire broken)display Error-
    if (digitalRead(9 ) == HIGH) {totalVal += 1;}  //Dec input 0
    if (digitalRead(10) == HIGH) {totalVal += 1;}  //Dec input 1
    if (digitalRead(11) == HIGH) {totalVal += 1;}  //Dec input 2
    if (digitalRead(12) == HIGH) {totalVal += 1;}  //Dec input 3
    if (digitalRead(13) == HIGH) {totalVal += 1;}  //Dec input 4
    if (digitalRead(14) == HIGH) {totalVal += 1;}  //Dec input 5
    if (digitalRead(15) == HIGH) {totalVal += 1;}  //Dec input 6
    if (digitalRead(16) == HIGH) {totalVal += 1;}  //Dec input 7 
    if (digitalRead(17) == HIGH) {totalVal += 1;}  //Dec input 8
    if (digitalRead(18) == HIGH) {totalVal += 1;}  //Dec input 9
  return totalVal;
}

void loop(){
  for (byte i = 9; i < 19; i ++){
      if (digitalRead(i) == LOW){
      sevenSeg_write(i-9);
    }
  }
  readSwitch();
  if (totalVal > 9) {
      sevenSeg_write(10);
  }
  delay(1000);
}


Advertisement

Gebruikers-avatar
Berichten: 631
Geregistreerd: 15 Nov 2015, 11:54

Re: 7-segment display

Berichtdoor Gij Kieken » 30 Dec 2016, 11:25

Opgelost,
Code: Alles selecteren
/*
   @  Control 7-segment display via relay's.
   @  7-segment display's made of individual Led stripes.
   @  Input is controlled via 10-pos.rotary-switch,common to ground.
   @  All inputs connected with 10k pull-up resistor to 5v.
   @  Connect Arduino inputs  DI9..DI13 and A0..A5 to rotary-switch pos.1--10.
   @  Notice that analog pin's can be addressed as digitalRead(14)--digitalRead(19).
   @  Led stripes connected in common Anode (common Kathode is also possible).
   @  Sketch is written for common anode because of relay-card is active low.
   @  0=Led-On, 1=Led-Off
   @  Connect Arduino pin numbers: 2,3,4,5,6,7,8 to 7-segment display :  a,b,c,d ,e ,f ,g.
   @  Error mesages, <broken wire [o]>, <short circuit [E]>.   
*/
//#define DEBUG    //  Uncomment this line for debug purposes
//  Setup an 2-dimensional array, ten numbers 0-9, 7-segments
/*
byte seven_seg_display[12][7]={  //  Setup for common Kathode or Arduino relay-card
  { 0,0,0,0,0,0,1 }  ,  // 0,  switch off the bits for number 0  (a,b,c,d,e,f,g)
  { 1,0,0,1,1,1,1 }  ,  // 1,  switch off the bits for number 1  (a,b,c,d,e,f,g)
  { 0,0,1,0,0,1,0 }  ,  // 2,  switch off the bits for number 2  (a,b,c,d,e,f,g)
  { 0,0,0,0,1,1,0 }  ,  // 3,  switch off the bits for number 3  (a,b,c,d,e,f,g)
  { 1,0,0,1,1,0,0 }  ,  // 4,  switch off the bits for number 4  (a,b,c,d,e,f,g)
  { 0,1,0,0,1,0,0 }  ,  // 5,  switch off the bits for number 5  (a,b,c,d,e,f,g)
  { 0,1,0,0,0,0,0 }  ,  // 6,  switch off the bits for number 6  (a,b,c,d,e,f,g)
  { 0,0,0,1,1,1,1 }  ,  // 7,  switch off the bits for number 7  (a,b,c,d,e,f,g)
  { 0,0,0,0,0,0,0 }  ,  // 8,  switch off the bits for number 8  (a,b,c,d,e,f,g)
  { 0,0,0,0,1,0,0 }  ,  // 9,  switch off the bits for number 9  (a,b,c,d,e,f,g)
  { 1,1,0,0,0,1,0 }  ,  //     error, broken wire
  { 0,1,1,0,0,0,0 }  ,  //     error, short circuit   
};
*/
byte totalVal = 0;
byte seven_seg_display[12][7]={  //  Test has been runned with common Kathode display
  { 1,1,1,1,1,1,0 }  ,  // 0,  highlight the bits for number 0  (a,b,c,d,e,f,g)
  { 0,1,1,0,0,0,0 }  ,  // 1,  highlight the bits for number 1  (a,b,c,d,e,f,g)
  { 1,1,0,1,1,0,1 }  ,  // 2,  highlight the bits for number 2  (a,b,c,d,e,f,g)
  { 1,1,1,1,0,0,1 }  ,  // 3,  highlight the bits for number 3  (a,b,c,d,e,f,g)
  { 0,1,1,0,0,1,1 }  ,  // 4,  highlight the bits for number 4  (a,b,c,d,e,f,g)
  { 1,0,1,1,0,1,1 }  ,  // 5,  highlight the bits for number 5  (a,b,c,d,e,f,g)
  { 1,0,1,1,1,1,1 }  ,  // 6,  highlight the bits for number 6  (a,b,c,d,e,f,g)
  { 1,1,1,0,0,0,0 }  ,  // 7,  highlight the bits for number 7  (a,b,c,d,e,f,g)
  { 1,1,1,1,1,1,1 }  ,  // 8,  highlight the bits for number 8  (a,b,c,d,e,f,g)
  { 1,1,1,1,0,1,1 }  ,  // 9,  highlight the bits for number 9  (a,b,c,d,e,f,g)
  { 0,0,1,1,1,0,1 }  ,  //     error, broken wire
  { 1,0,0,1,1,1,1 }  ,  //     error, short circuit 
};

void setup(){                   
//  Setup Arduino pin configuration
  pinMode(9 ,INPUT);    //  Dec input 0    All Input Pin's are external 10k Pull-up
  pinMode(10,INPUT);    //  Dec input 1
  pinMode(11,INPUT);    //  Dec input 2
  pinMode(12,INPUT);    //  Dec input 3
  pinMode(13,INPUT);    //  Dec input 4
  pinMode(A0,INPUT);    //  Dec input 5    Same as digitalRead(14)
  pinMode(A1,INPUT);    //  Dec input 6    Same as digitalRead(15)
  pinMode(A2,INPUT);    //  Dec input 7    Same as digitalRead(16)
  pinMode(A3,INPUT);    //  Dec input 8    Same as digitalRead(17)
  pinMode(A4,INPUT);    //  Dec input 9    Same as digitalRead(18)
  pinMode(2,OUTPUT);    //  Display segment a       
  pinMode(3,OUTPUT);    //  Display segment b   
  pinMode(4,OUTPUT);    //  Display segment c   
  pinMode(5,OUTPUT);    //  Display segment d   
  pinMode(6,OUTPUT);    //  Display segment e   
  pinMode(7,OUTPUT);    //  Display segment f     
  pinMode(8,OUTPUT);    //  Display segment g
 
//  Only runs one time for test purpose 
    for(byte downCount = 10; downCount > 0; downCount --){
    delay(1000);
    sevenSeg_write(downCount-1);
  }
delay(1000);            //  Give the system time to stabilize
}

void sevenSeg_write(byte digit){
  byte pin=2;            //  Display segments, segDisplay(0)=a,..., segDisplay(6)=g
  for(byte segDisplay = 0; segDisplay < 7; segDisplay ++){
    digitalWrite(pin, seven_seg_display[digit][segDisplay]);
    pin ++;
  }
}

byte readSwitch(){    //  Function to read the state of the Inputs
    totalVal = 0;     //  If all Inputs are High,(wire broken)display Error-
    if (digitalRead(9 ) == HIGH) {totalVal += 1;}  //Dec input 0
    if (digitalRead(10) == HIGH) {totalVal += 1;}  //Dec input 1
    if (digitalRead(11) == HIGH) {totalVal += 1;}  //Dec input 2
    if (digitalRead(12) == HIGH) {totalVal += 1;}  //Dec input 3
    if (digitalRead(13) == HIGH) {totalVal += 1;}  //Dec input 4
    if (digitalRead(14) == HIGH) {totalVal += 1;}  //Dec input 5
    if (digitalRead(15) == HIGH) {totalVal += 1;}  //Dec input 6
    if (digitalRead(16) == HIGH) {totalVal += 1;}  //Dec input 7 
    if (digitalRead(17) == HIGH) {totalVal += 1;}  //Dec input 8
    if (digitalRead(18) == HIGH) {totalVal += 1;}  //Dec input 9
  return totalVal;
}

void loop(){
  for (byte i = 9; i < 19; i ++){
      if (digitalRead(i) == LOW){
      sevenSeg_write(i-9);
    }
  }
  readSwitch();
  #ifdef DEBUG
    Serial.println(totalVal);   
  #endif 
  if (totalVal > 9) {      //  Check for broken wire
      sevenSeg_write(10);
  }
  if (totalVal < 9) {      //  Check for short circuit
      sevenSeg_write(11);
  }
  delay(1000);
}


Terug naar Arduino software

Wie is er online?

Gebruikers in dit forum: Geen geregistreerde gebruikers en 21 gasten