I am trying to use a light sensor to initiate my code. For example, If it gets dark outside I want the sensor to run a code when it's dark enough.
Okay, and? What's the problem? Code, hardware, or both? You haven't shown us anything, so there's nothing anyone can do until you rectify that.
here is the code.
#include <Servo.h>
const int buzzer = 4; //buzzer to arduino pin 9
Servo myservo;
int pos = 0;
void setup() {
myservo.attach(3); //attaches servo to pin 3
pinMode(7,OUTPUT);
pinMode(8,OUTPUT);
pinMode(buzzer, OUTPUT);
}
void loop() {
{digitalWrite(7,HIGH);//blinks led on and off
digitalWrite(8,HIGH);
delay(1000);
digitalWrite(7,LOW);
digitalWrite(8,LOW);
delay(1000);
digitalWrite(7,HIGH);
digitalWrite(8,HIGH);
delay(1000);
}
for (pos = 0; pos <= 180; pos += 1) {//moves servo in sweeping motion
myservo.write(pos);
delay(65); }
{
tone(buzzer,1000);
}
{digitalWrite(7,HIGH);
digitalWrite(8,HIGH);
delay(100);
digitalWrite(7,LOW);
digitalWrite(8,LOW);
delay(100);
digitalWrite(7,HIGH);
digitalWrite(8,HIGH);
delay(100);
noTone(buzzer);
}
for (pos = 180; pos >= 0; pos -= 1) {
myservo.write(pos);
delay(65); }
{
}
digitalWrite(7,HIGH);
digitalWrite(8,HIGH);
delay(1000);
digitalWrite(7,LOW);
digitalWrite(8,LOW);
delay(1000);
digitalWrite(7,HIGH);
digitalWrite(8,HIGH);
delay(1000);
for (pos = 0; pos <= 180; pos += 1) {
myservo.write(pos);
delay(65); }
{
tone(buzzer,1000);//makes sound
delay(100);
noTone(buzzer);
}
{digitalWrite(7,HIGH);
digitalWrite(8,HIGH);
delay(100);
digitalWrite(7,LOW);
digitalWrite(8,LOW);
delay(100);
digitalWrite(7,HIGH);
digitalWrite(8,HIGH);
delay(100);
tone(buzzer,1000)
delay(100)
noTone(buzzer);
}
for (pos = 180; pos >= 0; pos -= 1) {
myservo.write(pos);
delay(65); }
delay(300000);//waits for 5 min
{
}
}
And the light sensor is, what exactly? Hooked up how?
If you want help, start providing details.
When you get around to picking a light sensor, let us know the details.
For better readability, you should not use magic numbers. The I/O pins love to have a functional name.
and for even better readability, put your repeated code as a function - especially if its tested and working correctly.
How are we supposed to know what is connected to pin 8?
number 8 is for the second led
my bad ![]()