Variable velocity counter

Hi everyone.
I’m new with arduino and i would like to resolve a problem with my little project.
I need to do this variable velocity counter using a 7 segment display, a decoder bcd to 7 segment display (SN7448N),a multiplexer(CD4067) and 10 buttons.
I have to acquire the 10 buttons (i can press more than one in the same time) with the multiplexer then the information that i get from the output of the multiplexer goes to arduino and it does a sum of the buttons i have pressed.
This sum decides how fast the display is (it counts from 0 to 9 and restarts).
My problem is that when i start the program the sum starts at 55 (because 1+2+3+4+5+6+7+8+9+10) and when i press like the 5th button it does a 55 - button pressed, in this case 50, but i want that when i start the program the sum is 0 and when i press the buttons the sum increases.
I attach the program and how i built the circuit.

 int SPin[4] = {7,6,5,4};                                               // A = 4, B = 5, C = 6, D = 7 pin for selection of the multiplexer
  int WPin = 2;                                                          // Output of multiplexer
  int WState = HIGH;
  int BCD[10][4] =
  {
    {0,0,0,0},                                                           // 0
    {0,0,0,1},                                                           // 1
    {0,0,1,0},                                                           // 2
    {0,0,1,1},                                                           // 3
    {0,1,0,0},                                                           // 4
    {0,1,0,1},                                                           // 5
    {0,1,1,0},                                                           // 6
    {0,1,1,1},                                                           // 7
    {1,0,0,0},                                                           // 8
    {1,0,0,1}                                                            // 9
  };
    int BCD2[10][4] =
  {
    {0,0,0,0},                                                           // 0
    {0,0,0,1},                                                           // 1
    {0,0,1,0},                                                           // 2
    {0,0,1,1},                                                           // 3
    {0,1,0,0},                                                           // 4
    {0,1,0,1},                                                           // 5
    {0,1,1,0},                                                           // 6
    {0,1,1,1},                                                           // 7
    {1,0,0,0},                                                           // 8
    {1,0,0,1}                                                            // 9
  };
  int sBCD = 0;
  int sum = 0;
  int NPin[4] = {13,12,11,10};                                           // A = 10, B = 11, C = 12, D = 13 pin used for decoder
  int line = 0;
  int vel = 0;                                                           // speed of the display
  int t = 10000;                                                         // time


void setup()
{
  for(int a = 0; a < 4; a++)                                             
  {
    pinMode(NPin[a], OUTPUT);                                            // The pins 13, 12, 11, 10 are the output for the decoder
    pinMode(SPin[a], OUTPUT);                                            // The pins 7, 6, 5, 4 are the output for the decoder
  }
  pinMode(WPin, INPUT);                                                  // Il pin 2 is the input for the multiplexer
  Serial.begin(9600);                                                    
}

void loop()
{
  for(line = 0; line < 10; line++)                                   
  {
    while(sum == 0)
    {
      for(sBCD = 0; sBCD < 10; sBCD++)                                   // Cycle that checks which buttons are pressed
      {
        for(int b = 0; b < 4; b++)
        digitalWrite(SPin[b], BCD[sBCD][b]);
        WState = digitalRead(WPin);                                      
        if(WState == HIGH)
        sum = sum + sBCD+ 1;                                        
      }
    }
    
    
    Serial.print("sum = ");                                            
    Serial.print(sum);
    Serial.print('\n');
    vel = t / sum;                                                     
    for(int column = 0; column < 4; column++)
    digitalWrite(NPin[column], BCD2[line][column]);                    
    delay(vel);                                                          
    sum = 0;                                                           
  }
  
 }

Image from Original Post so we don’t have to download it. See Image Guide

8740491c95826a217200c82d7f98e7ea8eacb323.png

