0
Offline
Newbie
Karma: 0
Posts: 14
Arduino rocks
|
 |
« Reply #2 on: March 10, 2009, 12:02:13 pm » |
First I had this code working using Arduino and Xbee Shield with Xbee, know I'm trying to make the code work with LilyPad Arduino and LilyPad Xbee, but I don't know if I need to do anything that I haven't done.
I'm working in Arduino 11 because de FrequencyTimer library doesn't work with recent versions of Arduino.
One Arduino has this code:
#include <FrequencyTimer2.h>
#define A { \ { 1, 1, 1, 1}, \ { 1, 1, 1, 1}, \ { 1, 1, 1, 1}, \ { 1, 1, 1, 1}, \ }
#define B { \ { 0, 0, 0, 0}, \ { 0, 0, 0, 0}, \ { 0, 0, 0, 0}, \ { 0, 0, 0, 0}, \ }
#define C { \ { 1, 0, 0, 0}, \ { 0, 1, 0, 0}, \ { 0, 0, 1, 0}, \ { 0, 0, 0, 1}, \ }
#define D { \ { 1, 0, 0, 1}, \ { 0, 1, 1, 0}, \ { 0, 1, 1, 0}, \ {1, 0, 0, 1}, \ }
#define E { \ { 0, 0, 1, 0}, \ { 0, 1, 0, 0}, \ { 0, 1, 0, 0}, \ { 0, 0, 1, 0}, \ }
byte col = 0; byte leds[4][4];
// pin[xx] on led matrix connected to nn on Arduino (-1 is dummy to make array start at pos 1) int pins[9]= {-1, 3, 4, 6, 7, 11, 12, 8, 9};
// col[xx] of leds = pin yy on led matrix int cols[4] = {pins[5], pins[6], pins[7], pins[8]};
// row[xx] of leds = pin yy on led matrix int rows[4] = {pins[4], pins[3], pins[2], pins[1]};
byte propria[3][4][4]={ A,C,B };
byte anima72[3][4][4]={ D,E,B };
int pattern = 0; int frame = 0;
//
int incomingByte = 0; // varivel para a serial data
int contador=0;
int relo=0;
void setup() { Serial.begin(9600); for (int i = 1; i <= 8; i++) { pinMode(pins, OUTPUT); }
// set up cols and rows for (int i = 1; i <= 4; i++) { digitalWrite(cols[i - 1], LOW); }
for (int i = 1; i <= 4; i++) { digitalWrite(rows[i - 1], LOW); }
clearLeds();
// Turn off toggling of pin 11 FrequencyTimer2::disable(); // Set refresh rate (interrupt timeout period) FrequencyTimer2::setPeriod(2000); // Set interrupt routine to be called FrequencyTimer2::setOnOverflow(display);
}
void loop() { //LIGAÌO if (Serial.available() > 0) { //caso exista info "em linha de espera" ler incomingByte = Serial.read(); delay(100); Serial.print ('L'); Serial.flush(); }
else{ Serial.print ('L');//modo dormente }
//ACÌO
if (incomingByte == 72 && contador<4){ Serial.print ('L'); setPattern(anima72[frame]); frame++; if(frame >= 3){ frame=0; contador++; }
} else{ setPattern(propria[frame]); frame++; if(frame >= 3){ frame=0; } relo++; if (relo>10){ relo=0; contador=0; } } delay(2000); }
//
void clearLeds() { // Clear display array for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { leds[j] = 0; } } }
void setPattern(byte p[4][4]) { for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { leds[j] = p[j]; } } }
// Interrupt routine void display() { digitalWrite(cols[col], LOW); // Turn whole previous column off col++; if (col == 4) { col = 0; } for (int row = 0; row < 4; row++) { if (leds[col][3 - row] == 1) { digitalWrite(rows[row], LOW); // Turn on this led } else { digitalWrite(rows[row], HIGH); // Turn off this led } } digitalWrite(cols[col], HIGH); // Turn whole column on at once (for equal lighting times) delayMicroseconds(900); // Delay so that on times are longer than off time = brighter leds }
And the other has this code:
#include <FrequencyTimer2.h>
#define A { \ { 1, 1, 1, 1}, \ { 1, 1, 1, 1}, \ { 1, 1, 1, 1}, \ { 1, 1, 1, 1}, \ }
#define B { \ { 0, 0, 0, 0}, \ { 0, 0, 0, 0}, \ { 0, 0, 0, 0}, \ { 0, 0, 0, 0}, \ }
#define C { \ { 1, 0, 0, 0}, \ { 0, 1, 0, 0}, \ { 0, 0, 1, 0}, \ { 0, 0, 0, 1}, \ }
#define D { \ { 1, 0, 0, 1}, \ { 0, 1, 1, 0}, \ { 0, 1, 1, 0}, \ {1, 0, 0, 1}, \ }
#define E { \ { 0, 0, 1, 0}, \ { 0, 1, 0, 0}, \ { 0, 1, 0, 0}, \ { 0, 0, 1, 0}, \ }
byte col = 0; byte leds[4][4];
// pin[xx] on led matrix connected to nn on Arduino (-1 is dummy to make array start at pos 1) int pins[9]= {-1, 11, 3, 6, 8, 9, 7, 4, 12};
// col[xx] of leds = pin yy on led matrix int cols[4] = {pins[8], pins[7], pins[6], pins[5]};
// row[xx] of leds = pin yy on led matrix int rows[4] = {pins[4], pins[3], pins[2], pins[1]};
byte propria[3][4][4]={ D,E,B };
byte anima76[3][4][4]={ A,C,B };
int pattern = 0; int frame = 0;
//
int incomingByte = 0; // varivel para a serial data
int contador=0;
int relo=0;
void setup() { Serial.begin(9600); for (int i = 1; i <= 8; i++) { pinMode(pins, OUTPUT); }
// set up cols and rows for (int i = 1; i <= 4; i++) { digitalWrite(cols[i - 1], LOW); }
for (int i = 1; i <= 4; i++) { digitalWrite(rows[i - 1], LOW); }
clearLeds();
// Turn off toggling of pin 11 FrequencyTimer2::disable(); // Set refresh rate (interrupt timeout period) FrequencyTimer2::setPeriod(2000); // Set interrupt routine to be called FrequencyTimer2::setOnOverflow(display); }
void loop() { //LIGAÌO if (Serial.available() > 0) { //caso exista info "em linha de espera" ler incomingByte = Serial.read(); delay(100); Serial.print ('H'); Serial.flush(); }
else{ Serial.print ('H');//modo dormente }
//ACÌO
if (incomingByte == 76 && contador<4){ Serial.print ('H'); setPattern(anima76[frame]); frame++; if(frame >= 3){ frame=0; contador++; }
} else{ setPattern(propria[frame]); frame++; if(frame >= 3){ frame=0; } relo++; if (relo>5){ relo=0; contador=0; } } delay(2000); }
//
void clearLeds() { // Clear display array for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { leds[j] = 0; } } }
void setPattern(byte p[4][4]) { for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { leds[j] = p[j]; } } }
// Interrupt routine void display() { digitalWrite(cols[col], LOW); // Turn whole previous column off col++; if (col == 4) { col = 0; } for (int row = 0; row < 4; row++) { if (leds[col][3 - row] == 1) { digitalWrite(rows[row], LOW); // Turn on this led } else { digitalWrite(rows[row], HIGH); // Turn off this led } } digitalWrite(cols[col], HIGH); // Turn whole column on at once (for equal lighting times) delayMicroseconds(900); // Delay so that on times are longer than off time = brighter leds }
The objective is that the two LED matrix have different animations and when they are near each other, the animations exchange from one matrix to the other.
|