Voting resources, early voting, and poll worker information - VOTE. ... Adafruit is open and shipping.
0

Ok, more programming help........
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Re: Ok, more programming help........

by cashman04 on Mon Nov 29, 2010 11:37 pm

I got it working. I just changed the pin the temp sensor was wired into to analog 5. I had to make a few other changes in my code(forgot to change one of my map functions to temp). So now I'm getting my temp value fine.

I was really just wondered if I am using an analog pin 2, and a digital pin 2, how does it know the difference? They both just equal 2 when defining pins.
cashman04
 
Posts: 25
Joined: Fri Nov 19, 2010 7:04 pm

Re: Ok, more programming help........

by stinkbutt on Tue Nov 30, 2010 12:33 am

cashman04 wrote:I was really just wondered if I am using an analog pin 2, and a digital pin 2, how does it know the difference? They both just equal 2 when defining pins.


No idea. Post your code and I'll be able to tell you, though.
Red M&M, Blue M&M: They all wind up the same color

stinkbutt
 
Posts: 593
Joined: Wed Feb 17, 2010 2:40 am

Re: Ok, more programming help........

by adafruit_support_bill on Tue Nov 30, 2010 6:55 am

if I am using an analog pin 2, and a digital pin 2, how does it know the difference?

AnalogRead assumes that the pin parameter is an analog pin.
DigitalRead and DigitalWrite assume that the pin parameter is a digital pin.
You can use analog pins as digital pins, for DigitalRead and DigitalWrite, but you have to use pin numbers are 14-19.

AnalogWrite is the odd-ball - It assumes that the pin parameter is one of the digital PWM pins.

adafruit_support_bill
 
Posts: 79082
Joined: Sat Feb 07, 2009 10:11 am

Re: Ok, more programming help........

by cashman04 on Wed Dec 01, 2010 2:21 am

I figured analog and digital reads were what differentiated it. I just wasn't sure if there was anything else code-wise I need to do to specify the difference between digital pin 2 and analog pin 2 or such when using both.
cashman04
 
Posts: 25
Joined: Fri Nov 19, 2010 7:04 pm

Re: Ok, more programming help........

by adafruit_support_bill on Wed Dec 01, 2010 7:03 am

Analog pin 2 would be digital pin 16.

adafruit_support_bill
 
Posts: 79082
Joined: Sat Feb 07, 2009 10:11 am

Re: Ok, more programming help........

by cashman04 on Fri Dec 03, 2010 8:42 pm

Allrighty then, got another one. Been tinkering with code on this for a few hours but can't get this hammered out to work.

What I am trying to do now is take a 1-4 digit number, and have it pull the numbers from my font table to match it up.

like-

value = 1234

and have the code take it and know to do this

val1 = font_table[1]
val2 = font_table[2]
val3 = font_table[3]
val4 = font_table[4]

and for numbers with less than 4 digits, just return a blank value so nothing is displayed


here is what I have so far, just mainly recycled from what all has been shown to me so far.

Code: Select all | TOGGLE FULL SIZE
const int dataPin = 2;
const int clockPin = 3;
const int latchPin = 4;

const int digit1 = 5;
const int digit2 = 6;
const int digit3 = 7;
const int digit4 = 8;


const byte font_table[] =
{
   B01000000,
   B11111001,
   B10100100,
   B10110000,
   B10011001,
   B10010010,
   B10000011,
   B11111000,
   B10000000,
   B10011000
};

void setup() {

  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(dataPin, OUTPUT);
  pinMode(digit1, OUTPUT);
  pinMode(digit2, OUTPUT);
  pinMode(digit3, OUTPUT);
  pinMode(digit4, OUTPUT);

}


byte map_analog(const int an)
{
   
   if((an < 0) || (an >= (sizeof(font_table) / sizeof(font_table[1])))) return 0;
   return font_table[an]; }
   
   
   void loop() {
   
     
    int val1 = 1;
    int val2 = 8;
    int val3 = 8;
    int val4 = 5;
   
   
   
   
   


    digitalWrite(digit1, HIGH);
    digitalWrite(latchPin, LOW);
    shiftOut(dataPin, clockPin, MSBFIRST, map_analog(val1));
    digitalWrite(latchPin, HIGH);
    digitalWrite(digit1, LOW);
   
    digitalWrite(digit2, HIGH);
    digitalWrite(latchPin, LOW);
    shiftOut(dataPin, clockPin, MSBFIRST, map_analog(val2));
    digitalWrite(latchPin, HIGH);
    digitalWrite(digit2, LOW);
   
    digitalWrite(digit3, HIGH);
    digitalWrite(latchPin, LOW);
    shiftOut(dataPin, clockPin, MSBFIRST, map_analog(val3));
    digitalWrite(latchPin, HIGH);
    digitalWrite(digit3, LOW);
   
    digitalWrite(digit4, HIGH);
    digitalWrite(latchPin, LOW);
    shiftOut(dataPin, clockPin, MSBFIRST, map_analog(val4));
    digitalWrite(latchPin, HIGH);
    digitalWrite(digit4, LOW);
 
 
}
cashman04
 
