Problem counting.

Im having a problem with my sketch where in my seven segment displays count in a messed up pattern.
starting with 10, then G0, 90, and so on.

I'm using IDE 1.8.7 (told I have to as new one crashes when it opens on my mac)

The Switches are replaced with Ir break beams... Break beam Sensor

One thing to note, on the sketch, "LedControl.h" is dark green and all the other libraries I add are not. (i was told this is not a problem)

Can anyone help my counter count correctly?

#include "LedControl.h"  // Library used for communcation with 7 segment

LedControl lc=LedControl(12,11,10,1);  //  (DIN, CLK, LOAD, number of Max7219 chips)

// Variable to hold current scores
int displayone=0;
int displaytwo=0;

// Variables to split whole number into single digits
int rightdigit;
int leftdigit;

// Switches pin connection to Arduino UNO
#define switchone 2
#define switchtwo 3


void setup() {
  pinMode(switchone,INPUT_PULLUP);
  pinMode(switchtwo,INPUT_PULLUP);
  
  lc.shutdown(0,false);  // Wake up MAX7219

  lc.setIntensity(0,7);  // Set brightness to medium

  lc.clearDisplay(0);  // Clear all displays connected to MAX7219 chip #

// Put zeros on both displays at startup
  
  lc.setDigit(0,0,0,false);  // (Max7219 chip #, Digit, value, DP on or off)
  lc.setDigit(0,1,0,false);
  
  lc.setDigit(0,2,0,false);
  lc.setDigit(0,3,0,false);

}


void loop() { 

  // If switch 1 is clicked
  if (!digitalRead(switchone)) {
    
    displayone++;  // Increase score by 1
  
    // convert whole number to single digits
    rightdigit=displayone%10;
    leftdigit=displayone%100/10;

    // Display extracted digits on the display
    lc.setDigit(0,0,leftdigit,false);
    lc.setDigit(0,1,rightdigit,false);

    
    // Wait until switch is released to continue
    while (!digitalRead(switchone)) { 
    }
    delay(5);  // Small delay to debounce the switch
  }

    if (!digitalRead(switchtwo)) {
      
    displaytwo++;
  
    rightdigit=displaytwo%10;
    leftdigit=displaytwo%100/10;

    lc.setDigit(0,2,leftdigit,false);
    lc.setDigit(0,3,rightdigit,false);

    while (!digitalRead(switchtwo)) { 
    }    
    delay(5);
  }
}

The coloring of the words in the editor is only "mostly" helpful. It is not able to look at which libraries you are currently using, so it colors all keywords that appear in any library anywhere on your system.

Is this plugged into your computer for testing? Can you use Serial.print() to check the values of displayone and displaytwo?

MorganS:
The coloring of the words in the editor is only "mostly" helpful. It is not able to look at which libraries you are currently using, so it colors all keywords that appear in any library anywhere on your system.

Is this plugged into your computer for testing? Can you use Serial.print() to check the values of displayone and displaytwo?

yes its plugged into the computer, but To be honest I'm rather new to this so I'm not sure how to do that.

I figured the color code did not mean all that much in regards to this issue but I thought I would mention it incase it was something I had over looked.

BubbleHockey:
I'm using IDE 1.8.7 (told I have to as new one crashes when it opens on my mac)

Unrelated to your question, but you should be aware that the bug with macOS versions older than 10.12 that affected Arduino IDE 1.8.8 has been resolved in the Arduino IDE 1.8.9 release. If that's the cause of the crash you were having, you shouldn't feel that you're stuck on 1.8.7 anymore.

pert:
Unrelated to your question, but you should be aware that the bug with macOS versions older than 10.12 that affected Arduino IDE 1.8.8 has been resolved in the Arduino IDE 1.8.9 release. If that's the cause of the crash you were having, you shouldn't feel that you're stuck on 1.8.7 anymore.

I did not know it had been fixed, thank you, However that did not clear up the issue.

I took a min but I figured out that with both sides, whats happening is that it IS counting up. BUT the "ones" are in the "tens" spot. IE the numbers on the display are backwards.

and in addition, the "f" segment on the "tens" spot (what i think is digit two) is lighting up when it should not and NOT lighting when it should. turning a 2 into a 9, and a sideways t instead of a 4.

anyone have any idea as to what would cause this?

BubbleHockey:
However that did not clear up the issue.

Sorry, I didn't mean to imply that your use of 1.8.7 was in any way related to your problem, or that updating to 1.8.9 had any hope of fixing it.

BUT the "ones" are in the "tens" spot. IE the numbers on the display are backwards.

I recommend that you run the library example. "LCDemo7Segment"

I think that you have the digits backwards. From left to right the digits position is 3-2-1-0 You will also see if there are defective elements.

cattledog:
I recommend that you run the library example. "LCDemo7Segment"

I think that you have the digits backwards. From left to right the digits position is 3-2-1-0 You will also see if there are defective elements.

How would I switch the digits in the above sketch?

the max7219 is running 2, 2digit seven segment displays.

