Go Down

Topic: 4 digit 7-segment ARK:SR410561N 1332 w/ Arduino Uno  (Read 150 times) previous topic - next topic

Agal

I am having trouble with this project I am doing.  I am trying to make a digital scale using an Uno and a 4 digit 7 segment SR410561N.  I am also using a 1k pot to use as the values for my digital scale.  The numbers are reading right but I am having a trouble displaying different numbers for each digit at the same time because its not being put in serially.  I don't really understand how I would be able to accomplish it with just the hardware at hand.  I am open to many suggestions if possible! Here is the code I have so far.

#define ON HIGH
#define OFF LOW

// segment should be low
int digit1=2;
int digit2=3;
int digit3=4;
int digit4=5;
int arrayD[]= {digit1, digit2, digit3, digit4};

int segA=6;
int segB=7;
int segC=8;
int segD=9;
int segE=10;
int segF=11;
int segG=12;
int segDot = 13;

int potPin = 5;
int val = 0;
float voltage;
int digiScale[4];

void setup() {
  Serial.begin(9600);
  pinMode(segA, OUTPUT);
  pinMode(segB, OUTPUT);
  pinMode(segC, OUTPUT);
  pinMode(segD, OUTPUT);
  pinMode(segE, OUTPUT);
  pinMode(segF, OUTPUT);
  pinMode(segG, OUTPUT);
  pinMode(segDot, OUTPUT);
 
  pinMode(digit1, OUTPUT);
  pinMode(digit2, OUTPUT);
  pinMode(digit3, OUTPUT);
  pinMode(digit4, OUTPUT);
}

void loop(){
  val = analogRead(potPin);
  Serial.println(val);
  delay(1000);
 
  voltage = val * (5.0 / 1023.0);
  Serial.println(voltage);
  Serial.print("\n");
  delay(1000);
 
 
//  int reset = 0;
//  int dot;
// 
//      switch(dot){
//    case 1:
//      digitalWrite(digit1, ON);
//      break;
//    case 2:
//      digitalWrite(digit2, ON);
//      break;
//    case 3:
//      digitalWrite(digit3, ON);
//      break;
//    case 4:
//      digitalWrite(digit4, ON);
//      break;
//    }

   
//digitalWrite(digit1, ON);
//digitalWrite(digit2, ON);
//digitalWrite(digit3, ON);
//digitalWrite(digit4, ON);
//delay(1000);

Serial.println("\n");

int one = val % 10;
Serial.println(one);

int two = val % 100/10;
Serial.println(two);

int three = val % 1000 / 100;
Serial.println(three);

int four = val / 1000;
Serial.println(four);

digiScale[0] = four;
digiScale[1] = three;
digiScale[2] = two;
digiScale[3] = one;

digitalWrite(digit1, ON);
digitalWrite(digit2, ON);
digitalWrite(digit3, ON);
digitalWrite(digit4, ON);

if (digit1 == 1){
  numbers(digiScale[0]);
}
if (digit2 == 1){
  numbers(digiScale[1]);
}
if (digit3 == 1){
  numbers(digiScale[2]);
}
if (digit4 == 1){
  numbers(digiScale[3]);
}
delay(1000);


dispScale();

digitalWrite(digit1, OFF);
digitalWrite(digit2, OFF);
digitalWrite(digit3, OFF);
digitalWrite(digit4, OFF);

//digitalWrite(digit1, ON);
//numbers(digiScale[3]);
//digitalWrite(digit2, OFF);
//digitalWrite(digit3, OFF);
//digitalWrite(digit4, OFF);
//delay(100);
//
//digitalWrite(digit2, ON);
//numbers(digiScale[2]);
//digitalWrite(digit3, ON);
//numbers(digiScale[1]);
//digitalWrite(digit4, ON);
//numbers(digiScale[0]);

//digitalWrite(digit1, OFF);
//digitalWrite(digit2, OFF);
//digitalWrite(digit3, OFF);
//digitalWrite(digit4, OFF);
//delay(1000);
}

