IR Distance Sensor
The IR Distance Sensor uses infrared to send and receive signals. It is similar to the ultrasonic sensor we experimented with in the previous class, however, IR Distance sensor's range is smaller than the ultrasonic sensor. I still can't decide whether to use IR distance sensor or the ultrasonic sensor on my halloween project because one is more stable than the other but the other's range is greater and more adjustable. Tutorial 1 Tutorial 2 Materials: 1. Arduino board 2. IR Distance sensor 3. LED "Infrared distance sensors are useful for measuring distances without actually touching a surface. The three wires protruding from a distance sensor represent +5V (in most cases), Gnd (Ground) and signal. These are almost always color coded with black as ground, red as +V and white or yellow as the signal." Connect the black wire to Gnd. Connect the white wire to the analog pin A0. Connect the red wire to +5v. The LED will be at pin 13. The code that we'll be using is similar to the one we used for the potentiometer. We've modified it a little so that when the sensor detect something in its way, the LED will turn on. |
// Declare the used sensor pin <br>int sensorPin = A0;
int LED = 13; // Declare the connected LED
void setup(){
// Start the Serial connection
Serial.begin(9600);
}
void loop(){
// Read the analog value of the sensor
int val = analogRead(A0);
// Print the value over Serial
Serial.println(val);
// Write the value to the LED using PWM
analogWrite(LED, val);
// Wait a little for the data to print
delay(100);
}
After you've uploaded the code, open up either the serial monitor or the serial plotter to see if you've completed the circuit. I played around with the distance, and also tried using other sizes LED. I have also tried putting two tiny LED together. The only difficult thing is to figure out how I will adapt the set up to my Halloween project. And what exactly will I make? To answer that question, I tried duplicating a simple Halloween project.
Halloween Spooky Skull
Materials:
1. Arduino board
2. RGB LED
So I tried to follow this tutorial instead of the fade one -- in which I'm not even going to post the link for the fade one because it doesn't work and there's no point to sharing the link if we won't make any good use out of it. However, I'll post the code to the fade one that I've tried. I didn't really understand how the fading work with that code. Even though I modified the numbers, it seemed like the LED turn on duration is just shorter. I tried everything but there was no 'fade effect.'
Code:
int ledPin = 13;
void setup() {
}
void loop() {
for(int fadeValue = 0 ; fadeValue <= 10; fadeValue +=10) {
analogWrite(ledPin, fadeValue);
delay(60);
}
for(int fadeValue = 255 ; fadeValue >= 10; fadeValue -=10) {
analogWrite(ledPin, fadeValue);
delay(200);
}
}
The fade effect works! But I have one problem-- the sequence didn't include red light when it should. I tried modifying things and changing the code, but I couldn't find out what was wrong. I am however, satisfied with the result and the modification I've made.
Halloween Spooky Skull Code:
// RGB LED Light Up and Cycle Colors
// From Interactive Art Session at 757 Makerspace on October 17, 2015
// Design by Akin Yildiz and Beau Turner
// Global Variables Go Here
const int redPin = 13;
const int greenPin = 12;
const int bluePin = 11;
// The setup section runs one time at the start of the program
void setup() {
setColorRgb(0,0,0); // Set the the LED to off
}
//The loop section repeats over and over
void loop() {
unsigned int rgbColor[3];
rgbColor[0] = 255; // Start Red
rgbColor[1] = 0;
rgbColor[2] = 0;
// Cycle colors + & -
for (int decColor = 0; decColor < 3; decColor += 1) {
int incColor = decColor == 2 ? 0 : decColor + 1;
// Cross-fade colors
for(int i = 0; i < 255; i += 1) {
rgbColor[decColor] -= 1;
rgbColor[incColor] += 1;
setColorRgb(rgbColor[0], rgbColor[1], rgbColor[2]);
delay(5);
}
}
}
void setColorRgb(unsigned int red, unsigned int green, unsigned int blue) {
analogWrite(redPin, red);
analogWrite(greenPin, green);
analogWrite(bluePin, blue);
}