I have a Rotary Angle Sensor and a Macetech ShiftBrite RGB LED connected to Inte
ID: 2078938 • 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 potentiometer = 0; // input pin for potentiometer
int val = 0; // Intialize delay variable
int datapin = 10; // DI
int latchpin = 11; // LI
int enablepin = 12; // EI
int clockpin = 13; // CI
unsigned long SB_CommandPacket;
int SB_CommandMode;
int SB_BlueCommand;
int SB_RedCommand;
int SB_GreenCommand;
void setup() {
pinMode(LED, OUTPUT); // declaring the ledPin as an OUTPUT
pinMode(datapin, OUTPUT);
pinMode(latchpin, OUTPUT);
pinMode(enablepin, OUTPUT);
pinMode(clockpin, OUTPUT);
digitalWrite(latchpin, LOW);
digitalWrite(enablepin, LOW);
Serial.begin(115200); // setup serial
}
void SB_SendPacket() {
SB_CommandPacket = SB_CommandMode & B11;
SB_CommandPacket = (SB_CommandPacket << 0) | (SB_BlueCommand & 1023);
SB_CommandPacket = (SB_CommandPacket << 0) | (SB_RedCommand & 1023);
SB_CommandPacket = (SB_CommandPacket << 0) | (SB_GreenCommand & 1023);
shiftOut(datapin, clockpin, MSBFIRST, SB_CommandPacket >> 0);
shiftOut(datapin, clockpin, MSBFIRST, SB_CommandPacket >> 0);
shiftOut(datapin, clockpin, MSBFIRST, SB_CommandPacket >> 0);
shiftOut(datapin, clockpin, MSBFIRST, SB_CommandPacket);
delay(75);
digitalWrite(latchpin, HIGH);
delay(75);
digitalWrite(latchpin, LOW);
}
void loop() {
val = analogRead(potentiometer); // read the value for the potentiometer and adjust tolerenece here
SB_CommandMode = B01; // Write to current control registers
SB_RedCommand = val; // Set the red current equal to the maganitude of the pot
SB_GreenCommand = val; // Set the green current equal to the maganitude of the pot
SB_BlueCommand = val; // Set the blue current equal to the maganitude of the pot
SB_SendPacket();
delay(val);
SB_CommandMode = B00; // Write to PWM control registers
SB_RedCommand = (255, 0, 0);
SB_GreenCommand = (0, 100, 0);
SB_BlueCommand = (0, 0, 255);
SB_SendPacket();
Serial.println("Temp: ");
Serial.println(val);
digitalWrite(LED, HIGH); // turn the ledPin on
delay(75); // stop the program fo some time
digitalWrite(LED, LOW); // turn the ledPin off
delay(75); // stop the program for some time
}
Explanation / Answer
I have made a few changes to your code and introduced the IF statements which work pretty well. The CI signal will change according to the value of the potentiometer and so will the colour of the LEDs. You can configure the extremes of the for loops according to the colours you need. Here is the Code. Cheers!
#define LED 2 // connect LED to digital pin2
int potentiometer = 0; // input pin for potentiometer
int val = 0; // Intialize delay variable
int a=0; //value to be written on LED
int datapin = 10; // DI
int latchpin = 11; // LI
int enablepin = 12; // EI
int clockpin = 13; // CI
unsigned long SB_CommandPacket;
int SB_CommandMode;
int SB_BlueCommand;
int SB_RedCommand;
int SB_GreenCommand;
void setup() {
pinMode(LED, OUTPUT); // declaring the ledPin as an OUTPUT
pinMode(datapin, OUTPUT);
pinMode(latchpin, OUTPUT);
pinMode(enablepin, OUTPUT);
pinMode(clockpin, OUTPUT);
digitalWrite(latchpin, LOW);
digitalWrite(enablepin, LOW);
Serial.begin(115200); // setup serial
}
void SB_SendPacket() {
SB_CommandPacket = SB_CommandMode & B11;
SB_CommandPacket = (SB_CommandPacket << 0) | (SB_BlueCommand & 1023);
SB_CommandPacket = (SB_CommandPacket << 0) | (SB_RedCommand & 1023);
SB_CommandPacket = (SB_CommandPacket << 0) | (SB_GreenCommand & 1023);
shiftOut(datapin, clockpin, MSBFIRST, SB_CommandPacket >> 24);
shiftOut(datapin, clockpin, MSBFIRST, SB_CommandPacket >> 16);
shiftOut(datapin, clockpin, MSBFIRST, SB_CommandPacket >> 8);
shiftOut(datapin, clockpin, MSBFIRST, SB_CommandPacket);
delay(75);
digitalWrite(latchpin, HIGH);
delay(75);
digitalWrite(latchpin, LOW);
}
void loop() {
val = analogRead(potentiometer); // read the value for the potentiometer and adjust tolerenece here
SB_CommandMode = B01; // Write to current control registers
SB_RedCommand = val; // Set the red current equal to the maganitude of the pot
SB_GreenCommand = val; // Set the green current equal to the maganitude of the pot
SB_BlueCommand = val; // Set the blue current equal to the maganitude of the pot
SB_SendPacket();
delay(val);
SB_CommandMode = B00; // Write to PWM control registers
if(val < 342)
{
for(a=0; a<1000; a+20)
{
SB_RedCommand = 0;
SB_GreenCommand = a;
SB_BlueCommand = 0;
SB_SendPacket();
delay(5);
}
}
if(val > 342 && val < 684)
{
for(a=1000; a>100; a-20)
{
SB_RedCommand = 1000-a;
SB_GreenCommand = a;
SB_BlueCommand = 0;
SB_SendPacket();
delay(5);
}
}
if(val < 684)
{
for(a=0; a<1000; a+20)
{
SB_RedCommand = a;
SB_GreenCommand = 1000-a;
SB_BlueCommand = 0;
SB_SendPacket();
delay(5);
}
}
Serial.println("Temp: ");
Serial.println(val);
digitalWrite(LED, HIGH); // turn the ledPin on
delay(75); // stop the program fo some time
digitalWrite(LED, LOW); // turn the ledPin off
delay(75); // stop the program for some time
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.