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;
}
}
