Jump to content

I have problem with my code. i want to make a simple counter with an 4 digit 7 segment led display. it is not a big project but it is to test the led display, i never used it before.

anyway the project works in the serial monitor and with an 16 by 2 lcd. but with the led display, it doesn't do what i had in mind. It needs to count upwards to the number 9999. the counting happens and the digits works, but not at the same time, it doesn't properly show the number 1248 for example, not even number 10. so is there anyone who knows something about this that can help me?

 

here is the part of the code where i think the fault is.

 

void loop(){
  seg++;
  selectDwriteL(4,seg);
  if (seg > 9){
    w++;
    selectDwriteL(3,w);
    if (seg>9, w>9){
      x++;
      selectDwriteL(2,x);
      if (seg>9, w>9, x>9){
        y++;
        selectDwriteL(1,y);
        if(seg>9, w>9, x>9, y>9){
          y=0;
        }
       x=0;
      }
      w=0;
    }
    seg=0;
  }

 

here is a small video how it now works. the full program is also here but it isn't finished yet.

MVI_2198_WMV V9.wmv

test.ino

Link to comment
Share on other sites

Link to post
Share on other sites

Please use the code function in the editor, it makes it easier to read your code.

 

The problem with your code is that the part that displays a number on each segment is inside the if statement. Your code ONLY writes to the second segment when the first one is at 9. Placing the "selectDwriteL" commands outside all the if statements should solve the issue.

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to comment
Share on other sites

Link to post
Share on other sites

i tried it before by placing it after the last } with seg = 0. then it only displayed the last program line. so if you would set the digit order like this 1234 than it only showed the 4th digit.

Link to comment
Share on other sites

Link to post
Share on other sites

2 hours ago, lonewolf543210 said:

i tried it before by placing it after the last } with seg = 0. then it only displayed the last program line. so if you would set the digit order like this 1234 than it only showed the 4th digit.

I would use an array to store your number and use scientific notation when doing any math. 

 

When you display your array, you have to endure EACH index of the array is sent to a different position in the LED display. I programmed something like this once. (The documentation from arduino REALLY helps.) 

ill see if I can find my program and I’ll post it if it’s close enough to your problem. 

 

Did you find the Arduino documentation on LCD screens? 

Link to comment
Share on other sites

Link to post
Share on other sites

something like this?

n = [0,0,0,0];

void loop() {	
	for (i = 0; i < 4; i++) {		
		if (n[i] > 9) {
			n[i] = 0;
			if (i < 3) n[(i + 1) % 4]++;
		}
		selectDwrite(4 - i, n[i]);	
	}
	n[0]++;
}

 

If you're interested in a product please download and read the manual first.

Don't forget to tag or quote in your reply if you want me to know you've answered or have another question.

Link to comment
Share on other sites

Link to post
Share on other sites

22 minutes ago, keskparane said:

something like this?


n = [0,0,0,0];

void loop() {	
	for (i = 0; i < 4; i++) {		
		if (n[i] > 9) {
			n[i] = 0;
			if (i < 3) n[(i + 1) % 4]++;
		}
		selectDwrite(4 - i, n[i]);	
	}
	n[0]++;
}

 

no this didn't work either, with this it only shows the first digit an the other flash really fast but with almost no light, if you look very closely it does count and display. but it is unreadable. can this be a power issue? i will also include the electronic circuit, Ps. it is originally from a paintball time bomb hobby thingy or something but the resistors an wiring of the display are the same. and with the time bomb program the display works fine.Knipsel.JPG

 

2 hours ago, fpo said:

Did you find the Arduino documentation on LCD screens? 

not to be rude or so but I don't really get what a lcd has to do with this. i can program lcd, but i am new to the led displays and led matrics, so this i the first thing i am building with this display

Original timer code.ino

Link to comment
Share on other sites

Link to post
Share on other sites

Thanks for quoting. I actaully forgot to check if my past projects were relevant or not. 

24 minutes ago, lonewolf543210 said:

not to be rude or so but I don't really get what a lcd has to do with this. i can program lcd, but i am new to the led displays and led matrics, so this i the first thing i am building with this display

Original timer code.ino

Sorry mixed up what kind of display you were using. 
This is the documentation I was talking about:
https://www.arduino.cc/en/Tutorial/HelloWorld

 

