#include <VirtualWire.h>
#define LEDRED A0
#define LEDGREEN A1
#define LEDBLUE A2
#define LIGHTSENSOR A3
#define TX 10
#define buttonA 5
#define buttonB 6
//variables TX - RX
char separador [2] = {"a"};
char datarraycharred[(sizeof(int)*4+1)];
char datarraychargreen[(sizeof(int)*4+1)];
char datarraycharblue[(sizeof(int)*4+1)];
char datamodo[(sizeof(int)*4+1)];
char datatotal[(sizeof(int)*4+3)*3];
int index=0,ired=0,igreen=0,iblue=0;
int datared=0,datagreen=0,datablue=0 ; //cm/seg
int modo=0;
//for led copycat
int ledArray[] = {2,3,4};
int red = 0;
int green = 0;
int blue = 0;
int avgRead;
float colourArray[] = {0,0,0};
float whiteArray[] = {0,0,0};
float blackArray[] = {0,0,0};
void setup() {
pinMode(LEDRED,OUTPUT);
pinMode(LEDGREEN,OUTPUT);
pinMode(LEDBLUE,OUTPUT);
pinMode(LIGHTSENSOR,INPUT);
pinMode(buttonA,INPUT);
pinMode(buttonB,INPUT);
pinMode(TX,OUTPUT);
//ledarray copycat
pinMode(2,OUTPUT);
pinMode(3,OUTPUT);
pinMode(4,OUTPUT);
digitalWrite(2,HIGH);
digitalWrite(3,HIGH);
digitalWrite(4,HIGH);
Serial.begin(9600);
pinMode(TX, INPUT);
pinMode(TX, OUTPUT);
vw_set_tx_pin(TX); // Configura el pin como TX
vw_set_ptt_inverted(true); // Requerido para DR3100
vw_setup(2000); // Bits por segundo
}
void loop() {
if(digitalRead(buttonA)==0&&digitalRead(buttonB)==0)
modochill();
if(digitalRead(buttonA)==1&&digitalRead(buttonB)==0)
modoanalogico();
if(digitalRead(buttonB)==1)
modoLEDcopycat();
//construccion del array de datos a enviar
strcpy (datatotal,datamodo);
strcat (datatotal,separador);
strcat (datatotal,datarraycharred);
strcat (datatotal,separador);
strcat (datatotal,datarraychargreen);
strcat (datatotal,separador);
strcat (datatotal,datarraycharblue);
strcat (datatotal,separador);
//DEBUG
//Serial.print(datatotal);
Serial.print(" ");
Serial.println(datatotal);
//ENVIO RX
digitalWrite(13, HIGH); // Turn on a light to show transmitting
vw_send((uint8_t *)datatotal, strlen(datatotal));
vw_wait_tx(); // Wait until the whole message is gone
delay(50);
digitalWrite(13, LOW); // Turn off a light after transmission
}
//mode chillout defined at receiver, just freeze the dataflow and send modo=0
void modochill(){
modo=0;
itoa(modo,datamodo,10);//10--> base decimal
}
//mode and data rgb analog
void modoanalogico(){
modo=1;
itoa(modo,datamodo,10);//10--> base decimal
//analog read and int to char conversion
datared=analogRead(LEDRED);
itoa(datared,datarraycharred,10);//10--> base decimal
datagreen=analogRead(LEDGREEN);
itoa(datagreen,datarraychargreen,10);
datablue=analogRead(LEDBLUE);
itoa(datablue,datarraycharblue,10);
}
void modoLEDcopycat()
{
if(digitalRead(buttonA)==1&&digitalRead(buttonB)==1){setBalance();}
for(int i = 0;i<=2;i++){
digitalWrite(ledArray[i],LOW); //turn or the LED, red, green or blue depending which iteration
delay(100); //delay to allow CdS to stabalize, they are slow
getReading(5); //take a reading however many times
colourArray[i] = avgRead; //set the current colour in the array to the average reading
float greyDiff = whiteArray[i] - blackArray[i]; //the highest possible return minus the lowest returns the area for values in between
colourArray[i] = (colourArray[i] - blackArray[i])/(greyDiff)*255; //the reading returned minus the lowest value divided by the possible range multiplied by 255 will give us a value roughly between 0-255 representing the value for the current reflectivity(for the colour it is exposed to) of what is being scanned
digitalWrite(ledArray[i],HIGH); //turn off the current LED
delay(100);
}
//rgb con catodo comun , dar la vuelta al valor de intensidad con map
analogWrite(LEDRED,map(colourArray[0],0,255,255,0));
analogWrite(LEDGREEN,map(colourArray[1],0,255,255,0));
analogWrite(LEDBLUE,map(colourArray[2],0,255,255,0));
}
//asymetric blink for humans to notice
void piiipipi(){
digitalWrite(10,LOW);
digitalWrite(11,LOW);
digitalWrite(12,LOW);
delay(1000);
digitalWrite(10,HIGH);
digitalWrite(11,HIGH);
digitalWrite(12,HIGH);
delay(500);
digitalWrite(10,LOW);
digitalWrite(11,LOW);
digitalWrite(12,LOW);
delay(500);
digitalWrite(10,HIGH);
digitalWrite(11,HIGH);
digitalWrite(12,HIGH);
delay(500);
digitalWrite(10,LOW);
digitalWrite(11,LOW);
digitalWrite(12,LOW);
delay(500);
digitalWrite(10,HIGH);
digitalWrite(11,HIGH);
digitalWrite(12,HIGH);
delay(1000);
}
//when two buttons pressed, calibrate the rgb readings with white and black objects
void setBalance(){
piiipipi() ;
for(int i = 0;i<=2;i++){
digitalWrite(ledArray[i],LOW);
delay(100);
getReading(5); //number is the number of scans to take for average, this whole function is redundant, one reading works just as well.
whiteArray[i] = avgRead;
digitalWrite(ledArray[i],HIGH);
delay(100);
}
//done scanning white, now it will pulse blue to tell you that it is time for the black (or grey) sample.
//set black balance
piiipipi() ;
//go ahead and scan, sets the colour values for red, green, and blue when exposed to black
for(int i = 0;i<=2;i++){
digitalWrite(ledArray[i],LOW);
delay(100);
getReading(5);
blackArray[i] = avgRead;
//blackArray[i] = analogRead(2);
digitalWrite(ledArray[i],HIGH);
delay(100);
}
//delay another 5 seconds to allow the human to catch up to what is going on
delay(5000);
}
//get the colour!
void getReading(int times){
int reading;
int tally=0;
//take the reading however many times was requested and add them up
for(int i = 0;i < times;i++){
reading = analogRead(LIGHTSENSOR);
tally = reading + tally;
delay(10);
}
}
its reads an specific datagram with this concrete sintaxis
MaRRRRaGGGGaBBBB;
M->mode , 0 for waving colours, 1 for analog transmitted from the TX unit
RRRR-> RED VALUE
GGGG->GREEN VALUE
BBBB->BLUE VALUE
a->for separate the data