Codi 3

Aquesta és la tercera versió de firmware que hem fet.Feina de Francesc Galbany

// Jesús Arbués // Basat en ArduinoBoard de Rai and Mike McKenzie
//============================================================================

//#include <Wire.h>
char rfid[12];
int pos=0;
void ScratchBoardSensorReport(int sensor, int value)
{
Serial.write( B10000000
| ((sensor & B1111)<<3)
| ((value>>7) & B111));
Serial.write( value & B1111111);
}

void setup()
{
Serial.begin(9600);
pinMode(4, INPUT);
}

int map3a1(char a, char b, char c){
int val = 255*hexVal(a) + hexVal(b)*16 + hexVal(c);
return map(val, 0, 4095, 0, 1023); // Passem de 4k a 1k perdent precisi´
}

int hexVal(char c){
char ret;
if((c >= '0') && (c <= '9')){
ret = c - '0';
}else if((c >= 'A') && (c <= 'F')){
ret = c - 'A' + 10;
}
return ret;
}

void loop()
{
//Si hi ha dades RFID...
if(Serial.available())
{
while(Serial.available()){
delay(10);
char c = (char)Serial.read();
if( ((int)c >= 48) && ((int)c <=70)){
rfid[pos]=c;
pos++;
}
if(pos == 12){
break;
}
}
//Cal comprovar que i==12
if(pos == 12){     
pos = 0;
Serial.end();
Serial.begin(38400);
ScratchBoardSensorReport(0, map3a1(rfid[0],rfid[1],rfid[2]));
ScratchBoardSensorReport(1, map3a1(rfid[3],rfid[4],rfid[5]));
ScratchBoardSensorReport(2, map3a1(rfid[6],rfid[7],rfid[8]));
ScratchBoardSensorReport(3, map3a1(rfid[9],rfid[10],rfid[11]));
ScratchBoardSensorReport(4, 0);
ScratchBoardSensorReport(5, 0);
ScratchBoardSensorReport(6, 0);
ScratchBoardSensorReport(7, digitalRead(4)?1023:0);
tone(11,4000,200); 
delay(30);
Serial.end();
Serial.begin(9600);
}/*else{
/ScratchBoardSensorReport(0, 0);
ScratchBoardSensorReport(1, 0);
ScratchBoardSensorReport(2, 0);
ScratchBoardSensorReport(3, 0);
ScratchBoardSensorReport(4, 0);
ScratchBoardSensorReport(5, 0);
ScratchBoardSensorReport(6, 0);
ScratchBoardSensorReport(7, digitalRead(4)?1023:0);
}*/
}
// Let Scratch catch up with us
delay(30);
}

Obra colocada bajo licencia Creative Commons Attribution Non-commercial Share Alike 3.0 License