I get a 16bit word from my chip and I split it into 2 bytes when it comes in via SPI. How do I send the two bytes to LEDs on output pins?
I don't need all 16bits, I actually only need the lower 10 bits sent to the LEDs. I looked at the byte commands, but I don't know how to extract the binary and redirect it.
here is the code where the two bytes are created and the LED names.
//LEDs
int rdyled = 19;
int led0 = 20;
int led1 = 21;
int led2 = 22;
int led3 = 23;
int led4 = 24;
int led5 = 25;
int led6 = 26;
int led7 = 27;
int led8 = 28;
int led9 = 29;
// LEDS
pinMode(rdyled, OUTPUT);
pinMode(led0, OUTPUT);
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(led4, OUTPUT);
pinMode(led5, OUTPUT);
pinMode(led6, OUTPUT);
pinMode(led7, OUTPUT);
pinMode(led8, OUTPUT);
pinMode(led9, OUTPUT);
if (readbutton) {
highbyte = B00001000; //com2 read RDAC via SDO
lowbyte = B00000000;
digitalWrite(SS, LOW);
SPI.transfer(highbyte);
SPI.transfer(lowbyte);
rdacsethigh = SPI.transfer(0);
rdacsetlow = SPI.transfer(0);
digitalWrite(SS, HIGH);
It would be alot easier to do if you did the sensible thing and made an array. There is nothing scary about them, and they are not hard to use. Make the arduino do all the hard work, so you don't have to.
byte led[10]; //Create an array of 10 bytes.
//Oh, and pin numbers are all <255, so you can use a byte, save memory!!
void setup(){
for (byte i = 0; i < 10; i++){ //Count through setting each of the 10 pins to the correct number.
led[i] = 20 + i; //setup the pins. The first led: led[0] = 20 + 0 = 20; the second led: led[1] = 20 +1 = 21; and so on.
pinMode(led[0], OUTPUT); //Make it an output.
}
...
}
Then to use each led pin number, just replace led0 with led[0], led1 with led[1] etc.
You can then check each bit. If the 0th bit is set, set led[0]. If the 1st bit is set, set led[1].