I tried playing around a bit with "lc.setDigit(0,2,rightdigit,false);" switching the right for the left, and tho it worked on one of the displays, the second set of displays did not change even tho it two was switched in the sketch,

and even with the "f" segment was still (and not) lighting up when its not supposed to, as before.

I also tried a different max7219 chip, and the behaves the same. I tested the segments on the displays and they all seem to be functioning normally.

BubbleHockey:
I tried playing around a bit with "lc.setDigit(0,2,rightdigit,false);" switching the right for the left, and tho it worked on one of the displays, the second set of displays did not change even tho it two was switched in the sketch,

Show your updated sketch.

evanmars:
Show your updated sketch.

#include <LedControl.h>

LedControl lc=LedControl(12,11,10,1); // (DIN, CLK, LOAD, number of Max7219 chips)

// Variable to hold current scores
int displayone=0;
int displaytwo=0;

// Variables to split whole number into single digits
int rightdigit;
int leftdigit;

// Switches pin connection to Arduino UNO
#define switchone 2
#define switchtwo 3

void setup() {
pinMode(switchone,INPUT_PULLUP);
pinMode(switchtwo,INPUT_PULLUP);

lc.shutdown(0,false); // Wake up MAX7219

lc.setIntensity(0,7); // Set brightness to medium

lc.clearDisplay(0); // Clear all displays connected to MAX7219 chip #

// Put zeros on both displays at startup

lc.setDigit(0,0,0,false); // (Max7219 chip #, Digit, value, DP on or off)
lc.setDigit(0,1,0,false);

lc.setDigit(0,2,0,false);
lc.setDigit(0,3,0,false);

}

void loop() {

// If switch 1 is clicked
if (!digitalRead(switchone)) {

displayone++; // Increase score by 1

// convert whole number to single digits
rightdigit=displayone%100/10;
leftdigit=displaytwo%10;

// Display extracted digits on the display
lc.setDigit(0,0,rightdigit,false);
lc.setDigit(0,1,leftdigit,false);

// Wait until switch is released to continue
while (!digitalRead(switchone)) {
}
delay(5); // Small delay to debounce the switch
}

if (!digitalRead(switchtwo)) {

displaytwo++;

rightdigit=displaytwo%100/10;
leftdigit=displaytwo%10;

lc.setDigit(0,2,rightdigit,false);
lc.setDigit(0,3,leftdigit,false);

while (!digitalRead(switchtwo)) {
}
delay(5);
}
}

I think you have your indexes correct 3-2-1-0 for the left right display, but you have made errors in the extraction of the left (10's)digit and right(ones) digits from a two digit number. Modulo 10 gets the ones digit, and /10 uses integer math truncation to get the tens digit. You do not need the %100.

Are you considering left and right from the point of view of an observer looking at the display?

if (!digitalRead(switchone)) {
    
    displayone++;  // Increase score by 1
  
    // convert whole number to single digits
    
     //rightdigit=displayone%100/10;
    //leftdigit=displaytwo%10; //typo here or else error using displaytwo
   
     rightdigit = displayone%10;
     leftdigit = displayone/10;
    // Display extracted digits on the display
    lc.setDigit(0,0,rightdigit,false);
    lc.setDigit(0,1,leftdigit,false);

And for the other pair

if (!digitalRead(switchtwo)) {
      
    displaytwo++;
  
    //rightdigit=displaytwo%100/10;
    //leftdigit=displaytwo%10;

    rightdigit = displaytwo%10;
    leftdigit = displaytwo/10;

    lc.setDigit(0,2,rightdigit,false);
    lc.setDigit(0,3,leftdigit,false);

cattledog:
I think you have your indexes correct 3-2-1-0 for the left right display, but you have made errors in the extraction of the left (10's)digit and right(ones) digits from a two digit number. Modulo 10 gets the ones digit, and /10 uses integer math truncation to get the tens digit. You do not need the %100.

Are you considering left and right from the point of view of an observer looking at the display?

if (!digitalRead(switchone)) {

displayone++;  // Increase score by 1
 
    // convert whole number to single digits
   
    //rightdigit=displayone%100/10;
    //leftdigit=displaytwo%10; //typo here or else error using displaytwo
 
    rightdigit = displayone%10;
    leftdigit = displayone/10;
    // Display extracted digits on the display
    lc.setDigit(0,0,rightdigit,false);
    lc.setDigit(0,1,leftdigit,false);




And for the other pair


if (!digitalRead(switchtwo)) {
     
    displaytwo++;
 
    //rightdigit=displaytwo%100/10;
    //leftdigit=displaytwo%10;

rightdigit = displaytwo%10;
    leftdigit = displaytwo/10;

lc.setDigit(0,2,rightdigit,false);
    lc.setDigit(0,3,leftdigit,false);

Thank you! I realized the problem just before I read this. and that is exactly the solution I came up with.

the "rightdigit=displayone%100/10;" and the "leftdigit=desplayone%10;" needed to be switched for the digits to be in the right place.
And I solved the "f" segment problem. I had the SEG F pin on the wrong segment. (I overlooked it several times)

thanks for your help.