…R

    while(sum == 0)
    {
      for(sBCD = 0; sBCD < 10; sBCD++)                                   // Cycle that checks which buttons are pressed
      {
        for(int b = 0; b < 4; b++)

What is the purpose of the WHILE?

What does the second FOR do?

IMHO it is much easier to follow code if {} are used even when not strictly necessary. And when you post code here you want other people to be able to follow it.

If you want to distinguish which buttons are pressed when several may be pressed at the same time you need to allocate power-of-2 values – 2,4,8,16 etc. Then the addition of the values always gives a unique result.

…R

Thanks for your response. Sorry for attaching the image in the wrong way. I use the second for because it is a matrix so i have to distinguish line and column, i use the while so i'm sure that at the start it will enter the other for and if no button is pressed it doesn't go on. I don't need to distinguish the buttons, but each one has a number from 1 to 10 but they're automatically assigned by the multiplexer.

When I asked you the purpose of the 2nd FOR I intended that you study the code to see if it is doing what you think. There are no {} associated with it so I can't figure it out.

...R

Sorry for misunderstanding.
I re-write the code.

void setup()
{
  for(int a = 0; a < 4; a++)                                             
  {
    pinMode(NPin[a], OUTPUT);                                            // The pins 13, 12, 11, 10 are the output for the decoder
    pinMode(SPin[a], OUTPUT);                                            // The pins 7, 6, 5, 4 are the output for the decoder
  }
  pinMode(WPin, INPUT);                                                  // Il pin 2 is the input for the multiplexer
  Serial.begin(9600);                                                    
}

void loop()
{
  for(line = 0; line < 10; line++)                                   
  {
    while(sum == 0)
    {
      for(sBCD = 0; sBCD < 10; sBCD++)                                   // Cycle that checks which buttons are pressed
      {
        for(int b = 0; b < 4; b++){
        digitalWrite(SPin[b], BCD[sBCD][b]);
        }
        WState = digitalRead(WPin);                                      
        if(WState == HIGH)
        sum = sum + sBCD+ 1;                                        
      }
    }
    
    
    Serial.print("sum = ");                                            
    Serial.print(sum);
    Serial.print('\n');
    vel = t / sum;                                                     
    for(int column = 0; column < 4; column++)
    digitalWrite(NPin[column], BCD2[line][column]);                    
    delay(vel);                                                          
    sum = 0;                                                           
  }
  
 }

You have not posted a complete program and I am not going to do cutting and pasting.

I can't see what part of the code gives rise to this comment

My problem is that when i start the program the sum starts at 55 (because 1+2+3+4+5+6+7+8+9+10) and when i press like the 5th button it does a 55 - button pressed, in this case 50

As far as I can see sum is intialized at 0.

Maybe the way the multiplexer is set up it is giving you values the wrong way round - such as 1 when it is NOT pressed and 0 when it is. If so it should be easy to modify the code to deal with that.

Write a short program to show the results from the multiplexer on the Serial Monitor.

...R

Thanks for your response. I will try that tomorrow because i have to go to sleep. Sorry for not posting the entire code. The program that you mean is what the output of the mux gives?

Yoder99:
The program that you mean is what the output of the mux gives?

Yes.

Having the facts is frequently useful.

…R

Hi, thanks for all your responses.
I resolved the problem and now the sum starts at 0 and when i press the buttons it increases.
Now i would like to know how to do this: when i press the buttons the speed of the display remains constant and not like now, because now i can only count pressing constantly the buttons.
I repost the new code:

int SPin[4] = {7,6,5,4};                                               // A = 4, B = 5, C = 6, D = 7 pin for selection of the multiplexer
  int WPin = 2;                                                          // Output of multiplexer
  int WState = HIGH;
  int BCD[10][4] =
  {
    {0,0,0,0},                                                           // 0
    {0,0,0,1},                                                           // 1
    {0,0,1,0},                                                           // 2
    {0,0,1,1},                                                           // 3
    {0,1,0,0},                                                           // 4
    {0,1,0,1},                                                           // 5
    {0,1,1,0},                                                           // 6
    {0,1,1,1},                                                           // 7
    {1,0,0,0},                                                           // 8
    {1,0,0,1}                                                            // 9
  };
    int BCD2[10][4] =
  {
    {0,0,0,0},                                                           // 0
    {0,0,0,1},                                                           // 1
    {0,0,1,0},                                                           // 2
    {0,0,1,1},                                                           // 3
    {0,1,0,0},                                                           // 4
    {0,1,0,1},                                                           // 5
    {0,1,1,0},                                                           // 6
    {0,1,1,1},                                                           // 7
    {1,0,0,0},                                                           // 8
    {1,0,0,1}                                                            // 9
  };
  int sBCD = 0;
  int sum = 0;
  int NPin[4] = {13,12,11,10};                                           // A = 10, B = 11, C = 12, D = 13 pin used for decoder
  int line = 0;
  int vel = 0;                                                           // speed of the display
  int t = 10000;                                                         // time


void setup()
{
  for(int a = 0; a < 4; a++)                                             
  {
    pinMode(NPin[a], OUTPUT);                                            // The pins 13, 12, 11, 10 are the output for the decoder
    pinMode(SPin[a], OUTPUT);                                            // The pins 7, 6, 5, 4 are the output for the decoder
  }
  pinMode(WPin, INPUT);                                                  // Il pin 2 is the input for the multiplexer
  Serial.begin(9600);                                                    
}

void loop()
{
  for(line = 0; line < 10; line++)                                   
  {
    while(sum == 0)
    {
      for(sBCD = 0; sBCD < 10; sBCD++)                                   // Cycle that checks which buttons are pressed
      {
        for(int b = 0; b < 4; b++){
        digitalWrite(SPin[b], BCD[sBCD][b]);
        }
        WState = digitalRead(WPin);                                      
        if(WState == LOW)
        sum = sum + sBCD+ 1;                                        
      }
    }
    
    
    Serial.print("sum = ");                                            
    Serial.print(sum);
    Serial.print('\n');
    vel = t / sum;                                                     
    for(int column = 0; column < 4; column++)
    digitalWrite(NPin[column], BCD2[line][column]);                    
    delay(vel);                                                          
    sum = 0;                                                           
  }
  
 }

Yoder99:
when i press the buttons the speed of the display remains constant and not like now, because now i can only count pressing constantly the buttons.

I don’t understand.

…R

Sorry i'm italian my english is not that good, i redo the phrase. When i press the buttons i would like the program to do the sum and mantain it so the count would go on,instead now when i press the button it does only 1 sum so if i don't keep pressed the buttons the count doesn't go on. I hope you will understand.

Am I correct to think that you want to press the button (say Button 5) and even when you release the button it should keep adding 5 to the stored number continuously?

How would you stop the count?

I suspect I am not correct because that would be a strange way to use a keypad.

Perhaps you can provide a numeric example of what you mean?

...R

I do an example: If i press the button 5 and 4 the sum is 9, i would like that the sum 9 would continue to be there so the count on the display continues and when i press other buttons the velocity changes. Tell me if i wasn't clear, thanks anyway.

Then you need a variable to record the keypad value - perhaps call it currentKeyValue.

The keypad should be used to change that variable - for example to give it the number 9 as you mention in Reply #13.

I'm not sure what the Arduino sees when the keypad is not pressed - but you need to ensure that that is ignored so that it does not change currentKeyValue back to 0.

The code for updating the display should use the value in currentkeyValue to increment its display. The only link between the two parts of the code is the variable.

...R

Thanks for the response, will try that tomorrow.

Hi We seem to be puzzled by the term "velocity" it means speed in English. Do you mean Answer? Riposta?

What is the Italian word that you use, please?

Tom.... :)

Yes my bad...i use "velocità" and "risposta", thanks for correcting me.