Posts: 25
Joined: Fri Nov 19, 2010 7:04 pm

Re: Ok, more programming help........

by stinkbutt on Fri Dec 03, 2010 10:48 pm

Use the modulus (%) operator. For example:

Code: Select all | TOGGLE FULL SIZE
int x = 157;
int ones, tens, hundreds, thousands;

ones = x % 10;
tens = ( x / 10 ) % 10;              // Taking advantage of truncation in integer division...
hundreds = ( x / 100 ) % 10;     // ibid
thousands = ( x / 1000 ) % 10;  // ibid

// Begin leading zero removal code //
if( thousands == 0 )
{
  thousands = 10;                     // You're going to need to change font_table
  if( hundreds == 0 )                 // Nested if, only check if thousands is also zero.
  {
    hundreds = 10;
    if( tens == 0 )                     // Nested if, only check if hundreds (and by implication, thousands) is also zero.
    {
      tens = 10;
    }
  }
}

Remember in integer division they just drop the fraction, meaning 157 becomes 15, working out nicely for us in this case.

The modulus operator gives you the remainder of a division operation, so 16 % 10 = 6, 17 % 10 = 7, 17 % 4 = 1, etc...

Now, before this'll work, you'll need to change font_table here as well, adding one additional byte to the end of the array, a zero:

Code: Select all | TOGGLE FULL SIZE
byte[] font_table =
{
B10111011,
B00001010,
B01110011,
B01011011,
B11001010,
B11011001,
B11111000,
B00001011,
B11111011,
B11011011,
B00000000
}

Remember in arrays the count starts at zero, so the eleventh (11th) byte in the array is font_table[10]. And because that byte is all zeroes, the LED display is all off. So for the thousands variable, (and only the thousands variable here,) we set it to 10 instead of 0 because we want it to be blank for the leading zeroes.
Last edited by stinkbutt on Sat Dec 04, 2010 4:53 am, edited 3 times in total.
Red M&M, Blue M&M: They all wind up the same color

stinkbutt
 
Posts: 593
Joined: Wed Feb 17, 2010 2:40 am

Re: Ok, more programming help........

by cashman04 on Sat Dec 04, 2010 1:43 am

Ahh, I see it. I thought it had something to do with the %. I had seen other code that had that with that, just didn't understand what was happening. Makes perfect sense now.

Is there any way to define this
Code: Select all | TOGGLE FULL SIZE
digitalWrite(digit1, HIGH);
    digitalWrite(latchPin, LOW);
    shiftOut(dataPin, clockPin, MSBFIRST, map_analog(dig1));
    digitalWrite(latchPin, HIGH);
    digitalWrite(digit1, LOW);


as a function outside of the void loop that I can call too.

The only reason I ask is to see if I can speed it up any. So it loops through faster. With the multiplexing it is still a little dim, and I can see just a trace of ghosting on unlit segments.
Unless thats not the way it works, and I need to set a repeat timer around it.
cashman04
 
Posts: 25
Joined: Fri Nov 19, 2010 7:04 pm

Re: Ok, more programming help........

by stinkbutt on Sat Dec 04, 2010 4:46 am

cashman04 wrote:Is there any way to define this
Code: Select all | TOGGLE FULL SIZE
digitalWrite(digit1, HIGH);
    digitalWrite(latchPin, LOW);
    shiftOut(dataPin, clockPin, MSBFIRST, map_analog(dig1));
    digitalWrite(latchPin, HIGH);
    digitalWrite(digit1, LOW);


as a function outside of the void loop that I can call too.

The only reason I ask is to see if I can speed it up any. So it loops through faster. With the multiplexing it is still a little dim, and I can see just a trace of ghosting on unlit segments.
Unless thats not the way it works, and I need to set a repeat timer around it.


There might be, but it doesn't smell like that's the issue. Post the code in the loop() function. I suspect your duty cycle is too low, or your latch pin is screwy.
Red M&M, Blue M&M: They all wind up the same color

stinkbutt
 
Posts: 593
Joined: Wed Feb 17, 2010 2:40 am

Re: Ok, more programming help........

by mtbf0 on Sat Dec 04, 2010 9:01 am

you probably want to tuck the display code into a timer interrupt. unfortunately this is not specifically supported on the arduino, but it's not hard to do. you've got 3 timers each chugging along at ~1kHz. you just need to snag the overflow interrupt on timer1 or timer2 and run your display code for one digit every fourth time into the routine. that should give you each digit drawn about 60 times a second.

i'd love to carry on further, but i've got to head to work and i'm sure some helpful soul around here can get you straightened out before i get home.
"i want to lead a dissipate existence, play scratchy records and enjoy my decline" - iggy pop, i need more
User avatar
mtbf0
 