As for your Paintball screen, it MIGHT be damaged, but the chances of that are slim. You said it already CAN post characters to one slot. You're just having issues posting characters to the other slots. 

My code:

Spoiler

#include <LiquidCrystal.h>   // initialize the library
const int rs=12, en=11, d4=5, d5=4, d6=3, d7=2;  //declare constants for LCD pin numbers
LiquidCrystal lcd(rs,en,d4,d5,d6,d7);  //set liquid crystal control and data pins
const int pingPin = 7;

void setup() {
  lcd.begin(16,2);  //defines LCS to have 16 characters, 2 rows
  Serial.begin(9600);
  }

void loop() {
  long duration, inches; // ping sensor variables
  int tDelay = 100;
  //PingPin 
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);

  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);

  inches = microsecondsToInches(duration);
  
  delay(tDelay);

  lcd.setCursor(0,0);  //positions LCD cursor at character 0, row 0 (top left)
  lcd.print("inches");
  lcd.setCursor(0,1);
  lcd.print("                ");
  if(inches < 10)
    lcd.setCursor(15,1);  //positions LCS cursor at character 0, row 1 (bottom left)
  else if (inches < 100)
    lcd.setCursor(14,1);
  else if (inches < 1000)
    lcd.setCursor(14,1);
  lcd.print(inches);
}

long microsecondsToInches(long microseconds) {
  return microseconds / 74 / 2;
}

 

EDIT:
Oh, turns out I was using an LCD. I don't know if I was of much help then. 
Maybe there's a documentation on LED that can help you. 

Link to comment
Share on other sites

Link to post
Share on other sites

25 minutes ago, lonewolf543210 said:

no this didn't work either, with this it only shows the first digit an the other flash really fast but with almost no light, if you look very closely it does count and display. but it is unreadable. can this be a power issue?

I was just providing a logic for counting the number.

Your problem with the numbers flashing is probably because the loop is going too fast. You probably just need to put in a delay to slow it down.

 

If you're interested in a product please download and read the manual first.

Don't forget to tag or quote in your reply if you want me to know you've answered or have another question.

Link to comment
Share on other sites

Link to post
Share on other sites

2 hours ago, fpo said:

Oh, turns out I was using an LCD. I don't know if I was of much help then. 
Maybe there's a documentation on LED that can help you. 

thanks for the help anyways, it the effort that count:P

 

1 hour ago, keskparane said:

I was just providing a logic for counting the number.

Your problem with the numbers flashing is probably because the loop is going too fast. You probably just need to put in a delay to slow it down.

i know it was just an example for the counting, but it seems good so i tried it. but an delay does't help. they keep flashing with minimum light that is still unreadable, exept for the first one off course. but they will flash slower.

 

ps. i can not work very good with arrays:S

Link to comment
Share on other sites

Link to post
Share on other sites

10 minutes ago, lonewolf543210 said:

ps. i can not work very good with arrays:S

For Arrays, Presuming you know how they work, my intent was basically this:

Array

Index: 0  1   2   3

Value  1   2   4    8

 

And when you went to display, you'd say

slot 0 on LED, display index 0 of Array

Slot 1 on LED, display index 1 of Array

Slot 2 on LED, display index 2 of Array

Slot 3 on LED, display index 3 of Array

 

If there is a local University/College, perhaps an instructor (if they're a competent/passionate one) will be a good resource to potentially explain the LED Screen concepts & how the LED screen understands the electrical signals sent from an Arduino. That's just if you can't find anything here nor on Google. 

Link to comment
Share on other sites

Link to post
Share on other sites

21 hours ago, lonewolf543210 said:

i tried it before by placing it after the last } with seg = 0. then it only displayed the last program line. so if you would set the digit order like this 1234 than it only showed the 4th digit.

No, the write commands should be the first thing in the loop. That way, every segment is lit up at every iteration with its current value, regardless of what number you're at. The actual counting is separated and independent from the display. If your counting procedure is incorrect that's a different matter, but it didn't look like it was.

 

Please quote me if you want me to see your answer.

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to comment
Share on other sites

Link to post
Share on other sites

23 hours ago, lonewolf543210 said:

<snip>

If you look at your own diagram you posted above you see that it's controlling four 7-segment displays with only 12 wires where one would normally expect 36 - that should give you a clue that there's some trick going on.

 

