Show Posts
Pages: [1] 2
1  Using Arduino / Project Guidance / Re: Max Current on Serial Pin TX - Uno on: July 27, 2014, 10:00:27 pm
Thanks for the reply. And I get to update this topic with some results.....

IT WORKS!!
I figured it would. But the reason I'm so excited here is that I tried forever to get it to. First, I wasn't getting anything to transfer out the TX line. I may be wrong here, but I think it had to do with the fact that the Arduino was still plugged into my laptop.

After I managed to get one AC adapter to power both (used really short jumpers), it failed to transfer data. I failed to get any stable readings on my DMM for a while. Turns out I was trying to ground the black lead to the only bus on my breadboard without a power connection. I also managed to find that I had to omit the pull-down resistor on the receiving end because the RX pin is holding it high. Which also means that I had to relocate the IR detector.

In the end, the RX pin was placed on one side of the detector, and the other side was directly grounded. But it left me in the spot that the data was going in inverted. Instead of "Test," I was getting "USt||". I tried to re-invert it with a 555 timer, but quickly realised why it wouldn't work. I ended up flipping the emitting diode around. And now it works.

So now, along with getting another AC adapter, I get to see how far away I can get data to transfer. I'll then use the magnifying glass to extend that distance. I then get to move to a parabolic mirror (or two).
2  Using Arduino / Project Guidance / Max Current on Serial Pin TX - Uno on: July 27, 2014, 06:51:03 pm
Question at the end of post.

I wasn't really sure where to post this question, so, Mods, feel free to move it. Anyways, I have two Uno's and a future project in mind. I want to create a satellite dish of sorts for transmitting data across the room (mostly because I like how such dishes look). I have found that they operate primarily on light (much like every single RC operated things). I will be using a magnifying glass with an LED on the sending end - the LED will be at the magnifying glass' focal point.

The receiving end will have a 10K pull-down resistor and a phototransistor connecting the RX pin to VCC. That Arduino will print the data to an LCD screen. Think of it like using a remote to turn the TV to another channel.

My question is: can the Arduino provide enough current for the LED  on the TX pin while the pin is configured for serial communication? I don't see why it shouldn't, but I don't want to take the chance.
3  Using Arduino / Project Guidance / Can't Get DAC to Work on: June 20, 2014, 12:35:53 pm
I got this DAC to drive a tri-color LED via Op-Amps. But for the life of me, I can't figure out why it's not working. I am using shiftOut to send data to it. But it will not update the DAC. I used a pocket o-scope to check if the Uno was sending data too fast. I calculated it was sending data at about 100 kHz, well below the 5 MHz limit. I probed the data pin, the /cs pin, all outputs, everything. I have /reset held high.

Code:
/* This script controls a three channel DAC (MAX-512[513]). The DAC's outputs should be fed to the inputs of an Op-Amp.
pinout:
1. data
2. /CS
3. clk
4. /reset
5. vdd - positive supply; bypass .22uF cap to gnd; 2.7V to 5.5V
6. gnd - gnd
7. vss - negative supply; 0V or -1.5 to -5.5V; short to gnd for single supply; if using a negative voltage, bypass to gnd with .22uF to gnd
8. a - buffered; resets to 255 on reset
9. b - buffered; resets to 255 on reset
10. c - unbuffered; resets to 0 on reset
11. ref-c - reference voltage sets the 255 voltage
12. ref-a/b
13. i.c. - internally connected; do not connect
14. Lout - logic output; latched

bit order (shutdown bits and register load bits are all active high):
1. D0
2. D1
3. D2
4. D3
5. D4
6. D5
7. D6
8. D7
9. load A
10. load B
11. load C
12. shutdown A
13. shutdown B
14. shutdown C
15. logic output
16. uncommitted

send control bits first. connect red to A, green to B, and blue to C.
*/

//initialize the colors
int red = random(0,256);
int green = random(0,256);
int blue = random(0,256);

int LL; //lower limit
int UL; //upper limit

//define pins
int data = 2;
int cs = 3;
int clk = 4;

char method = MSBFIRST;

void setup(){
  pinMode(data,OUTPUT);
  pinMode(cs,OUTPUT);
  pinMode(clk,OUTPUT);
  digitalWrite(clk,0); //data is shifted in on the rising edge
  digitalWrite(cs,1);
  delay(10);
  colorWrite(red,green,blue);
}

void loop(){
  red = rGenerate();
  blue = bGenerate();
  green = gGenerate();
  colorWrite(red,green,blue);
}

int rGenerate(){
  if(red > 4){
    LL = red-5;
  }
  else{
    LL = 0;
  }
 
  if(red < 251){
    UL = red+5;
  }
 
  else{
    UL = 256;
  }
 
  return random(LL,UL);
}

int gGenerate(){
  if(green > 4){
    LL = green-5;
  }
  else{
    LL = 0;
  }
 
  if(green < 251){
    UL = green+5;
  }
 
  else{
    UL = 256;
  }
 
  return random(LL,UL);
  //return random(0,6);
}

int bGenerate(){
  if(blue > 4){
    LL = blue-5;
  }
  else{
    LL = 0;
  }
 
  if(blue < 251){
    UL = blue+5;
  }
 
  else{
    UL = 256;
  }
 
  return random(LL,UL);
}


