I'm trying to use my Arduino to keep track of how many times a relay turns a light on and off. The simplest way I can think to accomplish this would be to read input from a solar panel set up in front of the light bulb. To test the concept I've modified the sample "button" code to turn on the LED when voltage comes in from the solar panel, but it doesn't seem to be working.I have the solar panel positive lead feeding into pin 8, and the negative solar panel lead to ground. Here is the code as I have it so far:
// the setup function runs once when you press reset or power the board
const int led=13;
const int solar=8;
int solarstate = 0;
void setup() {
// initialize digital pin LED_BUILTIN as an output.
pinMode(led, OUTPUT);
pinMode(solar, INPUT);
}
// the loop function runs over and over again forever
void loop() {
solarstate = digitalRead(solar);
if (solarstate == HIGH) {
digitalWrite(led, HIGH);
} else {
digitalWrite(led, LOW);
}
}
Any thoughts or input would be greatly appreciated!
OK, changed gears. I found a photoresistor, and it works as the button in the button tutorial!
Now on to part 2 of the problem, I need to count the cycles and send the count to a serial monitor. The problem I now see is when the light hits it, it goes into rapid count mode and just keeps scrolling. How do I convince the setup to add 1 per light flash?
Updated code:
/ the setup function runs once when you press reset or power the board
const int led=13;
const int photo=8;
int photostate = 0;
int count=0;
void setup() {
// initialize digital pin LED_BUILTIN as an output.
Serial.begin(9600);
pinMode(led, OUTPUT);
pinMode(photo, INPUT);
}
// the loop function runs over and over again forever
void loop() {
photostate = digitalRead(photo);
if (photostate == HIGH) {
digitalWrite(led, HIGH);
count ++;
Serial.print(count);
} else {
digitalWrite(led, LOW);
}
It's giving the high counts because it adds one every time the pin is read HIGH. It should overflow as quickly as 9600 baud printing lets it and I don't see any code setting it back to zero.
Any large enough shift in voltage (while not exceeding VCC) can be used digitally.
In a pinch, a led can be used as a light detector just by placing the short lead in ground and the long one in A0. Expose to bright light and don't read A0 very often (analog read drains a tiny bit of charge) and you can see > 1V. A few quick reads will show the value drop fast. It's slow but it's easy and it works.
Please read the first post in any forum entitled how to use this forum. http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.
Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?
If they are wired with another resistor to make a voltage divider and if there is a significant difference between light levels they can be detected with digitalRead() - much simpler than using analogRead()