void numbers(int digit){
//  digitalWrite(digit1, ON);
//  digitalWrite(digit2, ON);
//  digitalWrite(digit3, ON);
//  digitalWrite(digit4, ON);
//  delay(1000);

 switch(digit){
  case 0:
    digitalWrite(segA, OFF);
    digitalWrite(segB, OFF);
    digitalWrite(segC, OFF);
    digitalWrite(segD, OFF);
    digitalWrite(segE, OFF);
    digitalWrite(segF, OFF);
    digitalWrite(segG, ON);
    break;

  case 1:
    digitalWrite(segA, ON);
    digitalWrite(segB, OFF);
    digitalWrite(segC, OFF);
    digitalWrite(segD, ON);
    digitalWrite(segE, ON);
    digitalWrite(segF, ON);
    digitalWrite(segG, ON);
    break;

  case 2:
    digitalWrite(segA, OFF);
    digitalWrite(segB, OFF);
    digitalWrite(segC, ON);
    digitalWrite(segD, OFF);
    digitalWrite(segE, OFF);
    digitalWrite(segF, ON);
    digitalWrite(segG, OFF);
    break;

  case 3:
    digitalWrite(segA, OFF);
    digitalWrite(segB, OFF);
    digitalWrite(segC, OFF);
    digitalWrite(segD, OFF);
    digitalWrite(segE, ON);
    digitalWrite(segF, ON);
    digitalWrite(segG, OFF);
    break;

  case 4:
    digitalWrite(segA, ON);
    digitalWrite(segB, OFF);
    digitalWrite(segC, OFF);
    digitalWrite(segD, ON);
    digitalWrite(segE, ON);
    digitalWrite(segF, OFF);
    digitalWrite(segG, OFF);
    break;

  case 5:
    digitalWrite(segA, OFF);
    digitalWrite(segB, ON);
    digitalWrite(segC, OFF);
    digitalWrite(segD, OFF);
    digitalWrite(segE, ON);
    digitalWrite(segF, OFF);
    digitalWrite(segG, OFF);
    break;

  case 6:
    digitalWrite(segA, OFF);
    digitalWrite(segB, ON);
    digitalWrite(segC, OFF);
    digitalWrite(segD, OFF);
    digitalWrite(segE, OFF);
    digitalWrite(segF, OFF);
    digitalWrite(segG, OFF);
    break;

  case 7:
    digitalWrite(segA, OFF);
    digitalWrite(segB, OFF);
    digitalWrite(segC, OFF);
    digitalWrite(segD, ON);
    digitalWrite(segE, ON);
    digitalWrite(segF, ON);
    digitalWrite(segG, ON);
    break;

  case 8:
    digitalWrite(segA, OFF);
    digitalWrite(segB, OFF);
    digitalWrite(segC, OFF);
    digitalWrite(segD, OFF);
    digitalWrite(segE, OFF);
    digitalWrite(segF, OFF);
    digitalWrite(segG, OFF);
    break;

  case 9:
    digitalWrite(segA, OFF);
    digitalWrite(segB, OFF);
    digitalWrite(segC, OFF);
    digitalWrite(segD, OFF);
    digitalWrite(segE, ON);
    digitalWrite(segF, OFF);
    digitalWrite(segG, OFF);
    break;

  case 10:
    digitalWrite(segA, ON);
    digitalWrite(segB, ON);
    digitalWrite(segC, ON);
    digitalWrite(segD, ON);
    digitalWrite(segE, ON);
    digitalWrite(segF, ON);
    digitalWrite(segG, ON);
    break;
   
 
 }
}   

void dispScale(){
  for(int i = 0; i < 4; i++){
    digitalWrite(arrayD, ON);
    numbers(digiScale);
    Serial.print("\n");
    delay(1000);
  }
  delay(2000);
  numbers(10);
}
   


