Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Arduino needed for this hw assignment. ECE 160 Arduino Lab 4 Name: pwm.ino Due:

ID: 2291368 • Letter: A

Question

Arduino needed for this hw assignment. ECE 160 Arduino Lab 4 Name: pwm.ino Due: see http llece 160 org Write a program to change the color of an RGB LED. Your program should query the three potentiometers and, based on the potentiometer (pot) setting, adjust the brightness of the Red (pot A0), Green (pot A1), and Blue (pot A2) LED. When the pot is fully counter clockwise, the associated color should be completely off. When the pot is fuly clockwise, the associated color should be completely on Analog Inputs: You can read in an analog input on any of the analog pins (port C) of the Arduino. To do this you call analogRead) analogRead) takes one argument, the pin to read in and returns a 1 bit value (0-1023 or 0-3FF) representing the voltage on the pin. Analog In Pin Pot A0 (leftmost, with buttons at bottom) A1 A2 (rightmost, with buttons at bottom) PWM PWM or Pulse Width Modulation, is a way of making an analog like waveform from a digital output. This works by varying the length of time the pulse is on vs the length of time the pulse is off (called the duty cycle). Doing this cycle at a high frequency (1 kHz in the case of the changes the analog voltage produced. Below are some examples of different duty cycles Arduino) makes the square wave (average out) at an analog voltage. Varying the duty cycle 50% duty cycle seria Deain 75% duty cycle seria seria 25% duty cycle Print (From SparkFun)

Explanation / Answer

// These constants won't change.
const int pot1 = A0; // Analog input pin that the potentiometer 1 is attached to
const int pot2 = A1; // Analog input pin that the potentiometer 2 is attached to
const int pot3 = A2; // Analog input pin that the potentiometer 3 is attached to

const int red   = 3;     // RED Color LED pin connected to Arduino's PWM pin 3
const int green = 5;    // GREEN Color LED pin connected to Arduino's PWM pin 5
const int blue = 9;    // BLUE Color LED pin connected to Arduino's PWM pin 9


void setup() {
   pinMode(red, OUTPUT);     // declare pin to be an output:
   pinMode(green, OUTPUT);   // declare pin to be an output:
   pinMode(blue, OUTPUT);    // declare pin to be an output:
   //By default all the potentiometer pins acts as input

}

void loop() {
// Reads pot1 and maps it range from 0-1023 to 0-255 for analogwrite
int RED_VALUE = map(analogRead(pot1),0,1023,0,255);
// Reads pot2 and maps it range from 0-1023 to 0-255 for analogwrite
int GREEN_VALUE = map(analogRead(pot2),0,1023,0,255);
// Reads pot3 and maps it range from 0-1023 to 0-255 for analogwrite
int BLUE_VALUE = map(analogRead(pot3),0,1023,0,255);

delay(1);   // Provide 1ms dealy for smooth running;

analogWrite(red, RED_VALUE); // Write the analog value in the variable to the pin
analogWrite(green, GREEN_VALUE); // Write the analog value in the variable to the pin
analogWrite(blue, BLUE_VALUE); // Write the analog value in the variable to the pin

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote