Write an Arduino code which by different valuespositions of

Write an Arduino code which, by different values/positions of a potentiometer input device, changes the rate of an LED blinking and synchronously causes variation in the Shiftbrite RGB LED.

Solution

Please find below the Arduino code with comments for better understanding on the code:

#define clockpin 13 // CI
#define latchpin 9 // LI
#define enablepin 10 // EI
#define datapin 11 // DI

#define NumLEDs 2

int LEDChnnls[NumLEDs][3] = {0}; //Declaration part
int SB_CmmdMode;
int SB_RedCmmd;
int SB_GreenCmmd;
int SB_BlueCmmd;

void setup() {

pinMode(datapin, OUTPUT);
pinMode(latchpin, OUTPUT);
pinMode(enablepin, OUTPUT);
pinMode(clockpin, OUTPUT);
SPCR = (1<<SPE)|(1<<MSTR)|(0<<SPR1)|(0<<SPR0);
digitalWrite(latchpin, LOW);
digitalWrite(enablepin, LOW);


}

void SB_SendPacket() {

if (SB_CmmdMode == B01) { //if statement
SB_RedCmmd = 120;
SB_GreenCmmd = 100;
SB_BlueCmmd = 100;
}

SPDR = SB_CmmdMode << 6 | SB_BlueCmmd>>4;
while(!(SPSR & (1<<SPIF)));
SPDR = SB_BlueCmmd<<4 | SB_RedCmmd>>6;
while(!(SPSR & (1<<SPIF)));
SPDR = SB_RedCmmd << 2 | SB_GreenCmmd>>8;
while(!(SPSR & (1<<SPIF)));
SPDR = SB_GreenCmmd;
while(!(SPSR & (1<<SPIF)));

}

void WriteLEDArray() {

SB_CmmdMode = B00; // This will Write to PWM control registers
for (int h = 0;h<NumLEDs;h++) {
   SB_RedCmmd = LEDChnnls[h][0];
   SB_GreenCmmd = LEDChnnls[h][1];
   SB_BlueCmmd = LEDChnnls[h][2];
   SB_SendPacket();
}

delayMicroseconds(15);
digitalWrite(latchpin,HIGH); // This will latch data into registers
delayMicroseconds(15);
digitalWrite(latchpin,LOW);

SB_CmmdMode = B01; // This will Write to current control registers
for (int z = 0; z < NumLEDs; z++) SB_SendPacket();
delayMicroseconds(15);
digitalWrite(latchpin,HIGH); // This will latch data into registers
delayMicroseconds(15);
digitalWrite(latchpin,LOW);

}

void loop() {

LEDChnnls[0][0] = 1023; //LEDChnnls assignment
LEDChnnls[0][1] = 0;
LEDChnnls[0][2] = 0;

LEDChnnls[1][0] = 0;
LEDChnnls[1][1] = 0;
LEDChnnls[1][2] = 1023;

WriteLEDArray();       //Write to LED Array
delay(200);

LEDChnnls[0][0] = 0;
LEDChnnls[0][1] = 0;
LEDChnnls[0][2] = 1023;

LEDChnnls[1][0] = 1023;
LEDChnnls[1][1] = 0;
LEDChnnls[1][2] = 0;

WriteLEDArray();       //Write to LED Array
delay(200);


}

Write an Arduino code which, by different values/positions of a potentiometer input device, changes the rate of an LED blinking and synchronously causes variati
Write an Arduino code which, by different values/positions of a potentiometer input device, changes the rate of an LED blinking and synchronously causes variati

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site