Pages: 1 [2]   Go Down
Author Topic: 8 x RGB LED Array  (Read 2735 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 0
Posts: 60
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Im stuggling a little bit with the maskbit , as i am unsure of the purpose this is serving,

The number i want to send to the array is in decimal, and i want the array to conver it to a binary representation,

is the byte command used for this?

thanks

Rich
Logged

Global Moderator
Boston area, metrowest
Online Online
Brattain Member
*****
Karma: 547
Posts: 27364
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

You are sending decimal, while it will be manipulated as  binary.

The maskBit makes sure that only 1 bit at a time is being tested for the digitalWrite.

Say you send in 64.
64 in binary = 01000000
so 64 & B00000001 = 0
64 & B00000010 = 0
64 & B00000100 = 0
64 & B00001000 = 0
64 & B00010000 = 0
64 & B00100000 = 0
64 & B01000000 = >0 so turn on an LED
64 & B10000000 = 0

You can prove this to yourself using serial.print to print out the results.
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Offline Offline
Jr. Member
**
Karma: 0
Posts: 60
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I tried running your code,

i had to move a few colons and add a few braces to get it to compile, but i dont seem to get any output,

Just for now i have replaced the CANbyte with a decimal number , i assume this should not be an issue,

code posted below

int x = 0;
int pinsArray[] = {2,3,4,5,6,7,8,9};  // could be whatever group you select
int maskBit = 1;
void setup()
{
pinMode(pinsArray[8],OUTPUT);
}
void loop ()
{

maskBit = 1;                                       // start with B00000001

for (x=0; x<8; x=x+1){
 
if ((126 & maskBit) >0){                 // makes all bits but 1 low: 0000000C, next pass 000000C0, etc

digitalWrite (pinsArray
  • ,  HIGH);
}

else {
 
digitalWrite (pinsArray
  • , LOW);
}
}


maskBit = maskBit<1;   // next bit - B00000010, B00000100, B00001000, etc up to B10000000
}  // next x
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 60
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

OK, so i have managed to get the code to start running, and modifying the decimal number where the CANbyte value was will give me the desired LED patter,

The isue being, that is only flashes up for a second and it doesnt seem to be running through the loops correctly,

I have had a look over it and i cant spot anything obvious,

I had to add a "<" to the Maskbit line at the bottom as i assume it was supposed to be a Bitshift command? is thios correct?

Appreciating all the help folks

RIch

Code:

int x = 0;
int pinsArray[] = {2,3,4,5,6,7,8,9};  // could be whatever group you select
int maskBit = 1;
int i = 0;


void setup()

{
  for(i=0; i<8; i++)
pinMode(pinsArray[i],OUTPUT);
pinMode(10,OUTPUT);


maskBit == 1;

}

void loop ()
{
digitalWrite(10,HIGH);
                                   // start with B00000001

for (x=0; x<8; x=x+1){
 
if ((255 & maskBit) >0){                 // makes all bits but 1 low: 0000000C, next pass 000000C0, etc

digitalWrite (pinsArray[x],  HIGH);
}

else {
 
digitalWrite (pinsArray[x], LOW);
}

}

maskBit = maskBit<<1;   // next bit - B00000010, B00000100, B00001000, etc up to B10000000

}// next x

Logged

Norfolk UK
Offline Offline
Faraday Member
**
Karma: 70
Posts: 2587
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I still cannot grasp exactly what your trying to do with the value you get but if your trying to turn on/off the LED's in the binary pattern corresponding to the value receive then you can also do it this way
Code:
const byte target = B11110001;

void setup(){
    Serial.begin(9600);
}

void loop(){
    for (byte x=0;x<8;x++){
        if (((target>>x) & 1)==1){
            Serial.print("1");
        }
        else {
            Serial.print("0");
        }
    }
             Serial.println();

    for (byte x=0;x<8;x++){
        if (((target<<x) & 128)==128){
            Serial.print("1");
        }
        else {
            Serial.print("0");
        }
    }
    while(true){
    }
}
I have included 2x versions that rotate the bits out to either the left or the right.
Logged


Offline Offline
Jr. Member
**
Karma: 0
Posts: 60
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Sorted, a few tweaks and i am there, i had to replace the == operator at the top of the loop with a single = operator as it was running a comparison and getting stuck,

will follow up with some updates when i have meged the PWM colour code and CAN recieve and transmit code into the main code.

Thanks again

Rich
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 60
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ah great thanks Riva, i will go try that code out now, the swapping of MSB and LSB looks very usefull
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 60
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Seems to be working great,

Thanks for all of the help Crossroads,

3 questions

1, How can i set the CAN code up to look at only a single base CAN id, at the moment, regardless of CAN id, it simple recieves any message and uses the data in the melevant bits to run the lights,

2, How can i clear the message buffers at the beginning of each time through the loop? at the moment, once the CAN message is removed from the bus, the led's remain in the last message state,

3, Any way of getting the code to run faster, as i believe i can display multiple colours at a time in software as the CAN u[pdate rate is fast enough for visual deception,

Code:


#include "mcp_can.h"
#include <SPI.h>
#include <stdio.h>
#define INT8U unsigned char

INT8U Flag_Recv = 0;
INT8U len = 0;
INT8U buf[8];
char str[20];

////////////////////////////////////////// CAN Setup Above

int x = 0;
int pinsArray[] = {4,7,8,9,14,15,16,17};  // could be whatever group you select
int maskBit = 1;
int i = 0;
///////////////////////////////////////////////////// LED Setup Above

void setup()

{
  for(i=0; i<8; i++)
pinMode(pinsArray[i],OUTPUT);
pinMode(3,OUTPUT);   // Red PWM
pinMode(5,OUTPUT);   // Green PWM
pinMode(6,OUTPUT);   // Blue PWM

//////////////////////////////////////////////////////////////// LED Outputs Above

 CAN.begin(CAN_500KBPS);                   // init can bus : baudrate = 500k
  attachInterrupt(0, MCP2515_ISR, FALLING); // start interrupt
  Serial.begin(115200);

////////////////////////////////////////////////////////CAN Begin Above
}


void MCP2515_ISR()
{
     Flag_Recv = 1;
}
/////////////////////////////////////////////CAN Calling code

void loop ()
{
 


 if(Flag_Recv)                   // check if get data
    {
      Flag_Recv = 0;                // clear flag
 
      CAN.readMsgBuf(&len, buf);    // read data,  len: data length, buf: data buf
      Serial.println("CAN_BUS GET DATA!");
      Serial.print("data len = ");Serial.println(len);
      for(int i = 0; i<len; i++)    // print the data
      {
        Serial.print(buf[i]);Serial.print("\t");
       
      }
      Serial.println();
    }
////////////////////////////////////////////////////////////////////////////////Recieve CAN Message
analogWrite(3,buf[1]);
analogWrite(5,buf[2]);
analogWrite(6,buf[3]);
////////////////////////////////////////////////////////Set RGB Colour With CAN Message


  maskBit = 1;                                  // start with B00000001

for (x=0; x<8; x=x+1){
 
if ((buf[0] & maskBit) >0){                 // makes all bits but 1 low: 0000000C, next pass 000000C0, etc

digitalWrite (pinsArray[x],  HIGH);
}

else {
 
digitalWrite (pinsArray[x], LOW);
}



maskBit = maskBit << 1;   // next bit - B00000010, B00000100, B00001000, etc up to B10000000
}

////////////////////////////////////////////////////////////////////////////////////////////////Write LED Pattern
}// next x



Logged

Global Moderator
Boston area, metrowest
Online Online
Brattain Member
*****
Karma: 547
Posts: 27364
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

1. I don't anything about the message you are receiving.  Do you  have some definition of the message?

2.  buf[1] = 0;
buf[2] = 0;
buf3 = 0;
??

3.  Comment out the serial print's.
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Offline Offline
Jr. Member
**
Karma: 0
Posts: 60
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

1, the CAN.ReadMsgBuf command in the code above will read the CAN messages that are being recieved into the message buffer.

Currently, i am using the contents of the CAN message frame   eg, buf[1] being the first part, for the sequence of the LED's,

So weather the Message being sent had a base ID of 0x500 or ox600 , or any other base ID, the LED's would still do whatever the frame of data held.

i want to be able to "take notice" of only a single given CAN id to get my data from,     

2, i tried doing this at the beginning of each loop, it seems to just cause the LED's to flash very quickly and at hardly any brightness, maybe i need to place it elsewehere in the loop?

3, thanks, i should have noticed that


Thanks

Rich
Logged

Global Moderator
Boston area, metrowest
Online Online
Brattain Member
*****
Karma: 547
Posts: 27364
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Wrap this part with ID checking code:
Code:
if (buf[0]== intended_ID){
////////////////////////////////////////////////////////Set RGB Colour With CAN Message


  maskBit = 1;                                  // start with B00000001

for (x=0; x<8; x=x+1){
 
if ((buf[0] & maskBit) >0){                 // makes all bits but 1 low: 0000000C, next pass 000000C0, etc

digitalWrite (pinsArray[x],  HIGH);
}

else {
 
digitalWrite (pinsArray[x], LOW);
}



maskBit = maskBit << 1;   // next bit - B00000010, B00000100, B00001000, etc up to B10000000
}

////////////////////////////////////////////////////////////////////////////////////////////////Write LED Pattern
}// next x

} // end ID check
else{
// add code to loop thru the pins with digitalWrite(pin, LOW)
}
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Offline Offline
Jr. Member
**
Karma: 0
Posts: 60
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks, im just waitinf for the correct command from the cuy that wrote the library to try it out, but looks great,

Any idea why when the CAN message is removed, or i disconnect the board from the CAN bus, it seems to display the last recieved sequence on the lights,

In principal, it should be able to show more than one colour at a time as the CAN speed is high enough, and writing some simple code of digital write high's and low's allows me to do it,

I think i may have something in there that is taking to long, or it isnt clearing the messge buffer fast enough,

What do you think?

Cheers

Rich
Logged

Pages: 1 [2]   Go Up
Jump to: