Hi,
welcome all.. new and inexperienced, but willing to learn i got a little project..
I'm trying to bring a little extra light in existence
.. and for this i am trying this little project.
I have 20 small houses with a led on my wall, and want to connect them to my arduino.
I want to simulate occupants getting home or leaving .. by turning lights on or off.
What I am trying to achieve is the following:
At startup i want to turn all lights off (or on)
I want to generate a random number between 1-20 (one of the "houses")
I want to check the state of that "house" and switch the state of it (if it's on, turn it off etc)
After a certain delay i want to rerun the loop.
So after a while i will have a some occupants home .. and constantly people arriving and leaving.
I'm using charlieplexing to connect the leds (want to us a micro arduino board to hide in one of the houses..)
So far i did some test using 6 leds charlieplexed and got some results .. but I'm not there yet!
I started of with the base code from the Arduino Cookbook page 242-243
the current attached code light random leds.. but doesn't keep "on" state of other leds i guess, because only 1 led lights up at a time don't know why.
thanks for any help!
this is my current code:
byte pins[] = {8,9,10};
const int NUMBER_OF_PINS = sizeof(pins)/ sizeof(pins[0]);
const int NUMBER_OF_LEDS = NUMBER_OF_PINS * (NUMBER_OF_PINS-1);
byte pairs[NUMBER_OF_LEDS/2][2] = { {0,1}, {1,2}, {0,2} };
int ledStates = 0; //holds states for up to 15 LEDs //not sure how this is stored?
int ledNr[6] = {1,2,3,4,5,6}; //nr of led
int ledOnOffCheck[6] = {0,0,0,0,0,0}; //declating array of led states 0 or 1
int refreshedLed; // the LED that gets refreshed
int counter = 0;
void setup()
{
Serial.begin(9600); // added this to see what's happening
}
void loop()
{
Serial.print(counter); // added this to see what's happening
delay(100); // added this to see what's happening
Serial.print(ledOnOffCheck[counter]); // added this to see what's happening
delay(100); // added this to see what's happening
if (counter ++> NUMBER_OF_LEDS-1)
{
counter = 0;
}
if (ledOnOffCheck[counter] == 1)
{
lightLed(counter);
}
int randLed;
randLed = random(NUMBER_OF_LEDS); //choses a random led
if( ledOnOffCheck[randLed]==0) //checks if it has the off state and switches using options following
{
ledOnOffCheck[randLed]=1;
}
else
{
ledOnOffCheck[randLed]=0;
}
delay(100);
}
void setState( int led, boolean state)
{
bitWrite(ledStates,led, state);
}
void ledRefresh()
{
// refresh a different LED each time this is called.
if( refreshedLed++ > NUMBER_OF_LEDS) // increment to the next LED
refreshedLed = 0; // repeat from the first LED if all have been refreshed
// if( bitRead(ledNr[refreshedLed],ledOnOffCheck[refreshedLed]) == 1)
if(ledOnOffCheck[ledNr[refreshedLed]] = 1)
lightLed( refreshedLed );
}
// this function is identical to the sketch above
// it lights the given LED, the first LED is 0
void lightLed(int led)
{
// the following four lines convert LED number to pin numbers
int indexA = pairs[led/2][0];
int indexB = pairs[led/2][1];
int pinA = pins[indexA];
int pinB = pins[indexB];
// turn off all pins not connected to the given LED
for(int i=0; i < NUMBER_OF_PINS; i++)
if( pins[i] != pinA && pins[i] != pinB)
{ // if this pin is not one of our pins
pinMode(pins[i], INPUT); // set the mode to input
digitalWrite(pins[i],LOW); // make sure pull-up is off
}
// now turn on the pins for the given LED
pinMode(pinA, OUTPUT);
pinMode(pinB, OUTPUT);
if( led % 2 == 0)
{
digitalWrite(pinA,LOW);
digitalWrite(pinB,HIGH);
}
else
{
digitalWrite(pinB,LOW);
digitalWrite(pinA,HIGH);
}
}