Posts: 1645
Joined: Sat Nov 10, 2007 12:59 am
Location: oakland ca

Re: Ok, more programming help........

by cashman04 on Sat Dec 04, 2010 3:12 pm

Heres my entire void loop

Code: Select all | TOGGLE FULL SIZE
void loop() {
   
 
     

   
   
   
   
   
   


    digitalWrite(digit1, HIGH);
    digitalWrite(latchPin, LOW);
    shiftOut(dataPin, clockPin, MSBFIRST, map_analog(dig1));
    digitalWrite(latchPin, HIGH);
    digitalWrite(digit1, LOW);
    delay(1);
 
    digitalWrite(digit2, HIGH);
    digitalWrite(latchPin, LOW);
    shiftOut(dataPin, clockPin, MSBFIRST, map_analog(dig2));
    digitalWrite(latchPin, HIGH);
    digitalWrite(digit2, LOW);
    delay(1);
   
    digitalWrite(digit3, HIGH);
    digitalWrite(latchPin, LOW);
    shiftOut(dataPin, clockPin, MSBFIRST, map_analog(dig3));
    digitalWrite(latchPin, HIGH);
    digitalWrite(digit3, LOW);
    delay(1);
   
    digitalWrite(digit4, HIGH);
    digitalWrite(latchPin, LOW);
    shiftOut(dataPin, clockPin, MSBFIRST, map_analog(dig4));
    digitalWrite(latchPin, HIGH);
    digitalWrite(digit4, LOW);
   
 delay(1);
 
 
 
 
 
 
 
 digitalWrite(digit5, HIGH);
    digitalWrite(latchPin1, LOW);
    shiftOut(dataPin, clockPin, MSBFIRST, map_analog(dig5));
    digitalWrite(latchPin1, HIGH);
    digitalWrite(digit5, LOW);
    delay(1);
 
    digitalWrite(digit6, HIGH);
    digitalWrite(latchPin1, LOW);
    shiftOut(dataPin, clockPin, MSBFIRST, map_analog(dig6));
    digitalWrite(latchPin1, HIGH);
    digitalWrite(digit6, LOW);
    delay(1);
   
    digitalWrite(digit7, HIGH);
    digitalWrite(latchPin1, LOW);
    shiftOut(dataPin, clockPin, MSBFIRST, map_analog(dig7));
    digitalWrite(latchPin1, HIGH);
    digitalWrite(digit7, LOW);
    delay(1);
   
    digitalWrite(digit8, HIGH);
    digitalWrite(latchPin1, LOW);
    shiftOut(dataPin, clockPin, MSBFIRST, map_analog(dig8));
    digitalWrite(latchPin1, HIGH);
    digitalWrite(digit8, LOW);
   
 delay(1);
 
}
cashman04
 
Posts: 25
Joined: Fri Nov 19, 2010 7:04 pm

Re: Ok, more programming help........

by stinkbutt on Sat Dec 04, 2010 4:32 pm

The code looks fine. I don't think that's the issue. Howabout your wiring? Are you going straight from the outputs of the shift register to the 7-segment LED? What's your supply voltage? What shift register are you actually using?
Red M&M, Blue M&M: They all wind up the same color

stinkbutt
 
Posts: 593
Joined: Wed Feb 17, 2010 2:40 am

Re: Ok, more programming help........

by cashman04 on Sat Dec 04, 2010 7:03 pm

I'm just gonna spend a bit re-wiring it. First off, I cheated and just put resistors on the input anodes of each of the digits, I know I should put them on the grounds, it just takes up much more room on a bread board to do that. But I'll make room.
Second, I ran both of the shift registers completely separately. I think I read you can essentially daisy chain them, so I'll do that this time. And finally, all the power was coming directly from the arduino, Im guessing I need to hook the board up to my external power source to save some juice out of the arduino.

Am I on the right track there?
cashman04
 
Posts: 25
Joined: Fri Nov 19, 2010 7:04 pm

Re: Ok, more programming help........

by stinkbutt on Sat Dec 04, 2010 7:32 pm

How big are the resistors? That could be it right there. And does thee 7 segment have internal current limiting resistors? What's the part #?

Don't stress about chaining the shift registers - it's a lot of effort and you have pins to spare.

Though making sure your Vcc is ok is also a good idea. Again, what's the part on the 7 segment?
Red M&M, Blue M&M: They all wind up the same color

stinkbutt
 
Posts: 593
Joined: Wed Feb 17, 2010 2:40 am

Re: Ok, more programming help........

by cashman04 on Sat Dec 04, 2010 8:28 pm

YSD-439AB4B-35 is the part number. Using 560 ohm resistors. I though they may be overkill, but I have blown one of my smaller 7-segment displays cause of the wrong resistor last week. Just trying not to do that again. No idea if they have internal resistance.
cashman04
 
Posts: 25
Joined: Fri Nov 19, 2010 7:04 pm

Please be positive and constructive with your questions and comments.