void colorWrite(int a,int b,int c){
//  digitalWrite(clk,0);
  digitalWrite(cs,0);
  shiftOut(data,clk,method,1); shiftOut(data,clk,method,a); //red
  digitalWrite(cs,1);
  delay(1);
  digitalWrite(cs,0);
  shiftOut(data,clk,method,2); shiftOut(data,clk,method,b); //green
  digitalWrite(cs,1);
  delay(1);
  digitalWrite(cs,0);
  shiftOut(data,clk,method,4); shiftOut(data,clk,method,c); //blue
  digitalWrite(cs,1);
}
4  Using Arduino / Project Guidance / Re: Help Making Timer Project; Function Fails on: June 17, 2014, 05:58:24 am
You mean like here, for example?
Code:
if(button==10||button==11)

I used 10 and 11 for * and #, respectively. In the array buttons[] (with an 's'), 10 and 11 do not correspond to a number, but to those symbols. Like here for example:
Code:
for(button=0;button<12;button++){ //all buttons are read on a Mux controlled by a shift regiter
      digitalWrite(en2,0); //enable pin for shift register
      shiftOut(sda,sck,LSBFIRST,buttons[button]); //the buttons[] array allows to scan the buttons in order (0-9, *, #)
      digitalWrite(en2,1);
      delay(1);

I'm using the number stored in button to scroll through the index for buttons. The number stored in the array for a given index is the address required to read the physical button for said index. The index is the button number we want, and the value at that index is where the button is. That's why it so out of order:
Code:
const int buttons[] = {8,3,11,7,2,10,6,1,9,5,0,4}; //0, 1, 2, 3, 4, 5, 6, 7, 8, 9, *, #
^note that has been edited. It would be the same as before, but I got two wires crossed up on the shift register... again.

That 30, it was just a random number I decided to use for a "stop what you're doing and go back" code.

Most of my issues were hardware related: not knowing that the enable pin on the mux was active low, trying to use the wrong bits on the shift register to control the mux, getting two wires crossed up on the shift register (shift registers - they'll only cause you trouble).

But it's finally working. I got all hardware bugs worked out (except one, which I worked around software wise), and all coding bugs worked out (I think). I may add a few more edits before calling it done.

Code:
// define numbers, buttons
const int numbers[] = {0b11111100,0b01100000,0b11011010,0b11110010,0b01100110,0b10110110,0b00111110,0b11100000,0b11111110,0b11100110}; // 0-9
const int buttons[] = {8,3,11,7,2,10,6,1,9,5,0,4}; //0, 1, 2, 3, 4, 5, 6, 7, 8, 9, *, #

// set the enable pins on both shift registers
const int en1 = 10; // set low when sending data, display
const int en2 = A2; // same as above, Mux

// these are the digits on the display
const int a1 = 2;
const int a2 = 3;
const int a3 = 4;
const int a4 = 5;

//data, clock, buttons, mux enable
const int sda = 7;
const int sck = 8;
const int sig = A0; //active LOW
const int me = A1;

//set the digits time
int a = 0; //a-d are defualt time
int b = 3;
int c = 0;
int d = 0;
int e = 0; //temporary storage
int f = 0; // ^
int g = 0; // ^^
int h = 0; // ^^^
int done = 0; //^^^^

//to remember which button we're on
int button;

//buzzer
const int buzzer = 13;

//We'll need these for timing
unsigned long now; //for the millis() function
unsigned long wait; //for the fail system
int seconds;

void setup(){
  pinMode(en1,OUTPUT); //enable 1
  pinMode(en2,OUTPUT); //enable 2
  pinMode(me,OUTPUT); //Mux Enable
  pinMode(a1,OUTPUT); //digit one
  pinMode(a2,OUTPUT); //digit two
  pinMode(a3,OUTPUT); //digit three
  pinMode(a4,OUTPUT); //digit four
  pinMode(sda,OUTPUT); //data out
  pinMode(sck,OUTPUT); //clock
  pinMode(sig,INPUT); //button read
  pinMode(buzzer,OUTPUT); //buzzer
  digitalWrite(buzzer,0); //make sure buzzer is off initially
  digitalWrite(en1,1); //stop any random write cycle to the registers
  digitalWrite(en2,1);
  delay(10); //just here to compensate for propegation; can be removed
  digitalWrite(en1,0); //write all 0's to both registers
  digitalWrite(en2,0);
  shiftOut(sda,sck,LSBFIRST,0);
  digitalWrite(en1,1);
  digitalWrite(en2,1);
  digitalWrite(me,1); //disable the mux
  
  //Serial.begin(9600);
}

void loop(){
  now=millis();
  
  while(millis() - now < 1000){ //while one second has not passed, strobe the time to the display
    digitalWrite(en1,0);
    shiftOut(sda,sck,LSBFIRST,numbers[a]);
    digitalWrite(a1,1);digitalWrite(a2,1);digitalWrite(a3,1);digitalWrite(a4,1);
    digitalWrite(en1,1);
    digitalWrite(a1,0);digitalWrite(a2,1);digitalWrite(a3,1);digitalWrite(a4,1);
    //delay(1);
    digitalWrite(en1,0);
    shiftOut(sda,sck,LSBFIRST,numbers[b]);
    digitalWrite(a1,1);digitalWrite(a2,1);digitalWrite(a3,1);digitalWrite(a4,1);
    digitalWrite(en1,1);
    digitalWrite(a1,1);digitalWrite(a2,0);digitalWrite(a3,1);digitalWrite(a4,1);
    //delay(1);
    digitalWrite(en1,0);
    shiftOut(sda,sck,LSBFIRST,numbers[c]);
    digitalWrite(a1,1);digitalWrite(a2,1);digitalWrite(a3,1);digitalWrite(a4,1);
    digitalWrite(en1,1);
    digitalWrite(a1,1);digitalWrite(a2,1);digitalWrite(a3,0);digitalWrite(a4,1);
    //delay(1);
    digitalWrite(en1,0);
    shiftOut(sda,sck,LSBFIRST,numbers[d]);
    digitalWrite(a1,1);digitalWrite(a2,1);digitalWrite(a3,1);digitalWrite(a4,1);
    digitalWrite(en1,1);
    digitalWrite(a1,1);digitalWrite(a2,1);digitalWrite(a3,1);digitalWrite(a4,0);
    delay(1);
    //end of the strobing
    digitalWrite(a4,1);
    digitalWrite(me,0); //enable the mux
    button_read(); //test for a button press
    digitalWrite(me,1); //disable the mux
  }
  
  // subtract a second
 seconds = (a*1000)+(b*100)+(c*10)+d; //combine all digits to one number
if(seconds>0){ //is there still time left
  seconds--; //if so, subtract one
  a=seconds/1000; //the rest of the code puts the number back to four digits
  b=seconds/100 - (a*10);
  c=seconds/10 - (a*100) - (b*10);
  d=seconds - (a*1000) - (b*100) - (c*10);
}
}

void button_read(){
  digitalWrite(en2,0);
  shiftOut(sda,sck,LSBFIRST,4); //test the # button
  digitalWrite(en2,1);
  //delay(1);
  if(digitalRead(sig)==1){ //if # was pressed, reset timer to 0300 seconds
  //Serial.println("#");
  for(int q=0;q<4;q++){
  digitalWrite(buzzer,1);
  delay(100);
  digitalWrite(buzzer,0);
  delay(100);
  }
    a=0;
    b=3;
    c=0;
    d=0;
}
  digitalWrite(en2,0);
  shiftOut(sda,sck,LSBFIRST,0); //look for * button
  digitalWrite(en2,1);
  delay(1);
  if(digitalRead(sig)==1){ //was the * pressed
  //Serial.println("*");
  digitalWrite(buzzer,1);
  delay(250);
  digitalWrite(buzzer,0);
  while(digitalRead(sig)==1){
    //wait
  }
  e=buttonn();
  if(e != 30){ //30 is the "cancel" code
    f=buttonn();
    if(f!=30){
      g=buttonn();
      if(g!=30){
        h=buttonn();
        if(h!=30){
          a=e; //a, b, c, d are the individual digits in the number
          b=f; //we are taking our temorary number and making it permanent
          c=g;
          d=h+1;
        }
      }
    }
  }
}
}
 
int buttonn(){
  wait = millis();
  while(millis() - wait < 20000){ //keep scanning buttons for no longer than 20 seconds
    for(button=0;button<12;button++){ //all buttons are read on a Mux controlled by a shift regiter
      digitalWrite(en2,0); //enable pin for shift register
      shiftOut(sda,sck,LSBFIRST,buttons[button]); //the buttons[] array allows to scan the buttons in order (0-9, *, #)
      digitalWrite(en2,1);
      delay(1);
      if(digitalRead(sig)==1){ //if there's a hit
      //Serial.println(button,DEC); //this is returning the index number, not the value corresponding to it
        digitalWrite(buzzer,1); //sound the buzzer
        delay(25);
        digitalWrite(buzzer,0);
        while(digitalRead(sig)==1){ //we don't want to fill all four variables with the same number by holding one button down
          //wait
        }
        if(button==10||button==11){ //these are my "cancel" buttons
        for(int q=0;q<5;q++){
          digitalWrite(buzzer,1);
          delay(10);
          digitalWrite(buzzer,0);
          delay(10);
        }
          return 30; //exit code
        }
        else{
          return button; //pressed number
        }
      }
    }
  }
  return 30; //if 20 seconds passed
}
5  Using Arduino / Project Guidance / Re: Help Making Timer Project; Function Fails on: June 16, 2014, 09:35:04 pm
Uhh.... which magic numbers? I did the best I could to spread comments around it explaining things. But I'll see what printing the buttons do.

*EDIT*
Okay, I started thining it may have been the Mux. So I headed over here (forgot to mention) to see what the heck I may have been doing wrong. The "EN" pin is active LOW, whereas the silkscreen indicates it's active HIGH. So I modified the code to reflect this. And low and behold, it's finally registering more buttons. Except that it's registering the wrong buttons.

In fact, the only button it's registering is *. And it is coming up different every time. It's registering as, well, any other button at any given time.

*EDIT 2*
Thank Karl Braun for the O-Scope. I traced the problem of random info to the shift register (to the mux). Still have no idea why it's working weirdly. But about to modify it.

*EDIT 3*
Stupid me didn't relize that Q0 on the shift register was bit 8. I changed the wiring to fix my error. And now all but the second column of buttons (2,5,8,0) don't work, along with #. * still wants to register as # randomly. The working numbers seem to register correctly , though printing them returns the array's index. Also, when in program mode, the * and # buttons act like numbers. Though pressing them causes weird things to happen, like missing digits (null?) and corrupted digits like little 'n' with a bar above it.
6  Using Arduino / Project Guidance / Help Making Timer Project; Function Fails on: June 16, 2014, 05:40:06 pm
I am trying to make a programmable timer. I have more about it here: http://forum.arduino.cc/index.php?topic=247174.msg1766691#msg1766691

Now I have it all put together and stuff. But my program isn't working correctly. What it should do that it isn't is one: it should see if I press the # button on a keypad. I do, it doesn't. Upon doing this, it is supposed to reset the timer to the default 0300 seconds.
Next, it should tell if I press the * button. It does. But it should also let me insert a new time in when I do. It don't. I have added some extra code to see where the problems lie. It is acting like it's scanning the buttons as it should, but it's not registering them.

I've traced a few bugs back to the multiplexer I have; it was disabling it when it wasn't supposed to. I have it set to enable the multiplexer just before the initial scan, then disable it afterward. I tried to see if it was disabling it. It wasn't. So I tried to move the part of the code that tests for # to before the part that tests *. Pressing # still does nothing. But pressing # will execute part of the code that resets the time (it beeps three times) before executing the correct code (beeps again) and still failing. In the following code, you can see where I commented out the code that tests for #.

Code:
// define numbers, buttons
const int numbers[] = {0b11111100,0b01100000,0b11011010,0b11110010,0b01100110,0b10110110,0b00111110,0b11100000,0b11111110,0b11100110}; // 0-9
const int buttons[] = {7,0,4,8,1,5,9,2,6,10,3,11}; //0, 1, 2, 3, 4, 5, 6, 7, 8, 9, *, #

// set the enable pins on both shift registers
const int en1 = 10; // set low when sending data, display
const int en2 = A2; // same as above, Mux

// these are the digits on the display
const int a1 = 2;
const int a2 = 3;
const int a3 = 4;
const int a4 = 5;

//data, clock, buttons, mux enable
const int sda = 7;
const int sck = 8;
const int sig = A0;
const int me = A1;

//set the digits time
int a = 0; //a-d are defualt time
int b = 3;
int c = 0;
int d = 0;
int e = 0; //temporary storage
int f = 0; // ^
int g = 0; // ^^
int h = 0; // ^^^
int done = 0; //^^^^

//to remember which button we're on
int button;

//buzzer
const int buzzer = 13;

//We'll need these for timing
unsigned long now; //for the millis() function
unsigned long wait; //for the fail system
int seconds;

void setup(){
  pinMode(en1,OUTPUT); //enable 1
  pinMode(en2,OUTPUT); //enable 2
  pinMode(me,OUTPUT); //Mux Enable
  pinMode(a1,OUTPUT); //digit one
  pinMode(a2,OUTPUT); //digit two
  pinMode(a3,OUTPUT); //digit three
  pinMode(a4,OUTPUT); //digit four
  pinMode(sda,OUTPUT); //data out
  pinMode(sck,OUTPUT); //clock
  pinMode(sig,INPUT); //button read
  pinMode(buzzer,OUTPUT); //buzzer
  digitalWrite(buzzer,0); //make sure buzzer is off initially
  digitalWrite(en1,1); //stop any random write cycle to the registers
  digitalWrite(en2,1);
  delay(10); //just here to compensate for propegation; can be removed
  digitalWrite(en1,0); //write all 0's to both registers
  digitalWrite(en2,0);
  shiftOut(sda,sck,LSBFIRST,0);
  digitalWrite(en1,1);
  digitalWrite(en2,1);
  digitalWrite(me,0); //disable the mux
}

void loop(){
  now=millis();
 
  while(millis() - now < 1000){ //while one second has not passed, strobe the time to the display
    digitalWrite(en1,0);
    shiftOut(sda,sck,LSBFIRST,numbers[a]);
    digitalWrite(a1,1);digitalWrite(a2,1);digitalWrite(a3,1);digitalWrite(a4,1);
    digitalWrite(en1,1);
    digitalWrite(a1,0);digitalWrite(a2,1);digitalWrite(a3,1);digitalWrite(a4,1);
    //delay(1);
    digitalWrite(en1,0);
    shiftOut(sda,sck,LSBFIRST,numbers[b]);
    digitalWrite(a1,1);digitalWrite(a2,1);digitalWrite(a3,1);digitalWrite(a4,1);
    digitalWrite(en1,1);
    digitalWrite(a1,1);digitalWrite(a2,0);digitalWrite(a3,1);digitalWrite(a4,1);
    //delay(1);
    digitalWrite(en1,0);
    shiftOut(sda,sck,LSBFIRST,numbers[c]);
    digitalWrite(a1,1);digitalWrite(a2,1);digitalWrite(a3,1);digitalWrite(a4,1);
    digitalWrite(en1,1);
    digitalWrite(a1,1);digitalWrite(a2,1);digitalWrite(a3,0);digitalWrite(a4,1);
    //delay(1);
    digitalWrite(en1,0);
    shiftOut(sda,sck,LSBFIRST,numbers[d]);
    digitalWrite(a1,1);digitalWrite(a2,1);digitalWrite(a3,1);digitalWrite(a4,1);
    digitalWrite(en1,1);
    digitalWrite(a1,1);digitalWrite(a2,1);digitalWrite(a3,1);digitalWrite(a4,0);
    //delay(1);
    //end of the strobing
    digitalWrite(me,1); //enable the mux
    button_read(); //test for a button press
    digitalWrite(me,0); //disable the mux
  }
 
  // subtract a second
 seconds = (a*1000)+(b*100)+(c*10)+d; //combine all digits to one number
if(seconds>0){ //is there still time left
  seconds--; //if so, subtract one
  a=seconds/1000; //the rest of the code puts the number back to four digits
  b=seconds/100 - (a*10);
  c=seconds/10 - (a*100) - (b*10);
  d=seconds - (a*1000) - (b*100) - (c*10);
}
}

void button_read(){
/*    digitalWrite(en2,0);
  shiftOut(sda,sck,LSBFIRST,11); //test the # button
  digitalWrite(en2,1);
  if(digitalRead(sig)==1){ //if # was pressed, reset timer to 0300 seconds
  for(int q=0;q<4;q++){
  digitalWrite(buzzer,1);
  delay(100);
  digitalWrite(buzzer,0);
  delay(100);
  }
    a=0;
    b=3;
    c=0;
    d=0;
}*/
  digitalWrite(en2,0);
  shiftOut(sda,sck,LSBFIRST,3); //look for * button
  digitalWrite(en2,1);
  if(digitalRead(sig)==1){ //was the * pressed
  digitalWrite(buzzer,1);
  delay(250);
  digitalWrite(buzzer,0);
  while(digitalRead(sig)==1){
    //wait
  }
  e=buttonn();
  if(e != 30){ //30 is the "cancel" code
    f=buttonn();
    if(f!=30){
      g=buttonn();
      if(g!=30){
        h=buttonn();
        if(h!=30){
          a=e; //a, b, c, d are the individual digits in the number
          b=f; //we are taking our temorary number and making it permanent
          c=g;
          d=h;
        }
      }
    }
  }
}
}
 
int buttonn(){
  wait = millis();
  while(millis() - wait < 10000){ //keep scanning buttons for no longer than 10 seconds
    for(button=0;button<12;button++){ //all buttons are read on a Mux controlled by a shift regiter
      digitalWrite(en2,0); //enable pin for shift register
      shiftOut(sda,sck,LSBFIRST,buttons[button]); //the buttons[] array allows to scan the buttons in order (0-9, *, #)
      digitalWrite(en2,1);
      if(digitalRead(sig)==1){ //if there's a hit
        digitalWrite(buzzer,1); //sound the buzzer
        delay(25);
        digitalWrite(buzzer,0);
        while(digitalRead(sig)==1){ //we don't want to fill all four variables with the same number by holding one button down
          //wait
        }
        if(button==10||button==11){ //these are my "cancel" buttons
          return 30; //exit code
        }
        else{
          return button; //pressed number
        }
      }
    }
  }
  return 30; //if 10 seconds passed
}

I can tell it's executing the function button_read() correctly and that it's nesting buttonn() correctly because after 10 seconds, it fails like it should and contenues the countdown. This is killing me.
7  Using Arduino / LEDs and Multiplexing / Re: Strobing to Light Four 7-Segment Displays; Not Working Properly on: June 15, 2014, 01:03:48 pm
I was going to say that I have no idea why that worked. And then I remembered, I'm dumb. I was thinking that with each loop, now was resetting to 0. Now I get to integrate a multiplexer, a keypad, and resistor array to make it adjustable.
8  Using Arduino / LEDs and Multiplexing / [Solved] Strobing to Light Four 7-Segment Displays; Not Working Properly on: June 15, 2014, 12:35:02 pm
So I got this little 7-segment bubble display with four digits from Sparkfun. I also got one of their shift registers (74HC595N). I've had success in the past getting these to work together. But now I have an issue.

I'm trying to get a timer together using these. I coded a number, 300, into the scetch as four different values: a=0,b=3,c=0,d=0. After squashing some bugs, it works.... except that when it reaches 0267, all digits except the last go blank (it remains on 8) and it stops counting. Thinkng it was the Arduino (Uno) going bad after all the abuse I sent it through, I uploaded the code to a new(er) Uno. It still acts the same. It seems as though it stops executing the code after so much time.

The only two conditions in the loop are
Code:
now=millis();
  
  while(millis() - now < 1000){
and
Code:
seconds = (a*1000)+(b*100)+(c*10)+d;
if(seconds>0){

Neither of those two were designed to stop execution of the code.

Code:
// define numbers
const int numbers[] = {0b11111100,0b01100000,0b11011010,0b11110010,0b01100110,0b10110110,0b00111110,0b11100000,0b11111110,0b11100110}; // 0-9

// set the enable pin
const int en = 10; // set low when sending data

// these are the digits on the display
const int a1 = 2;
const int a2 = 3;
const int a3 = 4;
const int a4 = 5;

//data,clock
const int sda = 7;
const int sck = 8;

//set the digits
int a;
int b;
int c;
int d;

//We'll need these for timing
int now;
int seconds;

void setup(){
  pinMode(en,OUTPUT);
  pinMode(a1,OUTPUT);
  pinMode(a2,OUTPUT);
  pinMode(a3,OUTPUT);
  pinMode(a4,OUTPUT);
  pinMode(sda,OUTPUT);
  pinMode(sck,OUTPUT);
  digitalWrite(en,1);
  delay(10);
  digitalWrite(en,0);
  shiftOut(sda,sck,LSBFIRST,0);
  digitalWrite(en,1);
  
  a=0;
  b=3;
  c=0;
  d=0;
}

void loop(){
  now=millis();
  
  while(millis() - now < 1000){
    digitalWrite(en,0);
    shiftOut(sda,sck,LSBFIRST,numbers[a]);
    digitalWrite(a1,1);digitalWrite(a2,1);digitalWrite(a3,1);digitalWrite(a4,1);
    digitalWrite(en,1);
    digitalWrite(a1,0);digitalWrite(a2,1);digitalWrite(a3,1);digitalWrite(a4,1);
    delay(1);
    digitalWrite(en,0);
    shiftOut(sda,sck,LSBFIRST,numbers[b]);
    digitalWrite(a1,1);digitalWrite(a2,1);digitalWrite(a3,1);digitalWrite(a4,1);
    digitalWrite(en,1);
    digitalWrite(a1,1);digitalWrite(a2,0);digitalWrite(a3,1);digitalWrite(a4,1);
    delay(1);
    digitalWrite(en,0);
    shiftOut(sda,sck,LSBFIRST,numbers[c]);
    digitalWrite(a1,1);digitalWrite(a2,1);digitalWrite(a3,1);digitalWrite(a4,1);
    digitalWrite(en,1);
    digitalWrite(a1,1);digitalWrite(a2,1);digitalWrite(a3,0);digitalWrite(a4,1);
    delay(1);
    digitalWrite(en,0);
    shiftOut(sda,sck,LSBFIRST,numbers[d]);
    digitalWrite(a1,1);digitalWrite(a2,1);digitalWrite(a3,1);digitalWrite(a4,1);
    digitalWrite(en,1);
    digitalWrite(a1,1);digitalWrite(a2,1);digitalWrite(a3,1);digitalWrite(a4,0);
    delay(1);
  }
  
  // subtract a second
 seconds = (a*1000)+(b*100)+(c*10)+d;
if(seconds>0){
  seconds--;
  a=seconds/1000;
  b=seconds/100 - (a*10);
  c=seconds/10 - (a*100) - (b*10);
  d=seconds - (a*1000) - (b*100) - (c*10);
}
}
9  Using Arduino / Project Guidance / Re: analog inputs vs digital inputs on: May 22, 2014, 09:18:20 pm
The way I understand the question, you wish to use the LDR (photoresistor) to detect a car of sorts. You will need to form a voltage divider as stated by retrolefty. If you can get the voltage at the junction to vary between, and including, 1/3 VCC (1.67V) and 2/3 VCC (3.33V), you can use a digital input. If the voltage at the junction <= (less than or equal to) 1.67V, it will register as logic 0 - Low, Off, No, False... If the voltage is >= (greater than or equal to) 3.33V, it will register as logic 1 - High, On, Yes, True... These assume your controller is at 5V. If your controller is 3.3V, adjust the numbers above.

If you find it too difficult to get these voltages (very likely), just use an analogue input. Run a few tests to see what the average values are when a "car" is over the LDR and when it's not. Then just compare these values in the program itself.
10  Using Arduino / Project Guidance / Re: I need to shiftout a nibble on: May 22, 2014, 10:48:49 am
Well that's going to make things complex. The chip has data shifted in MSB first, starting with the data byte. But how hard can it be, really? Just split the data byte into two nibbles. The first of which then gets four 0's tacked to the front (if I store it in a byte-long variable, this should be automatic) and the second gets tacked to the front of the control nibble.
11  Using Arduino / Project Guidance / I need to shiftout a nibble on: May 22, 2014, 08:53:55 am
I've been thinking of an LED project (only) I'd think was cool. I want to take a tri-colour LED and control each color's brightness with a DAC. I have fount a three channel DAC on Digikey with the part number BH2220FVM. Looking at the datasheet, it appears to be controlled via 12-bit commands: 8-bits for data and 4-bits for control (wording?).

Is is possible, with the Uno, to shift out a single nibble, short of bit-banging?
12  Using Arduino / Project Guidance / Re: floats to ints for temperature reading on: March 23, 2014, 09:21:45 pm
Do you really want it to self-scale without making it obvious that it has done? For example, with the approach you described, 10.2 and 102 would appear the same. If that's really what you want then I suggest you simply test whether the value is less than 100 degrees and multiply the float value by 10 or 100 to give you the integer value you want to display. You can simply cast it to an integer to get the significant digits to be displayed - add 0.5 first if you want the result to be rounded. Then you can either print the integer to get an ascii string, or use div/mod to extract the decimal digits from it.

Sometimes, I miss the simple things. This approach is 100x simpler than I anticipated.

And to answer jremington:
The code, as it currently sits, gets a reading from the sensor. But what gets printed to the display is:
F7x.4.
where 'x' is different every single time it gets written to the display, and the second decimal point may or may not show up on the write. Meanwhile, it was supposed to say something closer to:
F63.5

And it works now. It seems my analog thermometer is very wrong (it shows it's 43 F), but I think the digital one is correct. I wrote a PHP script on my local server to verify my maths. And here is my working code:
Code:
//Arduino 1.0+ Only
//Arduino 1.0+ Only

//////////////////////////////////////////////////////////////////
//©2011 bildr
//Released under the MIT License - Please reuse change and share
//Simple code for the TMP102, simply prints temperature via serial
//////////////////////////////////////////////////////////////////
// SDA is on analog pin 4, and SCL is on analog pin 5

#include <Wire.h>
int tmp102Address = 0x48;
const int numbers[] = {0b11111100,0b01100000,0b11011010,0b11110010,0b01100110,0b10110110,0b00111110,0b11100000,0b11111110,0b11100110}; // 0-9
const int ser = 8;
const int load = 10;
const int sck = 9;
const int a1 = 2;
const int a2 = 3;
const int a3 = 4;
const int a4 = 5;

void setup(){
  Wire.begin();
  pinMode(ser,OUTPUT);
  pinMode(sck,OUTPUT);
  pinMode(load,OUTPUT);
  pinMode(a1,OUTPUT);
  pinMode(a2,OUTPUT);
  pinMode(a3,OUTPUT);
  pinMode(a4,OUTPUT);
  digitalWrite(load,0);
  shiftOut(ser,sck,LSBFIRST,0);
  digitalWrite(a1,1);
  digitalWrite(a2,1);
  digitalWrite(a3,1);
  digitalWrite(a4,1);
  digitalWrite(load,1);
}
int a;
int b;
int c;
int s;
void loop(){
  int f;
  boolean dp = 0;

  float celsius = getTemperature();
  
  float fahrenheit = (1.8 * celsius) + 32;  
  
//  delay(200); //just here to slow down the output. You can remove this
 if(fahrenheit >= 100){
 f=(int)(fahrenheit+0.5);
 }
 
else{
  f=(int)((fahrenheit+0.05)*10.0);
  dp=1;
}

a=f/100;
b=(f/10)-(a*10)+dp;
c=f-(a*100)-((b-dp)*10);
 
 
  
  digitalWrite(load,0);
  digitalWrite(a1,1);digitalWrite(a2,1);digitalWrite(a3,1);digitalWrite(a4,1);
  shiftOut(ser,sck,LSBFIRST,0b10001110);
  digitalWrite(load,1);
  digitalWrite(a1,0);digitalWrite(a2,1);digitalWrite(a3,1);digitalWrite(a4,1);
  delay(5);
  
  digitalWrite(load,0);
  digitalWrite(a1,1);digitalWrite(a2,1);digitalWrite(a3,1);digitalWrite(a4,1);
  shiftOut(ser,sck,LSBFIRST,numbers[a]);
  digitalWrite(load,1);
  digitalWrite(a1,1);digitalWrite(a2,0);digitalWrite(a3,1);digitalWrite(a4,1);
  delay(5);
  
  if(dp==0){
    s = numbers[b];
  }
  else{
    s = numbers[b]+1;
  }
  
  digitalWrite(load,0);
  digitalWrite(a1,1);digitalWrite(a2,1);digitalWrite(a3,1);digitalWrite(a4,1);
  shiftOut(ser,sck,LSBFIRST,s);  
  digitalWrite(load,1);
  digitalWrite(a1,1);digitalWrite(a2,1);digitalWrite(a3,0);digitalWrite(a4,1);
  delay(5);
  
  digitalWrite(load,0);
  digitalWrite(a1,1);digitalWrite(a2,1);digitalWrite(a3,1);digitalWrite(a4,1);
  shiftOut(ser,sck,LSBFIRST,numbers[c]);
  digitalWrite(load,1);
  digitalWrite(a1,1);digitalWrite(a2,1);digitalWrite(a3,1);digitalWrite(a4,0);
  delay(5);
}

float getTemperature(){
  Wire.requestFrom(tmp102Address,2);

  byte MSB = Wire.read();
  byte LSB = Wire.read();

  //it's a 12bit int, using two's compliment for negative
  int TemperatureSum = ((MSB << 8) | LSB) >> 4;

  float celsius = TemperatureSum*0.0625;
  return celsius;
}
13  Using Arduino / Project Guidance / floats to ints for temperature reading on: March 22, 2014, 05:57:25 pm
I have this digital thermometer from Sparkfun: tmp102. I used the example code linked on the product page to get values from the thing. But I have an issue from there. I want to take the value given and print (strobe) them to a 4x7 segment display. My issue is getting from a float of some unknown length to four integers.

Let's say the value read is 70.68. I need to get it so that:
a=7
b=0
c=7 (because it rounded up)

Or if the value is 101.56:
a=1
b=0
c=2 (because it rounds up)

The purpose of the fourth digit is to determine if the third gets rounded. But the three digits get printed to the display.

I've got:
Code:
//Arduino 1.0+ Only
//Arduino 1.0+ Only

//////////////////////////////////////////////////////////////////
//©2011 bildr
//Released under the MIT License - Please reuse change and share
//Simple code for the TMP102, simply prints temperature via serial
//////////////////////////////////////////////////////////////////
// SDA is on analog pin 4, and SCL is on analog pin 5

#include <Wire.h>
int tmp102Address = 0x48;
const int numbers[] = {0b11111100,0b01100000,0b11011010,0b11110010,0b01100110,0b10110110,0b01111010,0b11100000,0b11111110,0b11100110}; // 0-9
const int ser = 8;
const int load = 10;
const int sck = 9;
const int a1 = 2;
const int a2 = 3;
const int a3 = 4;
const int a4 = 5;

void setup(){
  Wire.begin();
  pinMode(ser,OUTPUT);
  pinMode(sck,OUTPUT);
  pinMode(load,OUTPUT);
  pinMode(a1,OUTPUT);
  pinMode(a2,OUTPUT);
  pinMode(a3,OUTPUT);
  pinMode(a4,OUTPUT);
  digitalWrite(load,0);
  shiftOut(ser,sck,LSBFIRST,0);
  digitalWrite(a1,1);
  digitalWrite(a2,1);
  digitalWrite(a3,1);
  digitalWrite(a4,1);
  digitalWrite(load,1);
}
int a;
int b;
int c;
int s;
void loop(){
  int dp;

  float celsius = getTemperature();
 
  float fahrenheit = (1.8 * celsius) + 32; 
 
//  delay(200); //just here to slow down the output. You can remove this
 
 int f=(int)(fahrenheit*10.0);
 int g=f;
  if(g/1000 != 0){
    a=g/1000;
    b=(g/100)-(a*10);
    c=(g/10)-(((a*10)+b)*10);
    if((g-((a*100)+(b*10)+c)*10) > 4){
      c++;
    dp = 0;
    }
  }
  else{
    a=g/100;
    b=(g/100)-(a*10);
    c=g-(((a*10)+b)*10);
  }
 
  digitalWrite(load,0);
  digitalWrite(a1,1);digitalWrite(a2,1);digitalWrite(a3,1);digitalWrite(a4,1);
  shiftOut(ser,sck,LSBFIRST,0b10001110);
  digitalWrite(load,1);
  digitalWrite(a1,0);digitalWrite(a2,1);digitalWrite(a3,1);digitalWrite(a4,1);
  delay(5);
 
  digitalWrite(load,0);
  digitalWrite(a1,1);digitalWrite(a2,1);digitalWrite(a3,1);digitalWrite(a4,1);
  shiftOut(ser,sck,LSBFIRST,numbers[a]);
  digitalWrite(load,1);
  digitalWrite(a1,1);digitalWrite(a2,0);digitalWrite(a3,1);digitalWrite(a4,1);
  delay(5);
 
  if(dp==0){
    s = numbers[b];
  }
  else{
    s = numbers[b]+1;
  }
 
  digitalWrite(load,0);
  digitalWrite(a1,1);digitalWrite(a2,1);digitalWrite(a3,1);digitalWrite(a4,1);
  shiftOut(ser,sck,LSBFIRST,s); 
  digitalWrite(load,1);
  digitalWrite(a1,1);digitalWrite(a2,1);digitalWrite(a3,0);digitalWrite(a4,1);
  delay(5);
 
  digitalWrite(load,0);
  digitalWrite(a1,1);digitalWrite(a2,1);digitalWrite(a3,1);digitalWrite(a4,1);
  shiftOut(ser,sck,LSBFIRST,numbers[c]);
  digitalWrite(load,1);
  digitalWrite(a1,1);digitalWrite(a2,1);digitalWrite(a3,1);digitalWrite(a4,0);
//  delay(10);
}

float getTemperature(){
  Wire.requestFrom(tmp102Address,2);

  byte MSB = Wire.read();
  byte LSB = Wire.read();

  //it's a 12bit int, using two's compliment for negative
  int TemperatureSum = ((MSB << 8) | LSB) >> 4;

  float celsius = TemperatureSum*0.0625;
  return celsius;
}
14  Using Arduino / Programming Questions / Re: Dynamic Array of Integers on: June 26, 2013, 11:22:30 am
Your getting a new copy of buttons (set to 0) each time loop() is called. Make buttons static or global. Look up "scope of var in c/c++".

Mark

I know. It's supposed to reset at the beginning of the loop (and I just realise I can remove that line due to how I'm setting them).

How are your pull down resistors connected? Post updated code when you make changes.
I put them between the switches (which I've been calling buttons) and the Arduino (I'll create schematics later). And I'll post the new code after I re-update it.
Code:
void setup(){
  //define the inputs
  for(int z=2;z<6;z++){
    pinMode(z,INPUT);
  }
  //Define the outputs
  for(int z=8;z<12;z++){
    pinMode(z,OUTPUT);
  }
    Serial.begin(9600);
}
int buttons[16];
void loop(){

  //Read the first four inputs
  digitalWrite(8,HIGH); digitalWrite(9,LOW); digitalWrite(10,LOW); digitalWrite(11,LOW);

    buttons[0] = digitalRead(2);

    buttons[4] = digitalRead(3);

    buttons[8] = digitalRead(4);

    buttons[12] = digitalRead(5);

  //Read the next four
  digitalWrite(8,LOW); digitalWrite(9,HIGH); digitalWrite(10,LOW); digitalWrite(11,LOW);

    buttons[1] = digitalRead(2);

    buttons[5] = digitalRead(3);

    buttons[9] = digitalRead(4);

    buttons[13] = digitalRead(5);
 
   //Read the next four
   digitalWrite(8,LOW); digitalWrite(9,LOW); digitalWrite(10,HIGH); digitalWrite(11,LOW);

    buttons[2] = digitalRead(2);

    buttons[6] = digitalRead(3);

    buttons[10] = digitalRead(4);

    buttons[14] = digitalRead(5);
 
   //Read the last four
   digitalWrite(8,LOW); digitalWrite(9,LOW); digitalWrite(10,LOW); digitalWrite(11,HIGH);

    buttons[3] = digitalRead(2);

    buttons[7] = digitalRead(3);

    buttons[11] = digitalRead(4);

    buttons[15] = digitalRead(5);
  //Print the results
  for(int y=0;y<16;y++){
    Serial.print(buttons[y],DEC);
  }
  Serial.println();
  delay(500);
}

*EDIT*
I just figured out my problem. It's how I wired it. When there is voltage on four switches and I flip one, I put voltage on an input. That's how it should work. But when I flip a switch with no voltage on it, current travels back into the arduino; voltage drops to zero on the inputs.
15  Using Arduino / Programming Questions / Re: Dynamic Array of Integers on: June 26, 2013, 12:21:51 am
*pfft*
What I get for coding at 1AM. But now, it's not storing all the inputs. I can flip some switches and it will show. But if I flip other, it will show less. For example, right now I have two flipped. It is showing "0000000001000010", which is correct. But if I flip another, it will show "0000000001001000" when it should show "0000000001001010". And when it should show "1010000001001010", it shows "0000000001000000". I don't really have schematics right now, but I will create some soon.
Pages: [1] 2