Loading...
  Show Posts
Pages: [1] 2
1  Using Arduino / Programming Questions / Re: Random LED's on: February 10, 2013, 09:33:32 am
The code looks OK.  Have you checked to see that all of the lights work?  Watch each light for 10 seconds or until if comes on.  It might be that one or more of your LEDs is not working (bad connection) and so the random patterns will sometimes hit a non-working LED.

I ran a test and there was indeed a lose wire, thanks!
2  Using Arduino / Programming Questions / Re: Random LED's on: February 10, 2013, 08:49:45 am
Oh and my set up is like this:
3  Using Arduino / Programming Questions / Re: Random LED's on: February 10, 2013, 08:45:33 am
I think you have to show your code.

yeah it was basically the code you gave me, modified for shift register usage

it works when my loop is like this:

Code:
void loop(){

    // Turn on "Number" randonm LEDs
  for (int i=0; i<Number; i++) {
    do {
      thisPin = random(8);
      alreadySet = pattern & (1<<thisPin);
      pattern |= (1<<thisPin);
      setRegisterPin(thisPin, HIGH);
      writeRegisters();
    }
    while (alreadySet);  // If the LED was already on, try a different one
  }
pattern = 0;


  delay(timer);
  
  clearRegisters();

}

but as soon as I change the 8 in:
Code:
thisPin = random(8)
to a higher number, (starting to use the second shift register, which is a 74HC595 by the way) I get the problem (most of the time the desired amount of LEDs are burning but about 10% of the time a couple of them don't light up)

Here is the complete code if needed

Code:
int timer = (1000);
int SER_Pin = 8;   //pin 14 on the 75HC595
int RCLK_Pin = 9;  //pin 12 on the 75HC595
int SRCLK_Pin = 10; //pin 11 on the 75HC595
int Number = 4;
int thisPin;
int alreadySet;

#define shiftCount 2 //How many shift registers
#define pinCount shiftCount * 8 //How many pins

boolean registers[pinCount];
unsigned long pattern;

void setup(){
  pinMode(SER_Pin, OUTPUT);
  pinMode(RCLK_Pin, OUTPUT);
  pinMode(SRCLK_Pin, OUTPUT);
  Serial.begin(9600);

  //reset all register pins
  clearRegisters();
  writeRegisters();
}              

//set all register pins to LOW
void clearRegisters(){
  for(int i = 0; i <  pinCount; i++){
     registers[i] = LOW;
    
  }
}

//Set and display registers
//Only call AFTER all values are set how you would like (slow otherwise)
void writeRegisters(){

  digitalWrite(RCLK_Pin, LOW);

  for(int i = pinCount - 1; i >=  0; i--){
    digitalWrite(SRCLK_Pin, LOW);

    int val = registers[i];

    digitalWrite(SER_Pin, val);
    digitalWrite(SRCLK_Pin, HIGH);

  }
  digitalWrite(RCLK_Pin, HIGH);

}

//set an individual pin HIGH or LOW
void setRegisterPin(int index, int value){
  registers[index] = value;
}

void loop(){

    // Turn on "Number" randonm LEDs
  for (int i=0; i<Number; i++) {
    do {
      thisPin = random(8);
      alreadySet = pattern & (1<<thisPin);
      pattern |= (1<<thisPin);
      setRegisterPin(thisPin, HIGH);
      writeRegisters();
    }
    while (alreadySet);  // If the LED was already on, try a different one
  }
pattern = 0;


  delay(timer);
  
  clearRegisters();

}
4  Using Arduino / Programming Questions / Re: Random LED's on: February 10, 2013, 08:19:07 am
Thanks John! Although this way I won't be able to use shift registers won't I? because in my final project I will be using 27 LED's. But that's not to big of a problem, in that case I will use I2C I/O expanders.

You can use an unsigned long (32-bit integer) to keep track of which LEDs you want on and off.
Code:
int timer = 1000; //Sets timer
const int pinCount = 6; //Sets the number of pins
int Number = 3; //Sets the amount of LEDs that will go on
unsigned long pattern;
int thisPin;
int alreadySet;

void loop(){
  // Turn off all the LEDs
  pattern = 0;

  // Turn on "Number" randonm LEDs
  for (int i=0; i<Number; i++) {
    do {
      thisPin = random(pinCount);
      alreadySet = pattern & (1<<thisPin));
      pattern |= (1<<thisPin);
    }
    while (alreadySet);  // If the LED was already on, try a different one
  }
  //  Send 'pattern' to shift registers
  delay(timer);
}

