0
\$\begingroup\$

Currently i am doing a project on blinking two LED at random interval of time and count the number of blinks in one minute. I encounter one problem which is when number 2 display on the 7-segment display, the number 2 display looks weird. I hope anyone can help me to solve this issue. I had also attached my circuit diagram and the picture with weird number 2 below.

const int a=5;
const int b=4;
const int c=1;
const int d=2;
const int e=3;
const int f=7;
const int g=8;
const int DP=0;
const int lowerThreshold = 150;
const int upperThreshold = 200;
const int ldrPin = A0; // select the input pin for the LDR
int ledPin = 9;    // select the pin for the LED
int ledPin2 = 10; 
int counter = 0;
int analogValue = 0;
unsigned long int timer;

void setup() {
  // declare the ledPin as an OUTPUT
  pinMode(ledPin, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(ldrPin, INPUT);
  pinMode(a,OUTPUT);
  pinMode(b,OUTPUT);
  pinMode(c,OUTPUT);
  pinMode(d,OUTPUT);
  pinMode(e,OUTPUT);
  pinMode(f,OUTPUT);
  pinMode(g,OUTPUT);
  pinMode(DP,OUTPUT);
  Serial.begin(9600);
  Serial.println("CLEARDATA");
  Serial.println("LABEL,Time,Timer,Count for LED,Count for LED2");
}

void loop() {
  timer = millis()/1000;
  analogValue = analogRead(ldrPin); // Reading LDR Input.
  digitalWrite(ledPin, HIGH);
  digitalWrite(ledPin2, HIGH);
  Serial.println("DATA,Time," + String(timer) + "," + String(counter) + "," + String(counter));
  if(counter == 10|| timer > 60)
  {
    digitalWrite(ledPin, LOW);
    digitalWrite(ledPin2, LOW);
    Serial.println("SAVEWORKBOOK");
    Serial.println("STOPLOGGING");
  }else
  {
    counter++;
  }
  delay(random(20,3000));
  digitalWrite(ledPin, LOW);
  digitalWrite(ledPin2, LOW);
  delay(random(20,3000));
  switch(counter)
  {
    case 0:
    digitalWrite(a, HIGH);
    digitalWrite(b, HIGH);
    digitalWrite(c, HIGH);
    digitalWrite(d, HIGH);
    digitalWrite(e, HIGH);
    digitalWrite(f, HIGH);
    digitalWrite(g, LOW);
    digitalWrite(DP, HIGH);
    break;
    
    case 1:
    digitalWrite(a, LOW);
    digitalWrite(b, HIGH);
    digitalWrite(c, HIGH);
    digitalWrite(d, LOW);
    digitalWrite(e, LOW);
    digitalWrite(f, LOW);
    digitalWrite(g, LOW);
    digitalWrite(DP, HIGH);
    break;
    
    case 2:
    digitalWrite(a, HIGH);
    digitalWrite(b, HIGH);
    digitalWrite(c, LOW);
    digitalWrite(d, HIGH);
    digitalWrite(e, HIGH);
    digitalWrite(f, LOW);
    digitalWrite(g, HIGH);
    digitalWrite(DP, HIGH);
    break;

      case 3:
      digitalWrite(a, HIGH);
      digitalWrite(b, HIGH);
      digitalWrite(c, HIGH);
      digitalWrite(d, HIGH);
      digitalWrite(e, LOW);
      digitalWrite(f, LOW);
      digitalWrite(g, HIGH);
      digitalWrite(DP, HIGH);
      break;
      
      case 4:
      digitalWrite(a, LOW);
      digitalWrite(b, HIGH);
      digitalWrite(c, HIGH);
      digitalWrite(d, LOW);
      digitalWrite(e, LOW);
      digitalWrite(f, HIGH);
      digitalWrite(g, HIGH);
      digitalWrite(DP, HIGH);
      break;

      case 5:
      digitalWrite(a, HIGH);
      digitalWrite(b, LOW);
      digitalWrite(c, HIGH);
      digitalWrite(d, HIGH);
      digitalWrite(e, LOW);
      digitalWrite(f, HIGH);
      digitalWrite(g, HIGH);
      digitalWrite(DP, HIGH);
      break;
      
      case 6:
      digitalWrite(a, HIGH);
      digitalWrite(b, LOW);
      digitalWrite(c, HIGH);
      digitalWrite(d, HIGH);
      digitalWrite(e, HIGH);
      digitalWrite(f, HIGH);
      digitalWrite(g, HIGH);
      digitalWrite(DP, HIGH);
      break;
      
      case 7:
      digitalWrite(a, HIGH);
      digitalWrite(b, HIGH);
      digitalWrite(c, HIGH);
      digitalWrite(d, LOW);
      digitalWrite(e, LOW);
      digitalWrite(f, LOW);
      digitalWrite(g, LOW);
      digitalWrite(DP, HIGH);
      break;
      
      case 8:
      digitalWrite(a, HIGH);
      digitalWrite(b, HIGH);
      digitalWrite(c, HIGH);
      digitalWrite(d, HIGH);
      digitalWrite(e, HIGH);
      digitalWrite(f, HIGH);
      digitalWrite(g, HIGH);
      digitalWrite(DP, HIGH);
      break;

      case 9:
      digitalWrite(a, HIGH);
      digitalWrite(b, HIGH);
      digitalWrite(c, HIGH);
      digitalWrite(d, HIGH);
      digitalWrite(e, LOW);
      digitalWrite(f, HIGH);
      digitalWrite(g, HIGH);
      digitalWrite(DP, HIGH);
      break;
      
      break;
  }
}

enter image description here

\$\endgroup\$
7
  • 4
    \$\begingroup\$ In what way does "2" look weird? \$\endgroup\$
    – StarCat
    Dec 21, 2020 at 15:58
  • 2
    \$\begingroup\$ As in show us the weird 2! \$\endgroup\$ Dec 21, 2020 at 15:59
  • 2
    \$\begingroup\$ Is it displaying upside down? \$\endgroup\$
    – user16324
    Dec 21, 2020 at 16:00
  • \$\begingroup\$ I guess there's only one possible way for the number 2 to 'look weird', and we should all know what that is ... \$\endgroup\$
    – brhans
    Dec 21, 2020 at 16:03
  • \$\begingroup\$ The segment c will become HIGH as well, although i had set it to LOW \$\endgroup\$
    – Jin En Ng
    Dec 21, 2020 at 16:04

1 Answer 1

2
\$\begingroup\$

You're using pins 0 and 1 to drive your display ('dp' and 'c'), but these pins are also used for the Arduino's serial port (RX and TX).

This explains why segment "c" also lights up when displaying "2", as it is connected to your serial TX port and your program is sending output to it.

The solution is to use different (unused) pins for segments 'c' and 'dp', for example 11 and 12 as they don't seem to be used in your sketch.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.