11.07.2015 Views

ARDX-experimenters-g.. - Oomlout

ARDX-experimenters-g.. - Oomlout

ARDX-experimenters-g.. - Oomlout

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Code (no need to type everything in just)Download the Code from ( http://tinyurl.com/crdum6 )(copy the text and paste it into an empty Arduino Sketch)CIRC-09/* }* A simple programme that will change the /** intensity of an LED based on the amount of * loop() - this function will start after setup* light incident on the photo resistor. * finishes and then repeat* */*/ void loop(){//PhotoResistor Pin int lightLevel = analogRead(lightPin); //Read theint lightPin = 0; //the analog pin the// lightlevel//photoresistor is lightLevel = map(lightLevel, 0, 900, 0, 255);//connected to //adjust the value 0 to 900 to//the photoresistor is not lightLevel = constrain(lightLevel, 0, 255);//calibrated to any units so //make sure the value is betwween 0 and 255//this is simply a raw sensor analogWrite(ledPin, lightLevel); //write the valuevalue (relative light)//LED Pinint ledPin = 9;//the pin the LED is connected to//we are controlling brightness so//we use one of the PWM (pulse//width modulation pins)}void setup(){pinMode(ledPin, OUTPUT); //sets the led pin to//outputNot Working? (3 things to try)LED is Remaining DarkThis is a mistake we continueto make time and time again, ifonly they could make an LEDthat worked both ways. Pull itup and give it a twist.It Isn't Responding toChanges in Light.Given that the spacing of thewires on the photo-resistor isnot standad, it is easy tomisplace it. Double check its inthe right placeStill not quite working?You may be in a room which iseither too bright or dark. Tryturning the lights on or off tosee if this helps. Or if you havea flashlight near by give that atry.Making it BetterReverse the response:Perhaps you would like the opposite response. Don'tworry we can easily reverse this response just change.analogWrite(ledPin, lightLevel); ---->analogWrite(ledPin, 255 - lightLevel);Upload and watch the response change.Night light:Rather than controlling the brightness of the LED inresponse to light, lets instead turn it on or off based on athreshold value. Change the loop() code with.void loop(){int threshold = 300;if(analogRead(lightPin) > threshold){digitalWrite(ledPin, HIGH);}else{digitalWrite(ledPin, LOW);}}Light controlled servo:Lets use our newly found light sensing skills to control a servo(and at the same time engage in a little bit of Arduino codehacking). Wire up a servo connected to pin 9 (like in CIRC-04).Then open the Knob example program (the same one we usedin CIRC-08) File > Sketchbook > Examples > Library-Servo > Knob. Upload the code to your board and watch as itworks unmodified.Using the full range of your servo:You'll notice that the servo will only operate over a limitedportion of its range. This is because with the voltage dividingcircuit we use the voltage on analog pin 0 will not range from0 to 5 volts but instead between two lesser values (thesevalues will change based on your setup). To fix this play withthe val = map(val, 0, 1023, 0, 179); line. For hints on what todo visit http://arduino.cc/en/Reference/Map .More, More, More:More details, where to buy more parts, where to ask more questions.http://tinyurl.com/cpa83c25

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!