I am wondering how to address certain output pis on a shift register. Specifically I am using a WS2803 and want to address certain pins to have the LED output a certain color.
You send it 18 bytes of data, it adjusts its 18 outputs accordingly.
for (x=0; x<18; x=x+1){
SPI.transfer(ws2803Array[x]);
}
WS2803-preliminary-En.pdf (437 KB)
This may sound like a stupid question but, in the code you posted where do I put the info i want sent to light the LEDs.
Define the array in pre-setup code
byte sw2803Array[18]; // array of 18 bytes, values not declared
//or
bye sw2803Array[] = {0,1,2,3,4,5,67,8,9,10,11,12,13,14,15,16,17,}; // define some values when array is declared
Then address the elements as I did above either writing or reading:
sw2803Array[12] = 0x3f;
tempValue = sw2803Array[12]; // tempValue now equals 0x3f
OK, so if I put this in the setup loop:
bye sw2803Array[] = {0,1,2,3,4,5,67,8,9,10,11,12,13,14,15,16,17,};
then I can say something like;
int value = analogRead(pressure) / 4;
if((value >= 1) && (value <= 150))
{
sw2803Array[12] = 0xFF;
sw2803[0,1,2,3,4,5,6,7,8,9,10,13,14,15,16,17] = 0x00;
}
in the void loop and that will set the 12th bit high and all of the rest low when the pressure sensor reads between the designated values?
This goes at the top of the sketch before setup
byte sw2803Array[] = {0,1,2,3,4,5,67,8,9,10,11,12,13,14,15,16,17,};
This might work, but you'll probably have to do is as loop:
sw2803Array[0,1,2,3,4,5,6,7,8,9,10,13,14,15,16,17] = 0x00;
// write as:
for (x=0; x<18; x=x+1){
sw2803Array[x] = 0; / clear all values
}
sw2803[12] = 0xff; // put in new value
If you only want LEDs on & off, then simple shift registers are sufficient, such as TPIC6B595 to pull a cathode low. ws2803 is good for 256 levels of brightness control.
What I am trying to do in this project is to have 4 pressure sensors and 4 RGB LEDs and have the LED light blue when the reading from the sensor is 0, green when the reading is between 1 and 150, and red when the reading is above 150. I have set it up and working with 2 RGB LEDs without the IC but since each led uses 3 pins on the arduino I dont have enough so that is why I am trying to use an IC. Also when I have used non PWM pins on the Arduino the leds dont seem to act right (light the right colors).
darbye:
I am wondering how to address certain output pis on a shift register. Specifically I am using a WS2803 and want to address certain pins to have the LED output a certain color.
darbye:
What I am trying to do in this project is to have 4 pressure sensors and 4 RGB LEDs and have the LED light blue when the reading from the sensor is 0, green when the reading is between 1 and 150, and red when the reading is above 150.... I dont have enough [pins] so that is why I am trying to use an IC.
WS2803 != "RGB LEDs"
Which are we doing?
I am trying to have the WS2803 control/power the LEDs since without it I don't have enough outputs on the Arduino to use all 4 LEDs.
You have stated that from each that you only want R or G or B.
From my point of view, using a WS2803 for that involves a lot of unnecessary hassle.
Using a 74HC595 (two) would be a lot easier.
(If orange or aqua or purple were part of the plan then the WS2803 would probably be more appropriate.)
i continue to write that using a arduino to have 4x(1 sensor to power 1 led) is overkill !
Not sure what else I would use?
i can't say how to make the pure electronic for the output of the sensor to power the led the way you want
sorry
Arduino is just fine to read 4 sensors, and light up different colors based on the value read.
For example:
0-149, red
150-299, green
300-449, blue
500-649, red+green
650-799, red+blue
800-949, green+blue
950-1023, red+green+blue
Then 2 simple shift registers is all that is needed, with 2 bytes of data, 1 bit representing 1 color in each byte:
byte12= 0b00000000; // with 7-6-5-4-3-2-1-0 = not used, not used, r1, g1, b1, r2, g2, b2
byte34 = 0b00000000; // with 7-6-5-4-3-2-1-0 = not used, not used, r3, g3, b3, r4, g4, b4
yes as arduino seller CrossRoads will not write it is overkill
That makes sense to me. So if I wanted the 1st LED to light red I would send 0b00100000 to the register?
int speaker = 2;
int led[] = {
3, 5, 6};
int led2[] = {
9, 10, 11};
int pressure = A1;
int pressure2 = A2;
const boolean ON = LOW;
const boolean OFF = HIGH;
const boolean RED[] = {
ON, OFF, OFF};
const boolean GREEN[] = {
OFF, ON, OFF};
const boolean BLUE[] = {
OFF, OFF, ON};
void setup() {
Serial.begin(9600);
for(int i = 0; i < 3; i++){
pinMode(led[i], OUTPUT);
pinMode(led2[i], OUTPUT);
pinMode(speaker, OUTPUT);
}
}
void loop() {
int value = analogRead(pressure) / 4;
if((value >= 1) && (value <= 150))
{
setColor(led, GREEN);
}
else if(value >= 151)
{
setColor(led, RED);
}
else
{
setColor(led, BLUE);
}
int value2 = analogRead(pressure2) / 4;
if((value2 >= 1) && (value2 <= 150))
{
setColor(led2, GREEN);
}
else if(value2 >= 151)
{
setColor(led2, RED);
}
else
{
setColor(led2, BLUE);
}
if((value >= 1) && (value <= 150) || (value2 >= 1) && (value2 <= 150))
{
digitalWrite(speaker, HIGH);
}
else
{
digitalWrite(speaker, LOW);
}
Serial.println(value);
Serial.println(value2);
delay(100);
}
void setColor(int* led, boolean* color){
for(int i = 0; i < 3; i++){
digitalWrite(led[i], color[i]);
}
}
void setColor(int* led, const boolean* color){
boolean tempColor[] = {
color[0], color[1], color[2] };
setColor(led, tempColor);
}
That is my code that works with two RGB LEDs hooked directly to the Arduino. Would I just change the "set color" to "shift out" the bytes i want based on the sensor readings?
Yes, just shift out the 2 bytes to 2 shift registers, if anodes are connected to the shift register outputs and current limit resistor to cathode & Gnd, then a 1 output will make the LED turn on.
I am using common anode so I connected the anode to 5v and the cathodes to the shift register outputs through a resistor and I am pretty sure a 0 will make the led turn on the color of the cathode that has the 0 sent to it. Is there anything I have to do to set up the 595 other than set the clock, data, and latch pins as outputs? There always seems to be a statement like this in there:
for(int i = 0; i < 256; i++)
SRCLR connects to +5
OE/ connects to GND, or to a PWM pin if brightness control is desired.
If you use SPI to transfer the data (instead of the slower shiftOut) then your sketch needs to include the SPI library
#include <SPI.h>
//That defines pins 11,12,13 for SPI use. 13 connects to SRCLK, 11 connects to Ser in
//define a slave select pin
byte ssPin = 10;
//and declare it as an output in setup
pinMode (ssPin, OUTPUT);
//Then start SPI:
SPI.begin();
//In loop, to send data out:
digitalWrite (ssPin, LOW);
SPI.transfer(byte1);
SPI.transfer(byte2); // assuming '595s are daisy chained
digitalWrite (ssPin, HIGH); // data appears at 595 output on this rising edge