So i thought this did the trick since I tested it with one shift register (register pin 0 - 7), today I tried daisy chaining an other one to it (register pin 0 - 15). And I ran in to the same problem: most of the time the desired amount of LEDs are burning but about 10% of the time a couple of them don't light up (I guess it has some thing to do with the pattern being full or something?). Is there a fix for this?
5  Using Arduino / Programming Questions / Re: Random LED's on: February 09, 2013, 01:56:58 pm
Quote
No not at all, I just picked up Arduino as a hobby and I'm trying to rebuild a binary clock I saw a while back.
How does a clock use random numbers?

Like this!

6  Using Arduino / Programming Questions / Re: Random LED's on: February 09, 2013, 12:40:00 pm
Thanks John! Although this way I won't be able to use shift registers won't I? because in my final project I will be using 27 LED's. But that's not to big of a problem, in that case I will use I2C I/O expanders.
7  Using Arduino / Programming Questions / Re: Random LED's on: February 09, 2013, 12:04:49 pm
Thanks!

This is oddly prescriptive, is it homework?

No not at all, I just picked up Arduino as a hobby and I'm trying to rebuild a binary clock I saw a while back.
8  Using Arduino / Programming Questions / Re: Random LED's on: February 09, 2013, 11:35:46 am
That will turn on a random combination of the 9 LEDs but will not "let a certain number of LED's go on, but which of the LED's go on has to be random."  By that I assume they meant that the number of lights turned on is known but the pattern is random.  That's why I recommended the shuffle.

That's right, I didn't fully understand your first post though but so far I've got this:

Code:
int timer = 1000; //Sets timer
int ledPins [] = {2, 3, 4, 5, 6, 7,}; //Sets pins
int pinCount = 6; //Sets the number of pins
int Number = 3; //Sets the amount of LEDs that will go on
int thisPin; //Specificies a certain pin
 
void setup() {
  Serial.begin(9600);
  for (int thisPin = 0; thisPin < pinCount; thisPin++) {
    pinMode(ledPins[thisPin], OUTPUT); //Sets all the ledPins as output
  }
}

void loop(){
  for (int i=0; i<=Number; i++){
  if (i < Number) {
  thisPin = random(pinCount);
  digitalWrite(ledPins[thisPin], HIGH); }  //this will loop "Number" times
  else {
  delay(timer);
  for (int thisPin = 0; thisPin < pinCount; thisPin++) {
  digitalWrite(ledPins[thisPin], LOW); } //turns of all the pins
  }
  }
}

this almost works except sometimes there will be only 1 or 2 light burning instead of 3, because the pin has already been picked.
9  Using Arduino / Programming Questions / [SOLVED] Random LED's on: February 09, 2013, 09:43:11 am
Hi,

Say I have 9 LED's connected to my arduino on pin 1 to 9. I want to be able to let a certain number of LED's go on, but which of the LED's go on has to be random. Any thoughts?
10  Community / Website and Forum / Re: Karma? on: February 08, 2013, 08:38:26 am
Well after my 5th post on the forum I was able to give Karma so I guess that had something to do with it.
11  Using Arduino / Programming Questions / Re: Reading data from a PCF8574 on: February 08, 2013, 08:33:56 am
I've got a much better understanding about I2C's now and got it working. Thank you both!
12  Community / Website and Forum / Re: Karma? on: February 08, 2013, 08:29:13 am
It doesn't show up in both Google Chrome and Safari. But maybe that's because of my "Newbie" status.

[Edit] yes as soon as I posted this I was able to give karma
13  Community / Website and Forum / Re: Karma? on: February 08, 2013, 07:32:45 am
So where do I click to give some one Karma? I can't find the + button
14  Using Arduino / Programming Questions / [Solved] Reading data from a PCF8574 on: February 07, 2013, 02:37:23 pm
Hi,

I'm fairly new to Arduino, I did a few basic projects. Now I'm trying to learn more about the PCF8574 I/O bus expander. I was able to connect 8 LED's and make them blink one at the time without to much trouble. But now I'm trying to read a button push and it's giving me a hard time for the past 2 days.

This is the way I've set up my project:


and this is the code I'm using:
Code:
#define  DEVICE_ADDRESS  56
 
#include <Wire.h>

byte x=0;
 
void setup()
{
  Wire.begin();                
  Serial.begin(9600);      
}
 
void loop()
{
  
  x = Wire.read();
  Serial.println(x);  

}

If somebody could take a look and tell me what I'm doing wrong, that would be great!
15  Using Arduino / Programming Questions / Re: IR receiver code's on: January 14, 2013, 07:04:54 am
That did the trick thanks a lot!
Pages: [1] 2