The display is being multiplexed. There's lots of articles and explanations about display multiplexing online so I suggest you study that first.

Once you understand multiplexing you'll know that:

  • Only one display can be active at any one time, when you write a value to a display it turns the other 3 off. That means you can only update 1 display each iteration of the loop.
  • You must update all 4 displays sequentially constantly to provide the illusion all displays are on at the same time.
Link to comment
Share on other sites

Link to post
Share on other sites

3 hours ago, Sauron said:

No, the write commands should be the first thing in the loop. That way, every segment is lit up at every iteration with its current value, regardless of what number you're at. The actual counting is separated and independent from the display. If your counting procedure is incorrect that's a different matter, but it didn't look like it was.

this didn't work either, but tanks for answering

2 hours ago, Unimportant said:

If you look at your own diagram you posted above you see that it's controlling four 7-segment displays with only 12 wires where one would normally expect 36 - that should give you a clue that there's some trick going on.

 

The display is being multiplexed. There's lots of articles and explanations about display multiplexing online so I suggest you study that first.

Once you understand multiplexing you'll know that:

  • Only one display can be active at any one time, when you write a value to a display it turns the other 3 off. That means you can only update 1 display each iteration of the loop.
  • You must update all 4 displays sequentially constantly to provide the illusion all displays are on at the same time.

it is indeed highly possible that this is the case. i didn't even know that a multiplexed led display existed, i thought that it was a normal display. but i don't really get why the original program works and my program not, i used the same command? but I am going to look it up anyway and i will post something if i found an solution or not, I already thank all of you for helping:)

Link to comment
Share on other sites

Link to post
Share on other sites

31 minutes ago, lonewolf543210 said:

this didn't work either, but tanks for answering

This didn't work?

void loop(){
    selectDwriteL(4,seg);
    selectDwriteL(3,w);
    selectDwriteL(2,x);
    selectDwriteL(1,y);
    seg++;
    if (seg > 9){
        w++;
        if (seg>9, w>9){
            x++;
            if (seg>9, w>9, x>9){
                y++;
                if(seg>9, w>9, x>9, y>9){
                    y=0;
                }
                x=0;
            }
            w=0;
        }
        seg=0;
    }
}

then there's probably something wrong in the way this function is being called; if @Unimportant is right about the displays needing to be constantly refreshed, then what you'd need is something like this:

void loop(){
    seg++;
    if (seg > 9){
        w++;
        if (seg>9, w>9){
            x++;
            if (seg>9, w>9, x>9){
                y++;
                if(seg>9, w>9, x>9, y>9){
                    y=0;
                }
                x=0;
            }
            w=0;
        }
        seg=0;
    }
}

int main(){
	seg,x,y,w=0;	//I assume these are declared globally
  	int secs=0;
	while(1){
      	selectDwriteL(4,seg);
    	selectDwriteL(3,w);
    	selectDwriteL(2,x);
    	selectDwriteL(1,y);
      	if(secs++ == 100){	//this will determine how long the interval between increments is
          	loop();
          	secs = 0;
        }
    }
  	return 0;
}

This way, the displays are being constantly refreshed but they only change value (with the loop() function) at somewhat fixed intervals. The code should be correct, there may be some hardware limitations I'm unaware of though.

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to comment
Share on other sites

Link to post
Share on other sites

 

2 hours ago, Sauron said:

This didn't work?

indeed that did not work

2 hours ago, Sauron said:

then there's probably something wrong in the way this function is being called; if @Unimportant is right about the displays needing to be constantly refreshed, then what you'd need is something like this:

it didn't work either, actually not at all. not one digit light up. when i change the int main() in to an void and this void add to the void loop. it lights up with zero's, and when i put the canter in the int main the same thing happens.

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, lonewolf543210 said:

 

indeed that did not work

it didn't work either, actually not at all. not one digit light up. when i change the int main() in to an void and this void add to the void loop. it lights up with zero's, and when i put the canter in the int main the same thing happens.

Then there must be some arduino specific stuff I'm unaware of, sorry for being unable to help :(

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to comment
Share on other sites

Link to post
Share on other sites

5 hours ago, Sauron said:

Then there must be some arduino specific stuff I'm unaware of, sorry for being unable to help :(

