fezder
Well-Known Member
Ok, i suppose it's not surpise that i have problem with code, probadly, as it at least works at some degree, but needs some fine tuning, which i was hoping you guys could help out. So, i decided to start practicing with shift registers, 74595, SIPO. So far, i'we managed to make it count 00-99 and roll around. BUT the same problem persist as my previous counter-when i try to scale numbers 00-99 with potentiometer, it stops at 98!. There is something way over my head, as my experience with coding is well, limited to say the least, but more experienced on hardware side, circuits i mean.
Help would be appreciated, and umm, i'd appreciate that you'd ackowledge the fact mine coding language is only slightly better than mine swedish, i understand bits from there and there, but not the whole idea.
but, to make your job a lot easier, here's the code (probadly has bunch of ''erros'', feel free to tell 'em, or to tell suggestions how to make things more nice'n tidy) some crayed-out sections are from previous counter scetch, hopefully they don't confuse you....
Help would be appreciated, and umm, i'd appreciate that you'd ackowledge the fact mine coding language is only slightly better than mine swedish, i understand bits from there and there, but not the whole idea.
but, to make your job a lot easier, here's the code (probadly has bunch of ''erros'', feel free to tell 'em, or to tell suggestions how to make things more nice'n tidy) some crayed-out sections are from previous counter scetch, hopefully they don't confuse you....
C:
int dataPin = 2; //Define which pins will be used for the Shift Register control
int latchPin = 3;
int clockPin = 4;
//long n = 0;
int x = 100;
int del = 10;
void setup(){
// clearLEDs();
// testLEDs();
pinMode(4, OUTPUT);
pinMode(3, OUTPUT);
pinMode(2, OUTPUT);
}
void loop(){
int potval = analogRead(A0);
int n = map(potval, 0, 1023, 0, 9999);
clearLEDs();
pickDigit(1);
pickNumber((n/x/1000)%10);
delayMicroseconds(del);
clearLEDs();
pickDigit(2);
pickNumber((n/x/100)%10);
delayMicroseconds(del);
clearLEDs();
pickDigit(3);
pickNumber((n/x/10)%10);
delayMicroseconds(del);
clearLEDs();
pickDigit(4);
pickNumber(n/x % 10);
delayMicroseconds(del);
/*if(n/x > 9999){
n = 0;
delay(5000);
}
else n++;*/
}
void pickDigit(int x){
digitalWrite( 6, LOW); // dig 3
digitalWrite( 5, LOW); // dig 4
switch(x){
case 3: digitalWrite(6, HIGH); break;
case 4: digitalWrite(5, HIGH); break;
}
}
void pickNumber(int x){
switch(x){
case 1: one(); break;
case 2: two(); break;
case 3: three(); break;
case 4: four(); break;
case 5: five(); break;
case 6: six(); break;
case 7: seven(); break;
case 8: eight(); break;
case 9: nine(); break;
default: zero(); break;
}
}
void clearLEDs ()
{
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, LSBFIRST, B00000000);
digitalWrite (latchPin, HIGH);
}
void one()
{
digitalWrite(latchPin, LOW); //Pull latch LOW to start sending data
shiftOut(dataPin, clockPin, LSBFIRST, B01100000); //Send the data 1
digitalWrite(latchPin, HIGH); //Pull latch HIGH to stop sending data
}
void two()
{
digitalWrite(latchPin, LOW); //Pull latch LOW to start sending data
shiftOut(dataPin, clockPin, LSBFIRST, B11011010); //Send the data 2
digitalWrite(latchPin, HIGH); //Pull latch HIGH to stop sending data
}
void three()
{
digitalWrite(latchPin, LOW); //Pull latch LOW to start sending data
shiftOut(dataPin, clockPin, LSBFIRST, B11110010); //Send the data 3
digitalWrite(latchPin, HIGH); //Pull latch HIGH to stop sending data
}
void four()
{
digitalWrite(latchPin, LOW); //Pull latch LOW to start sending data
shiftOut(dataPin, clockPin, LSBFIRST, B01100110); //Send the data 4
digitalWrite(latchPin, HIGH); //Pull latch HIGH to stop sending data
}
void five()
{
digitalWrite(latchPin, LOW); //Pull latch LOW to start sending data
shiftOut(dataPin, clockPin, LSBFIRST, B10110110); //Send the data 5
digitalWrite(latchPin, HIGH); //Pull latch HIGH to stop sending data
}
void six()
{
digitalWrite(latchPin, LOW); //Pull latch LOW to start sending data
shiftOut(dataPin, clockPin, LSBFIRST, B10111110); //Send the data 6
digitalWrite(latchPin, HIGH); //Pull latch HIGH to stop sending data
}
void seven()
{
digitalWrite(latchPin, LOW); //Pull latch LOW to start sending data
shiftOut(dataPin, clockPin, LSBFIRST, B11100000); //Send the data 7
digitalWrite(latchPin, HIGH); //Pull latch HIGH to stop sending data
}
void eight()
{
digitalWrite(latchPin, LOW); //Pull latch LOW to start sending data
shiftOut(dataPin, clockPin, LSBFIRST, B11111110); //Send the data 8
digitalWrite(latchPin, HIGH); //Pull latch HIGH to stop sending data
}
void nine()
{
digitalWrite(latchPin, LOW); //Pull latch LOW to start sending data
shiftOut(dataPin, clockPin, LSBFIRST, B11110110); //Send the data 9
digitalWrite(latchPin, HIGH); //Pull latch HIGH to stop sending data
}
void zero()
{
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, LSBFIRST, B11111100);
digitalWrite(latchPin, HIGH);
}
Last edited by a moderator: