365 lines
8.6 KiB
C++
365 lines
8.6 KiB
C++
|
//NOTA OJOJOJOJOJOJOJO CUIDADO \u00aa!!!!!!!!!!!
|
||
|
// EL VALOR DE UMBRAL CAMBIA
|
||
|
|
||
|
//SETTUP PARA CADA PLACA
|
||
|
#include "WProgram.h"
|
||
|
void setup();
|
||
|
void loop();
|
||
|
void readSensor();
|
||
|
void Xbeetx(int id, int dat);
|
||
|
void SendXbee();
|
||
|
void readByte();
|
||
|
void sendUp();
|
||
|
void simplificamosRFID();
|
||
|
void actualizamosRFID();
|
||
|
void blink(int thisPin, int howManyTimes);
|
||
|
void playTone();
|
||
|
void pitidoPositivo();
|
||
|
void pitidoParada();
|
||
|
void pitidoNegativo();
|
||
|
int boarNumber = 17; //Board Number change this value for each board
|
||
|
int umbral = 20; //VER NOTA ARRIBA, SI ES ACELEROMETRO UMBRAL = 20 /cajas 17,18,19 y 20 = 40
|
||
|
boolean dosSensores = true; //depende del tipo de placa 1 o 2 sensores analogicos
|
||
|
#define dataRate 9600 // for RFID reader
|
||
|
// TONES ==========================================
|
||
|
// Start by defining the relationship between
|
||
|
// note, period, & frequency.
|
||
|
#define c 3830 // 261 Hz
|
||
|
#define d 3400 // 294 Hz
|
||
|
#define e 3038 // 329 Hz
|
||
|
#define f 2864 // 349 Hz
|
||
|
#define g 2550 // 392 Hz
|
||
|
#define a 2272 // 440 Hz
|
||
|
#define b 2028 // 493 Hz
|
||
|
#define C 1912 // 523 Hz
|
||
|
// Define a special note, 'R', to represent a rest
|
||
|
#define R 0
|
||
|
|
||
|
// SETUP ============================================
|
||
|
// Set up speaker on a PWM pin (digital 9, 10 or 11)
|
||
|
int speakerOut = 9;
|
||
|
|
||
|
// MELODY and TIMING =======================================
|
||
|
// melody[] is an array of notes, accompanied by beats[],
|
||
|
// which sets each note's relative length (higher #, longer note)
|
||
|
int melodyStart[] = { C, b, g, C, R};
|
||
|
int beatsStart[] = { 16, 16, 16, 8, 32};
|
||
|
|
||
|
int melodyStop[] = { b, e};
|
||
|
int beatsStop[] = { 8, 16};
|
||
|
|
||
|
int melodyNo[] = { c };
|
||
|
int beatsNo[] = { 32};
|
||
|
|
||
|
int MAX_COUNT1 = sizeof(melodyStart)/2; // Melody length, for looping.
|
||
|
int MAX_COUNT2 = sizeof(melodyStop)/2; // Melody length, for looping.
|
||
|
int MAX_COUNT3 = sizeof(melodyNo)/2; // Melody length, for looping.
|
||
|
|
||
|
// Set overall tempo
|
||
|
long tempo = 10000;
|
||
|
// Set length of pause between notes
|
||
|
int pause = 1000;
|
||
|
// Loop variable to increase Rest length
|
||
|
int rest_count = 100; //<-BLETCHEROUS HACK; See NOTES
|
||
|
|
||
|
// Initialize core variables
|
||
|
int tone = 0;
|
||
|
int beat = 0;
|
||
|
long duration = 0;
|
||
|
|
||
|
//THE TAG-RFID READER
|
||
|
char code[10]; // var that will hold the bytes-in read from the serialBuffer
|
||
|
char oldCode[10]; //para guardar la ultimo TAG-RFID y poder compara con el nuevo
|
||
|
char miniCode[4]; //var para enviar solo los ultimos 4 digitos del codigo del RFID
|
||
|
int val = 0; //variable para guardar codigos temporal
|
||
|
int valX = 0;
|
||
|
int valY = 0;
|
||
|
int valZ = 0;
|
||
|
int valInfrarred = 0;
|
||
|
int diff = 0;
|
||
|
int bytesread = 0; //variable para guardar el numero de decimales leidos del RFID
|
||
|
|
||
|
long time1;
|
||
|
long timeOld1;
|
||
|
long difTime1 = 0;
|
||
|
long difTimeUmbral1 = 5000; //envia que el sensor esta activo cada 5 segudos
|
||
|
|
||
|
////////////////////////////////////////////////////////////////////
|
||
|
|
||
|
int lowbat = 13; // Pin de bateria baja
|
||
|
int nivel = 3; //Sensor de nivel de bateria
|
||
|
int xbee = 4; //Pin de activacion del xbee
|
||
|
int buzzer = 9;
|
||
|
|
||
|
int i=0;
|
||
|
int rfid_ok=0;
|
||
|
int id=0;
|
||
|
int ini=20;
|
||
|
int env=3;
|
||
|
|
||
|
void setup()
|
||
|
{
|
||
|
|
||
|
digitalWrite(xbee, HIGH); //Habilito XBEE
|
||
|
//encendemos el led
|
||
|
pinMode(speakerOut, OUTPUT);
|
||
|
|
||
|
Serial.begin(9600);
|
||
|
pinMode(lowbat, OUTPUT);
|
||
|
pinMode(xbee, OUTPUT);
|
||
|
|
||
|
for (i = 5; i < 9; i++) pinMode(i, INPUT);
|
||
|
for (i = 5; i < 9; i++)
|
||
|
{
|
||
|
if (digitalRead(i))
|
||
|
{
|
||
|
if (i==5) val=1;
|
||
|
else if (i==6) val=2;
|
||
|
else if (i==7) val=4;
|
||
|
else if (i==8) val=8;
|
||
|
id=id+val;
|
||
|
}
|
||
|
}
|
||
|
id = 21;//en n futuro cogerlo de los pines
|
||
|
|
||
|
blink(13,3); // blink an LED:
|
||
|
|
||
|
//pitido positivo
|
||
|
pitidoPositivo();
|
||
|
|
||
|
//inicializamos miniCode a 0
|
||
|
for (int i = 0; i<3; i++) {
|
||
|
miniCode[i]='0';
|
||
|
}
|
||
|
miniCode[3]=0;
|
||
|
|
||
|
//inicializamos code a 0
|
||
|
for (int i = 0; i<9; i++) {
|
||
|
code[i]='0';
|
||
|
oldCode[i] = '0';
|
||
|
}
|
||
|
code[9]=0;
|
||
|
oldCode[9]=0;
|
||
|
|
||
|
}
|
||
|
|
||
|
|
||
|
void loop()
|
||
|
{
|
||
|
readByte();
|
||
|
|
||
|
readSensor();
|
||
|
|
||
|
sendUp();
|
||
|
|
||
|
delay(100);
|
||
|
|
||
|
}
|
||
|
|
||
|
void readSensor(){
|
||
|
|
||
|
valX=analogRead(0); //tres ejes acelerometro
|
||
|
valY=analogRead(1); //tres ejes acelerometro
|
||
|
valZ=analogRead(2); //tres ejes acelerometro
|
||
|
valInfrarred=analogRead(4); //infrarrojo
|
||
|
|
||
|
//difX = abs(valXold - valX);
|
||
|
//difY = abs(valYold - valY);
|
||
|
//difZ = abs(valZold - valZ);
|
||
|
//difInfrarred = abs(valInfrarrdOld - valInfrarred);
|
||
|
|
||
|
SendXbee();
|
||
|
}
|
||
|
|
||
|
void Xbeetx(int id, int dat){
|
||
|
Serial.print((id<<4)||(dat>>4),BYTE);
|
||
|
Serial.print((id<<4)||(0x0F&dat),BYTE);
|
||
|
}
|
||
|
|
||
|
void SendXbee(){
|
||
|
Serial.print(id,DEC); //id
|
||
|
delay(env);
|
||
|
Serial.print(":");
|
||
|
delay(env);
|
||
|
Serial.print(miniCode);
|
||
|
delay(env);
|
||
|
Serial.print(":");
|
||
|
delay(env);
|
||
|
|
||
|
Serial.print(map(valX, 0, 675, 0, 255),DEC);
|
||
|
delay(env);
|
||
|
Serial.print(":");
|
||
|
delay(env);
|
||
|
Serial.print(map(valY, 0, 675, 0, 255),DEC);
|
||
|
delay(env);
|
||
|
Serial.print(":");
|
||
|
delay(env);
|
||
|
Serial.print(map(valZ, 0, 675, 0, 255),DEC);
|
||
|
delay(env);
|
||
|
Serial.print(":");
|
||
|
delay(env);
|
||
|
|
||
|
Serial.println(map(valInfrarred, 0, 675, 0, 255),DEC);
|
||
|
delay(env);
|
||
|
}
|
||
|
|
||
|
void readByte(){
|
||
|
bytesread=0;
|
||
|
|
||
|
while (Serial.available()>0)
|
||
|
{
|
||
|
val=Serial.read(); //RFID
|
||
|
code[bytesread] = val;
|
||
|
bytesread++;
|
||
|
|
||
|
if(bytesread == 10){
|
||
|
//comparamos si el nuevo codigo es igual al anterior (se lee el mismo tag dos veces)
|
||
|
if (memcmp(code,oldCode, 10) == 0){ //si son iguales
|
||
|
pitidoNegativo();
|
||
|
}else{ //SI SON DISTINTOS (el nuevo codigo es diferente)
|
||
|
actualizamosRFID();
|
||
|
simplificamosRFID();
|
||
|
pitidoPositivo();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void sendUp(){
|
||
|
|
||
|
time1 = millis(); //record the actual time
|
||
|
difTime1 = abs(time1 - timeOld1); //calculate de diference time from last reading
|
||
|
|
||
|
if (difTime1 > difTimeUmbral1){
|
||
|
val=analogRead(nivel); //bateria
|
||
|
if (val<=614) digitalWrite(lowbat, LOW); //Umbral de bateria baja 3v, que equivale a 614
|
||
|
else digitalWrite(lowbat, HIGH);
|
||
|
|
||
|
Serial.print(boarNumber, DEC);
|
||
|
delay(env);
|
||
|
Serial.print(":c:");
|
||
|
delay(env);
|
||
|
Serial.print(map(val, 0, 860, 0, 810),DEC); //El maximo de tension de la bateria son 4'2v, que equivale a 860
|
||
|
delay(env);
|
||
|
timeOld1 = time1;
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
void simplificamosRFID() { //simplificamos el codigo del RFID a los 4 ultimos valores
|
||
|
|
||
|
for (int i = 0; i<3; i++) { //guardamos el valor corto del RFID
|
||
|
miniCode[i]=code[7+i];
|
||
|
}
|
||
|
miniCode[3]=0;
|
||
|
}
|
||
|
|
||
|
void actualizamosRFID() {
|
||
|
|
||
|
for (int i = 0; i < 10; i++){ //guardamos el valor del RFID en memoria
|
||
|
oldCode[i] = code[i];
|
||
|
}
|
||
|
|
||
|
for (int i = 0; i<3; i++) { //guardamos el valor corto del RFID
|
||
|
miniCode[i]=oldCode[7+i];
|
||
|
}
|
||
|
miniCode[3]=0;
|
||
|
}
|
||
|
|
||
|
|
||
|
/*
|
||
|
blink method blinks an LED a given number of times
|
||
|
*/
|
||
|
void blink(int thisPin, int howManyTimes) {
|
||
|
// set the LED's pin as an output:
|
||
|
pinMode(thisPin, OUTPUT);
|
||
|
// blink the LED:
|
||
|
for (int t = 0; t < howManyTimes; t++) {
|
||
|
digitalWrite(thisPin, HIGH);
|
||
|
delay(200);
|
||
|
digitalWrite(thisPin, LOW);
|
||
|
delay(200);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// PLAY TONE ==============================================
|
||
|
// Pulse the speaker to play a tone for a particular duration
|
||
|
void playTone() {
|
||
|
long elapsed_time = 0;
|
||
|
if (tone > 0) { // if this isn't a Rest beat, while the tone has
|
||
|
// played less long than 'duration', pulse speaker HIGH and LOW
|
||
|
while (elapsed_time < duration) {
|
||
|
|
||
|
digitalWrite(speakerOut,HIGH);
|
||
|
delayMicroseconds(tone / 2);
|
||
|
|
||
|
// DOWN
|
||
|
digitalWrite(speakerOut, LOW);
|
||
|
delayMicroseconds(tone / 2);
|
||
|
|
||
|
// Keep track of how long we pulsed
|
||
|
elapsed_time += (tone);
|
||
|
}
|
||
|
}
|
||
|
else { // Rest beat; loop times delay
|
||
|
for (int j = 0; j < rest_count; j++) { // See NOTE on rest_count
|
||
|
delayMicroseconds(duration);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
void pitidoPositivo() {
|
||
|
|
||
|
for (int i=0; i<MAX_COUNT1; i++) {
|
||
|
tone = melodyStart[i];
|
||
|
beat = beatsStart[i];
|
||
|
|
||
|
duration = beat * tempo; // Set up timing
|
||
|
playTone();
|
||
|
// A pause between notes...
|
||
|
delayMicroseconds(pause);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void pitidoParada(){
|
||
|
|
||
|
for (int i=0; i<MAX_COUNT2; i++) {
|
||
|
tone = melodyStop[i];
|
||
|
beat = beatsStop[i];
|
||
|
|
||
|
duration = beat * tempo; // Set up timing
|
||
|
playTone();
|
||
|
// A pause between notes...
|
||
|
delayMicroseconds(pause);
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
void pitidoNegativo() {
|
||
|
|
||
|
for (int i=0; i<MAX_COUNT3; i++) {
|
||
|
tone = melodyNo[i];
|
||
|
beat = beatsNo[i];
|
||
|
|
||
|
duration = beat * tempo; // Set up timing
|
||
|
playTone();
|
||
|
// A pause between notes...
|
||
|
delayMicroseconds(pause);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
int main(void)
|
||
|
{
|
||
|
init();
|
||
|
|
||
|
setup();
|
||
|
|
||
|
for (;;)
|
||
|
loop();
|
||
|
|
||
|
return 0;
|
||
|
}
|
||
|
|