no problem, its the effort that counts. thanks for trying.:)

Link to comment
Share on other sites

Link to post
Share on other sites

What about ....

n = [0,0,0,0];
refreshRate = 1000;
incrementRate = 1000;
reset = false;

while (refreshRate > 0) {	
	for (i = 0; i < 4; i++) {		
		selectDwrite(4 - i, n[i]);
	}
	delay(refreshRate);
}

tick();

void tick() {
	if(reset) {
		reset = false;
		n = [0,0,0,0];
		return;
	}
	else {
		for (i = 0; i < 4; i++) {		
			if (n[i] > 9) {
				n[i] = 0;
				if (i < 3) n[(i + 1) % 4]++;			
			}
		}	
		n[0]++;
		delay(incrementRate);	
		tick();	
	}
}

If it's too dim lower refreshRate

If you're interested in a product please download and read the manual first.

Don't forget to tag or quote in your reply if you want me to know you've answered or have another question.

Link to comment
Share on other sites

Link to post
Share on other sites

4 hours ago, keskparane said:

If it's too dim lower refreshRate

thaks for the reaction, but it didn't work either. I lowered the refreshrate althe way to 0 and it still didn't work. with the refreshrate on 1 it maks the 3 last digit barely visible an the first one normal, just like 10, 100 and 1000. when i set it to 0 it lights up completely normal, all 4 digit with the normal dim it is suppose to do. but it doesn't count or change the digits. it just stays on 4 zero's.

 

ps. i had to adjust the array part from

n = [0,0,0,0];

to

int n[] = {0,0,0,0};

before the arduino ide(version 1.8.5) it accepted. is this correct or do i have to change it to something else

Link to comment
Share on other sites

Link to post
Share on other sites

I don't know. I have never worked with arduino. I only responded initially because I thought it was more about the loop logic.

What I'm wondering (since 1 digit displays correctly) is if the numbers have to be written in the other order. What happens if you change the order they are written?

while (refreshRate > 0) {	
	for (i = 1; i <= 4; i++) {		
		selectDwrite(i, n[4 - i]);
	}
	delay(refreshRate);
}

edit: I just realized the previous post will never make it to the tick function. If this writes correctly we'll update the increment.

edit #2: I also just noticed I had left the L off selectDwriteL

edit #3: Although then and again I hadn't actually changed the way you initially wrote to the screen. So if yours displayed I don't see what changed.

If you're interested in a product please download and read the manual first.

Don't forget to tag or quote in your reply if you want me to know you've answered or have another question.

Link to comment
Share on other sites

Link to post
Share on other sites

4 hours ago, keskparane said:

I don't know. I have never worked with arduino. I only responded initially because I thought it was more about the loop logic.

What I'm wondering (since 1 digit displays correctly) is if the numbers have to be written in the other order. What happens if you change the order they are written?

the same happens but in the opposite direction, first the last one than the other tree. and it also doesn't tick, so i am an little bit confused because the program i adjusted worked perfectly, and afterwards it didn't and i used the same command to light up the display

Link to comment
Share on other sites

Link to post
Share on other sites

Ok I looked at your full code and know whats happening. I'll provide an explanation tomorrow when I have more time.

If you're interested in a product please download and read the manual first.

Don't forget to tag or quote in your reply if you want me to know you've answered or have another question.

Link to comment
Share on other sites

Link to post
Share on other sites

At the start of selectDwriteL it is deciding which digits to turn on. Obviously you need each digit in your number to be turned on.

 

I am unsure how you select which digit gets written to though as I can't see any indexing for the display.

If you're interested in a product please download and read the manual first.

Don't forget to tag or quote in your reply if you want me to know you've answered or have another question.

Link to comment
Share on other sites

Link to post
Share on other sites

8 hours ago, keskparane said:

I am unsure how you select which digit gets written to though as I can't see any indexing for the display.

the indexing happens in the beginning, the d1 - d4 are the digits, but i don't think you mean this, right?

but it is like i said in the beginning, it was originally from an watchdogs timer project on instructables. and it worked, so i wanted ti make it count up if it receives an signal of an IR sensor. actually just a counting devise with a led display instead of an lcd.

 

https://www.instructables.com/id/Arduino-Time-BOMB-Airsoft-Using-the-KYX-5461AS-4-D/

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×