Arduino Challenge #3 analog to digital write
Let me begin to explain what I did at the beginning.
This is the code:
beginning 2 pins are used the 0 goes to an analog and the 9 to digital ledpin. In this exercise we got the information from the analog pin to come from a sensorPin, this sensor was a light sensor. The parameters ranged from 9600 to 0.
0 to 800 we set it up to be darkness, then the signal told the sensor to send a signal to the led and turn it on. Anything above 800 was light therefore the light was off. The problem here was the resistors since in order to get the signal to that level we have to use a 10k ohm resistor in both the light and the sensor. 20k so we can get the level of data coming through. Now this is a video with 20 10k ohm resistors.
You will see that the light had to be really bright outside in order for it to work. I had to use the phone lamp to make it work.
On this next step I added the MAP command, which maps out the numbers that the digital led read to tell the light to be bright or dim.
Here is the code.
PHOTORESISTOR! aight, writing that here because I didn't know the name of it.
Now onto this weeks challenge.
DRIVING AN RGB LED
1. the point of this experiment was to run a program that will run an RGB (red, green and blue) Led.
2. The program will run the colors 8 of them if you count black as a color when the LED is off for an amount of time. (1000 = 1 second)
3. at the end we use the statement (for) this statement is a loop that runs forever.
This is the code:
const int RED_PIN = 9;
const int GREEN_PIN = 10;
const int BLUE_PIN = 11;
int DISPLAY_TIME = 10; // In milliseconds
void setup()
{
pinMode(RED_PIN, OUTPUT);
pinMode(GREEN_PIN, OUTPUT);
pinMode(BLUE_PIN, OUTPUT);
}
void loop()
{
mainColors();
showSpectrum();
}
void mainColors()
{
// Off (all LEDs off):
digitalWrite(RED_PIN, LOW);
digitalWrite(GREEN_PIN, LOW);
digitalWrite(BLUE_PIN, LOW);
delay(1000);
// Red (turn just the red LED on):
digitalWrite(RED_PIN, HIGH);
digitalWrite(GREEN_PIN, LOW);
digitalWrite(BLUE_PIN, LOW);
delay(1000);
// Green (turn just the green LED on):
digitalWrite(RED_PIN, LOW);
digitalWrite(GREEN_PIN, HIGH);
digitalWrite(BLUE_PIN, LOW);
delay(1000);
// Blue (turn just the blue LED on):
digitalWrite(RED_PIN, LOW);
digitalWrite(GREEN_PIN, LOW);
digitalWrite(BLUE_PIN, HIGH);
delay(1000);
// Yellow (turn red and green on):
digitalWrite(RED_PIN, HIGH);
digitalWrite(GREEN_PIN, HIGH);
digitalWrite(BLUE_PIN, LOW);
delay(1000);
// Cyan (turn green and blue on):
digitalWrite(RED_PIN, LOW);
digitalWrite(GREEN_PIN, HIGH);
digitalWrite(BLUE_PIN, HIGH);
delay(1000);
// Purple (turn red and blue on):
digitalWrite(RED_PIN, HIGH);
digitalWrite(GREEN_PIN, LOW);
digitalWrite(BLUE_PIN, HIGH);
delay(1000);
// White (turn all the LEDs on):
digitalWrite(RED_PIN, HIGH);
digitalWrite(GREEN_PIN, HIGH);
digitalWrite(BLUE_PIN, HIGH);
delay(1000);
}
void showSpectrum()
{
int x; // define an integer variable called "x"
for (x = 0; x < 768; x++) //for statement, makes the transition of the led colors look smoother.
// Each time we loop (with a new value of x), do the following:
{
showRGB(x); // Call RGBspectrum() with our new x
delay(DISPLAY_TIME); // Delay for 10 ms (1/100th of a second)
}
}
void showRGB(int color)
{
int redIntensity;
int greenIntensity;
int blueIntensity;
if (color <= 255) // zone 1
{
redIntensity = 255 - color; // red goes from on to off
greenIntensity = color; // green goes from off to on
blueIntensity = 0; // blue is always off
}
else if (color <= 511) // zone 2
{
redIntensity = 0; // red is always off
greenIntensity = 255 - (color - 256); // green on to off
blueIntensity = (color - 256); // blue off to on
}
else
{
redIntensity = (color - 512); // red off to on
greenIntensity = 0; // green is always off
blueIntensity = 255 - (color - 512); // blue on to off
}
analogWrite(RED_PIN, redIntensity);
analogWrite(BLUE_PIN, blueIntensity);
analogWrite(GREEN_PIN, greenIntensity);
}
Video of RGB
Explaining the code:
In this code we worked with (const int) this function gives a constant signal until it is not. It got attached to the pins where the rgb was hooked 9,10,11
we used the pinMode to make the three pins outputs
Loop
created a maincolor loop blinking
and a show spectrum look dimmed pretty cool!
the maincolor loop was pretty straight forward no and off function.
The one that was interesting to see was the spectrum which used the FOR statement.
This statement allowed for the spectrum to work and the led light to go from color to color gradually instead of on and off abruptly.
This second part is confusing. All I got from the "for" statement is that gives a variable a function to increase the light one at a time by adding the x++. Genius! that is why the ++ at the end. That is how the illusion of the spectrum gets made.
2nd challenge - driving multiple leds creating an ARRAY { x,x1,x2} and the statement "for"
circuit diagram
In this challenge we are running 8 leds using an array list. This list allows you to create a list that you can use to let arduino go one by one or pick from them to turn them on and off.
This is the result.
loop 1 turns leds on and off
loop 2 added a sequence
loop 3 was the ping pong feeling letting the lights all turn on and turning off in the order they turned on.
if you need the code let me know, I got it from the link.
Continuation:
The hooking up of everything was pretty easy I just ended plugging the voltage pin on the opposite pin to make it work at the end. I made the mistake of hooking all the resistors on the negative side.
My bad!
Challenge: On this challenge I used the array to hook all the leds with the Photoresistor.
Here is the code:
This next code is using the index
as you can see it is less work and same result.
For the final Challenge I combined the Photoresistor and the one after another loop.
My goal was for the lights to go out gradually but all I got is that whenever the light turns off the leds go one after another. Pretty neat but not what I expected. My main goal was to have the lights dimmed as the light approached the photoresistor. This is what I got instead.
This is the code that I ended up using. If anyone can help me not lose all the light but have it decrease it's brightness that would be nice.
That is all I wrote! thanks and have a great day! and happy Thanksgiving!
IF YOU GOT HERE HERE IS THE SECOND
SECOND PART RESULTS ON THIS ONE I TOOK OF THE DIGITAL WRITE PART AND JUST LEFT THE ANALOG PART TO HAVE THAT DIMMING EFFECT.






Comments
Post a Comment