I have a Rotary Angle Sensor and a Macetech ShiftBrite RGB LED connected to Inte
ID: 2078959 • Letter: I
Question
I have a Rotary Angle Sensor and a Macetech ShiftBrite RGB LED connected to Intel Galileo Gen 2. Please write an Arduino Code that will do the following:
1. When the Rotary Angle Sensor is rotated from 0 to 1/3 of 10 kohms, the ShiftBrite RGB LED will change from a light green to a solid green and no other color.
2. When the Rotary Angle Sensor is rotated from 1/3 to 2/3 10 kohms, the ShiftBrite RGB LED will change from a solid green to a light yellow to a solid yellow and no other color.
3. When the Rotary Angle Sensor is rotated from 2/3 to 10 kohms, the ShiftBrite RGB LED will change from a solid yellow to a light red and to a solid red and no other color.
Note: the code below only outputs shades of red and not the Green, Yellow, or Red according to the if statements and the value of the rotary angle sensor input value. Also, am I not supposed to have the CI signal to change the colors according to the if else statements? Please help to correct this problem!
Code:
#define LED 2 // connect LED to digital pin2
int val = 0; // variable for serial output
int rotarySensor = 0; // input pin for rotary angle sensor
int R = 11; // define red output pin
int G = 10; // define green output pin
int B = 9; // define blue output pin
//#define COMMON_ANODE
void setup() {
pinMode(LED, OUTPUT); // declaring the ledPin as an OUTPUT
pinMode(R, OUTPUT); // set red output pin
pinMode(G, OUTPUT); // set green output pin
pinMode(B, OUTPUT); // set blue output pin
pinMode(A0, INPUT); // set rotary input pin
Serial.begin(115200); // setup serial
}
void loop() {
val = analogRead(A0); // read the value for the rotary sensor
rotarySensor = analogRead(A0); // read rotary sensor and assign it to input variable
if (rotarySensor < 85) { // if true set light green then solid green
setColor(144, 238, 144); // light green
delay(100); // wait 100 milliseconds
setColor(0, 100, 0); // solid green
}
else if (rotarySensor > 85 && rotarySensor < 170) {// if true set light yellow then solid yellow
setColor(255, 255, 224); // light yellow
delay(100); // wait 100 milliseconds
setColor(255, 255, 0); // solid yellow
}
else if (rotarySensor > 170 && rotarySensor < 256) {// if true set light red then solid red
setColor(255, 204, 203); // light red
delay(100); // wait 100 milliseconds
setColor(139, 0, 0); // solid red
}
Serial.println("Temp: "); // write to serial monitor
Serial.println(val); // write to serial monitor
}
void setColor(int red, int green, int blue) {// set colors for output
#ifdef COMMON_ANODE
red = red; // set the red shade
green = green; // set the green shade
blue = blue; // set the blue shade
#endif
analogWrite(R, red); // output the red color
analogWrite(G, green); // output the green color
analogWrite(B, blue); // output the blue color
}
Explanation / Answer
#define LED 2
int val = 0;
int rotarySensor = 0;
int redPin = 11;
int greenPin = 10;
int bluePin = 9;
#define COMMON_ANODE
void setup()
{
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
}
void loop()
{
val = analogRead(A0);
rotarySensor = analogRead(A0);
// Green to dark green
if (rotarySensor < 85)
{
setColor(0, 150, 0);
delay(1000); // to increase visibility
setColor(0, 255, 0);
}
// light yellow to dark yellow
else if (rotarySensor > 85 && rotarySensor < 170)
{
setColor(150, 150, 220);
delay(1000);
setColor(255, 255, 0);
}
// light red to dark red
else if (rotarySensor > 170 && rotarySensor < 256)
{
setColor(150, 0, 0);
delay(1000);
setColor(255, 0, 0);
}
}
void setColor(int red, int green, int blue)
{
#ifdef COMMON_ANODE
red = 255 - red;
green = 255 - green;
blue = 255 - blue;
#endif
analogWrite(redPin, red);
analogWrite(greenPin, green);
analogWrite(bluePin, blue);
}
Changes Made In Given Code
Since this is a common anode led. The vaue must be subtracted from 255. Also i have changes the delay time to 1s to improve visibility
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.