Offline
Newbie
Karma: 0
Posts: 15
|
 |
« on: March 27, 2011, 02:53:23 pm » |
I'm trying to create a code that has an LED (2 in this case), randomly blinking on/off between set values. I've got this part working amongst the code below.
But I also want to incorporate a LDR sensor into it, so that the closer a hand gets (the less light it receives) the brighter and more stable (gradually stop blinking) the light gets.
//Work in progress Code int LED1 = 9; // LED connected to digital pin 9 int LED2 = 11;
long randOn = 0; // Initialize a variable for the ON time long randOff = 0; // Initialize a variable for the OFF time int val = 0; // variable used to store the value coming from the senor
void setup() // run once, when the sketch starts { randomSeed (analogRead (0)); // randomize pinMode( LED1, OUTPUT); // sets the digital pin as output pinMode( LED2, OUTPUT); }
void loop() // run over and over again { val = analogRead(0); // read the value from the sensor analogWrite(LED1, val/4); analogWrite(LED2, val/4); delay(10); randOn = random (10, 150); // generate ON time between 0.02 and 0.15 seconds randOff = random (250, 2000); // generate OFF time between 0.25 and 2.5 seconds digitalWrite(LED1, HIGH); // sets the LED on digitalWrite(LED2, HIGH); delay(randOn); // waits for a random time while ON digitalWrite(LED1, LOW); // sets the LED off digitalWrite(LED2, LOW); delay(randOff); // waits for a random time while OFF
}
Any ideas what I'm doing wrong? At the moment, it's only blinking the LED's randomly, not reacting to the sensor at all...
Thanks!
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Faraday Member
Karma: 12
Posts: 2857
ruggedcircuits.com
|
 |
« Reply #1 on: March 27, 2011, 02:54:55 pm » |
First question, how do you have the LDR hooked up? -- The Gadget Shield: accelerometer, RGB LED, IR transmit/receive, speaker, microphone, light sensor, potentiometer, pushbuttons
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 138
Posts: 19067
I don't think you connected the grounds, Dave.
|
 |
« Reply #2 on: March 27, 2011, 02:57:37 pm » |
analogWrite(LED1, val/4); analogWrite(LED2, val/4); delay(10); randOn = random (10, 150); // generate ON time between 0.02 and 0.15 seconds randOff = random (250, 2000); // generate OFF time between 0.25 and 2.5 seconds digitalWrite(LED1, HIGH); // sets the LED on digitalWrite(LED2, HIGH);
So, 1/100th of a second after you wrote a PWM value, you turn it full on anyway?
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 15
|
 |
« Reply #3 on: March 27, 2011, 03:08:37 pm » |
RuggedCircuits - I've got the LDR connected to a resistor, into the ground and 5.v of my arduino uno board?
AWOL - Should I not have the digital write function in the code?
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 138
Posts: 19067
I don't think you connected the grounds, Dave.
|
 |
« Reply #4 on: March 27, 2011, 03:12:02 pm » |
Probably not. Look at how "loop" well, loops, so things repeat. A digitalWrite will turn the LED either fully on or fully off (which is which depends on how you've wired the LED).
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
0
Offline
Faraday Member
Karma: 12
Posts: 2857
ruggedcircuits.com
|
 |
« Reply #5 on: March 27, 2011, 03:12:56 pm » |
I've got the LDR connected to a resistor, into the ground and 5.v of my arduino uno board? It's still not exactly clear, but how about you write a small program just to test your LDR. Take the analogRead() readings and print them to the serial terminal. -- Beat707: MIDI drum machine / sequencer / groove-box for Arduino
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 15
|
 |
« Reply #6 on: March 27, 2011, 03:19:40 pm » |
cool thanks,
what would you have instead of digitalWrite though?
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 138
Posts: 19067
I don't think you connected the grounds, Dave.
|
 |
« Reply #7 on: March 27, 2011, 03:20:38 pm » |
what would you have instead of digitalWrite though Personally? Nothing.
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 15
|
 |
« Reply #8 on: March 27, 2011, 03:23:55 pm » |
RuggedCircuits - my LDR works, I've used it with other codes, I've got it so that it gets brighter as you get near (as the LDR receives less light), I'm just trying to combine that with the random blinking, i.e when there is a set level of light it randomly blinks. Then when this light is reduced, it will become stable and begin getting brighter...
|
|
|
|
|
Logged
|
|
|
|
|
|