Analog Output Experimentation (Photoresistor + Joystick Attempt)
Following the Arduino tutorial, I successfully set up the photoresistor and used it to control the blinking of an LED.
Following this tutorial, I successfully set up a circuit for using the joystick to control the LED lights blinking on and off. My next step was to integrate the above photoresistor into this circuit so that I could cause the LEDS to blink in different ways (such as blinking in rapid succession when dark and blinking once normally when light) depending on the ambient light values detected.
However, I had trouble getting this to execute properly.
Circuit
In addition to the joystick circuit, I had the photoresistor connected to analog pin 2 with a 220 ohm resistor.
Code Attempt
I made minor changes to the left-right controlling code to test out whether the photoresistor integration would work or not. If the ambient light levels fell below 450 while the joystick was being pushed to the left, then the LED should blink in rapid succession. If the joystick was pushed to the left with the light levels above 450, then it should blink once normally. If the joystick was not pushed to the left, then the LED would remain off. The result of this circuit and code was that there was no change. I don't see where the problem is now, but I'll continue to investigate. Hopefully I didn't overlook some silly mistake!
int UD = 0;
int LR = 0;
const int sensorPin = A2;
/* Arduino Micro output pins*/
int DWN = 13;
int UP = 12;
int LEFT = 11;
int RT = 10;
/*Arduino Micro Input Pins */
int IUP=A0;
int ILR=A1;
int MID = 10;
int LRMID = 0;
int UPMID = 0;
void setup(){
pinMode(DWN, OUTPUT);
pinMode(UP, OUTPUT);
pinMode(LEFT, OUTPUT);
pinMode(RT, OUTPUT);
digitalWrite(DWN, HIGH);
digitalWrite(UP, HIGH);
digitalWrite(LEFT, HIGH);
digitalWrite(RT, HIGH);
//calibrate center
LRMID = analogRead(ILR);
UPMID = analogRead(IUP);
}
void loop(){
int sensorReading = analogRead(A2);
UD = analogRead(IUP);
LR = analogRead(ILR);
// UP-DOWN
if(UD < UPMID - MID){
digitalWrite(DWN, HIGH);
} else {
digitalWrite(DWN, LOW);
}
if(UD > UPMID + MID){
digitalWrite(UP, HIGH);
} else {
digitalWrite(UP, LOW);
}
// LEFT-RIGHT
if(LR < LRMID - MID){
digitalWrite(LEFT, HIGH);
} else {
digitalWrite(LEFT, LOW);
}
if(LR > LRMID + MID && sensorReading <= 450){
digitalWrite(RT, HIGH);
delay(200);
digitalWrite(RT, LOW);
delay(200);
} else if (LR > LRMID + MID) {
digitalWrite(RT, HIGH);
} else {
digitalWrite(RT, LOW);
}
delay(400);
}
No comments:
Post a Comment