here is the working code with the button. I did follow a guide i found online (forgot to bookmark it)
#include <Servo.h>;
// pushbutton pin
const int buttonPin = 2;
// servo pin
const int servoPin = 9;
Servo servo;
//create a variable to store a counter and set it to 0
int counter = 0;
void setup()
{
servo.attach (servoPin);
// Set up the pushbutton pins to be an input:
pinMode(buttonPin, INPUT);
}
void loop()
{
// local variable to hold the pushbutton states
int buttonState;
//read the digital state of buttonPin with digitalRead() function and store the //value in buttonState variable
buttonState = digitalRead(buttonPin);
//if the button is pressed increment counter and wait a tiny bit to give us some //time to release the button
if (buttonState == LOW) // light the LED
{
counter++;
delay(150);
}
if(counter == 0)
servo.write (175); // zero degrees
else if(counter == 1)
servo.write(120);
//else reset the counter to 0 which resets thr servo to 0 degrees
else
counter = 0;
}
the new code. I think i added the light level correctly. All it does is make the servo jitter from 120deg to 180 deg
#include <Servo.h>
// servo set up
Servo servo1;
//light sensor set up
const int photoSen = A0; //photoresistor pin
const int sunny = 70; // light val for sunny, can be changed
int lightLevel; //light level
void setup() {
// put your setup code here, to run once:
pinMode(photoSen, INPUT);
lightLevel = analogRead(photoSen);
servo1.attach(9);
}
void loop() {
// put your main code here, to run repeatedly:
int position;
lightLevel = analogRead(photoSen);
if(lightLevel > sunny){ //stays off if sunny
servo1.write(180);
}
if(lightLevel < sunny);{//turn on
servo1.write(120);
}
if(lightLevel == sunny);{
servo1.write(120);
}
}
I added a serial print function so i can check if my photoresistor is working. it is.
void loop() {
// put your main code here, to run repeatedly:
lightLevel = analogRead(photoSen);
Serial.println("lightLevel");
Serial.println(lightLevel);
delay(5000);