//Also all leds are connected with resistors.
 

Agal

Sorry, I cleaned up the code a little bit.

#define ON HIGH
#define OFF LOW

// segment should be low
int digit1=2;
int digit2=3;
int digit3=4;
int digit4=5;
int arrayD[]= {digit1, digit2, digit3, digit4};

int segA=6;
int segB=7;
int segC=8;
int segD=9;
int segE=10;
int segF=11;
int segG=12;
int segDot = 13;

int potPin = 5;
int val = 0;
float voltage;
int digiScale[4];

void setup() {
  Serial.begin(9600);
  pinMode(segA, OUTPUT);
  pinMode(segB, OUTPUT);
  pinMode(segC, OUTPUT);
  pinMode(segD, OUTPUT);
  pinMode(segE, OUTPUT);
  pinMode(segF, OUTPUT);
  pinMode(segG, OUTPUT);
  pinMode(segDot, OUTPUT);
 
  pinMode(digit1, OUTPUT);
  pinMode(digit2, OUTPUT);
  pinMode(digit3, OUTPUT);
  pinMode(digit4, OUTPUT);
}

void loop(){
  val = analogRead(potPin);
  Serial.println(val);
  delay(1000);
 
  voltage = val * (5.0 / 1023.0);
  Serial.println(voltage);
  Serial.print("\n");
  delay(1000);
 
//digitalWrite(digit1, ON);
//digitalWrite(digit2, ON);
//digitalWrite(digit3, ON);
//digitalWrite(digit4, ON);
//delay(1000);

Serial.println("\n");

i0nt one = val % 10;
Serial.println(one);

int two = val % 100/10;
Serial.println(two);

int three = val % 1000 / 100;
Serial.println(three);

int four = val / 1000;
Serial.println(four);

digiScale[0] = four;
digiScale[1] = three;
digiScale[2] = two;
digiScale[3] = one;

dispScale();

digitalWrite(digit1, OFF);
digitalWrite(digit2, OFF);
digitalWrite(digit3, OFF);
digitalWrite(digit4, OFF);
}

void numbers(int digit){


 switch(digit){
  case 0:
    digitalWrite(segA, OFF);
    digitalWrite(segB, OFF);
    digitalWrite(segC, OFF);
    digitalWrite(segD, OFF);
    digitalWrite(segE, OFF);
    digitalWrite(segF, OFF);
    digitalWrite(segG, ON);
    break;

  case 1:
    digitalWrite(segA, ON);
    digitalWrite(segB, OFF);
    digitalWrite(segC, OFF);
    digitalWrite(segD, ON);
    digitalWrite(segE, ON);
    digitalWrite(segF, ON);
    digitalWrite(segG, ON);
    break;

  case 2:
    digitalWrite(segA, OFF);
    digitalWrite(segB, OFF);
    digitalWrite(segC, ON);
    digitalWrite(segD, OFF);
    digitalWrite(segE, OFF);
    digitalWrite(segF, ON);
    digitalWrite(segG, OFF);
    break;

  case 3:
    digitalWrite(segA, OFF);
    digitalWrite(segB, OFF);
    digitalWrite(segC, OFF);
    digitalWrite(segD, OFF);
    digitalWrite(segE, ON);
    digitalWrite(segF, ON);
    digitalWrite(segG, OFF);
    break;

  case 4:
    digitalWrite(segA, ON);
    digitalWrite(segB, OFF);
    digitalWrite(segC, OFF);
    digitalWrite(segD, ON);
    digitalWrite(segE, ON);
    digitalWrite(segF, OFF);
    digitalWrite(segG, OFF);
    break;

  case 5:
    digitalWrite(segA, OFF);
    digitalWrite(segB, ON);
    digitalWrite(segC, OFF);
    digitalWrite(segD, OFF);
    digitalWrite(segE, ON);
    digitalWrite(segF, OFF);
    digitalWrite(segG, OFF);
    break;

  case 6:
    digitalWrite(segA, OFF);
    digitalWrite(segB, ON);
    digitalWrite(segC, OFF);
    digitalWrite(segD, OFF);
    digitalWrite(segE, OFF);
    digitalWrite(segF, OFF);
    digitalWrite(segG, OFF);
    break;

  case 7:
    digitalWrite(segA, OFF);
    digitalWrite(segB, OFF);
    digitalWrite(segC, OFF);
    digitalWrite(segD, ON);
    digitalWrite(segE, ON);
    digitalWrite(segF, ON);
    digitalWrite(segG, ON);
    break;

  case 8:
    digitalWrite(segA, OFF);
    digitalWrite(segB, OFF);
    digitalWrite(segC, OFF);
    digitalWrite(segD, OFF);
    digitalWrite(segE, OFF);
    digitalWrite(segF, OFF);
    digitalWrite(segG, OFF);
    break;

  case 9:
    digitalWrite(segA, OFF);
    digitalWrite(segB, OFF);
    digitalWrite(segC, OFF);
    digitalWrite(segD, OFF);
    digitalWrite(segE, ON);
    digitalWrite(segF, OFF);
    digitalWrite(segG, OFF);
    break;

  case 10:
    digitalWrite(segA, ON);
    digitalWrite(segB, ON);
    digitalWrite(segC, ON);
    digitalWrite(segD, ON);
    digitalWrite(segE, ON);
    digitalWrite(segF, ON);
    digitalWrite(segG, ON);
    break;
 }
}   

