Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Problem with code (surprise....)

Status
Not open for further replies.

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....



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:
First! Its easier to read with code tags.... Second, the 0~9 functions should really been just 1 function with a single parameter..
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);
  outleds(0);
  pickDigit(1);
  pickNumber((n/x/1000)%10);
  delayMicroseconds(del);
  outleds(0);
  pickDigit(2);
  pickNumber((n/x/100)%10);
  delayMicroseconds(del);
  outleds(0);
  pickDigit(3);
  pickNumber((n/x/10)%10);
  delayMicroseconds(del);
outleds(0);
  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: outleds(B01100000); break;
  case 2: outleds(B11011010); break;
  case 3: outleds( B11110010); break;
  case 4: outleds(B01100110); break;
  case 5: outleds(B10110110); break;
  case 6: outleds(B10111110); break;
  case 7: outleds(B11100000); break;
  case 8: outleds(B11111110); break;
  case 9: outleds(B11110110); break;
  default: outleds(B11111100); break;
  }
}
void outleds (unsigned char x)
{
  digitalWrite(latchPin, LOW);
  shiftOut(dataPin, clockPin, LSBFIRST, x);
  digitalWrite (latchPin, HIGH);
}

Much smaller tidier code... Now I'll analyze it
 
Last edited:
Aah, never knew about adding code with code tags, much clearer indeed...
And, i see that you ''shrinked'' code by a lot, took a while to see what you did. thanks a lot for helping new player with those tips!
 
well now, there doesn't seem to be problem after all....excepct, i did forgot one perhaps important component from circuit: bypass capacitor, for 74595's voltage supply pins now it goes smoothly all the way from 00 to 99.
what do you think?
 
well i can try, hopefully my camera is fast enought to catch any funny stuff
 
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top