Week 2 - Lab 2 and more
So, I completed the first lab from our homework, it went off without a hitch. I understood the code, could comprehend the various in and out points between the breadboard and the Arduino. Took me about 20 minutes or so.
Here it is…
So, I decided to move on to the next stage.
I had several potentiometers lying around. In addition I had a whole bag of RGB LED’s that I was excited to use. I was hoping to get it so each potentiometer would control the intensity of each color. I hooked up everything as a logical evolution from the preceding lab and then modified the original code taking everything into account. I even soldered my leads onto the potentiometer to make sure te contacts were solid. When I loaded my code onto the board and fired it up it did nothing like I thought it would. I m sure there is a perfectly logical explanation but it totally escapes me. What ended up happening was the first knob acted as a dimmer like it did in the first lab. The second knob would switch between blue/green, which I suppose is what I intended. The lst knob merely caused the light to flicker between dark blue and a more white blue.
I altered my code several times to serial monitor each ADC pin and strangely all activity being controlled by the potentiometers was happening in the opposite rotation from the first lab. Meaning, when I turned the knob clockwise, which should decrease resistance and generates a “1024” serial read it ended up turning off the light gradually. This was only one strange symptom generated by my setup. I am going to bring it in to try to decipher it further.
Anyway here is my code, setup and video.
int potPin0 = 0; // Analog input pin that the potentiometer is attached to
int potPin1 = 1;
int potPin2 = 2;
int potValue0 = 0; // value read from the pot
int potValue1 = 0;
int potValue2 = 0;
int led9 = 9; // PWM pin that the LED is on. n.b. PWM 0 is on digital pin 9
int led8 = 8;
int led7 = 7;
void setup() {
// initialize serial communications at 9600 bps:
Serial.begin(9600);
}
void loop() {
potValue0 = analogRead(potPin0); // read the pot value
potValue1 = analogRead(potPin1);
potValue2 = analogRead(potPin2);
analogWrite(led9, potValue0/4); // PWM the LED with the pot value (divided by 4 to fit in a byte)
analogWrite(led8, potValue1/4);
analogWrite(led7, potValue2/4);
Serial.println(potValue2); // print the pot value back to the debugger pane
delay(10); // wait 10 milliseconds before the next loop
}
3 years ago • 0 notes