void dispScale(){
  for(int i = 0; i < 4; i++){
    digitalWrite(arrayD, ON);
    numbers(digiScale);
    delay(1000);
  }
  delay(2000);
  numbers(10);
}
   

PaulRB

Maybe, but you still didn't read the forum rules...

Did you notice some of parts of your sketch are missing and other parts are in italics?

Agal

I saw that what I deleted isn't there anymore so that it can be easier to read. I did see a typo on my part i0nt one should be int one. I don't see any missing parts. The parts in italics does not give me any problems.

PaulRB

The parts in italics does not give me any problems.
No, it gives problems to those who would like to help you.

Look at this line of your code that I have simply copied and pasted here:
Code: [Select]
   digitalWrite(arrayD, ON);
does that look the same as the line of code in your sketch? Can you see what is missing yet?

mvmacd

How do you have the hardware wired?

I have a 4 digit 7 segment (7 line plus a decimal) common cathode display. What I did to display 4 different numbers was to multiplex the display. You can only show 1 digit at a time because that's the only way to have complete control over all segments individually. So you must quickly and continuously run a function to draw each individual digit, at a very fast rate to trick the eye into seeing it as a whole.

I'm on my android right now so I don't have access the code rn, but you need to do something like this (untested)

Code: [Select]
int pointer = 0;
void tick()
{
if (pointer > 3)
   pointer = 0;

int pin = arrayD[pointer];
int lastpin;
if(pointer == 0)
   lastpin = arrayD[3]; //hop back
else lastpin = arrayD[pointer  - 1]; //one to the left/right

digitalWrite(lastpin, HIGH); //disable last digit

//add code here for drawing segments for digit 'pointer', where 0 is first digit, 3 is last.
digitalWrite(pin , LOW ); //enable current digit
pointer++;
}


Now the best thing to call this function is with an ISR, and call this function every 2 millisecond or so. If you can't get that working, you can just put it in loop () with a delay. However, that would limit the rest of the program.

Here is the timer example:

Code: [Select]

#include <TimerOne.h> //If this doesn't work, you need to download and import the Timer1 arduino library

//then add this to the setup() function:
Timer1.initialize(2000); // set a timer of length 2000 microseconds)
Timer1.attachInterrupt( tick ); // attach the service routine here



Hope this is of help.
Matt

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy