Question: using Arduino Modify Building a Controllable RGB LED Nightlight by creating your own function . Remember that your RGB LED is common anode the long

using Arduino

Modify Building a Controllable RGB LED Nightlight by creating your own function. Remember that your RGB LED is common anode the long wire goes to 5 volts. Your function should take an integer between 1 and 9 from the user. If the function receives a 1-3, the function should turn the RGB LED to blue. 1 = dim blue, 2 = brighter blue, and 3 = brightest blue. If the function receives a 4-6, the function should turn the RGB LED to green. 4 = dim green, 5 = brighter green, and 6 = brightest green. If the function receives a 7-9, the function should turn the RGB LED to red. 7 = dim red, 8 = brighter red, and 9 = brightest red. You will use the analogWrite() function to set the brightness of the LED you are controlling. Make sure you use the PWM pins when using analogWrite(). Use cin and get a number from the user between 0 and 9. 0 should turn all the LEDs off. If a number outside the 0 to 9 range is entered, whatever LED that is on should remain on.

Note that you are NOT using the LDR or push button in this sketch.

Write your own function setRGB_LED().

Your loop() function should only be:

void loop()

{

int num;

do

{

cout << Enter a number between 0 and 9: << endl;

cin >> num;

} while (num < 0 || num > 9);

setRGB_LED(num);

}

NOTE: Do not hardcode any numbers in the analogWrite() function. Use variables and/or constants only. These variables and constants should be declared inside your function if that is the only block where these variables and constants are used.

b)

Add a force sensor and resistor to your circuit. Create another function to get input from the force sensor. You will no longer use the user for input. You will build the following circuit using 1kW resistors. (Pretend round black part below is a force sensor.)

Your loop() function should only be the following code:

void loop()

{

int num;

//FORCE is the pin# that reads force sensor

num = sensorRead(FORCE);

//this function sets the LED, num is between 0-9

setRGB_LED(num);

delay(500);

}

NOTE: Your sketch will include two functions now: sensorRead() & setRGB_LED()

c)

One problem with the sketch about is that we dont know the range of values we might get from the force sensor without testing the force sensor first.

Create another function called calibrate(FORCE). This function should prompt the user to press the force sensor for a period of a few seconds, going from soft to hard. Using the readings, the function should set a range over which the force sensor will vary. This range should be used in the setRGB_LED() function.

setup() and loop() should now be:

int low_force;

int high_force;

const int FORCE = A0;

void setup()

{

Serial.begin(9600);

pinMode(red, OUTPUT);

pinMode(green, OUTPUT);

pinMode(blue, OUTPUT);

calibrate(FORCE);

}

void loop()

{

int num;

num = sensor_read(FORCE);

setRGB_LED(num);

delay(500);

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!