jueves, 8 de mayo de 2014

Lab 5.2 A Sunset Sensor

Realizado por Vanessa Alcaraz & Sergi Martín.

Este laboratorio mantiene la idea de conectar dos Xbees inalámbricamente, pero esta vez, uno de los Xbees estará configurado como un coordinador API (conectado al Arduino UNO), y el otro como un router AT remoto.

Nuestro proyecto consiste en tener un sensor de luz conectado al Xbee router, y éste Xbee le pasa información sobre la intensidad de luz al otro Xbee que esta conectado al Arduino UNO.

El Xbee router también tiene conectado un LED rojo, que depende de la información sobre la intensidad de luz que reciba el Arduino, éste le enviará un estado remoto al Xbee router de si debe apagar o encender el LED.
En nuestro caso, si se recibe mucha intensidad luz el LED rojo del Xbee remoto se enciende, en caso contrario, se apaga.

Además tenemos conectado un LED azul y un buzzer al Arduino UNO que dependiendo de la intensidad de luz que reciba el LED aumenta o disminuye la luz, y el buzzer aumenta o disminuye el tono.

El material utilizado para este laboratorio:



El esquema del circuito:




El código:

int analogValue = 0;
int LED = 10;
int remoteIndicator = false;
int lastRemoteIndicator = false;
int bell = 5;

void setup() {
        pinMode(LED, OUTPUT);
        pinMode(bell, OUTPUT);
        Serial.begin(9600);
}

void loop() {
        if (Serial.available() >= 23) {
                  if (Serial.read() == 0x7E) {
                            for (int i = 0; i<20; i++) {
                                   byte discard = Serial.read();
                            }
                            int analogHigh = Serial.read();
                            int analogLow = Serial.read();
                            analogValue = analogLow + (analogHigh * 256);
                            while(analogValue > 0 && analogValue <= 1023){
                                    if (analogValue > 0 && analogValue <= 750) remoteIndicator = true;
                                    else remoteIndicator = false;
                                    analogWrite(LED, analogValue/4);
                                    tone(bell, analogValue/4, 150);
                                    if (remoteIndicator != lastRemoteIndicator) {
                                             if (remoteIndicator==false) setRemoteState(0x4);
                                             if (remoteIndicator==true) setRemoteState(0x5);
                                             lastRemoteIndicator = remoteIndicator;
                                   }
                                   analogHigh = Serial.read();
                                   analogLow = Serial.read();
                                   analogValue = analogLow + (analogHigh * 256);
                           }
                   }
          }
}

void setRemoteState(char value) {
       Serial.write(0x7E);//Start byte
       Serial.write((byte)0x0);//Length
       Serial.write(0x10);//Length High
       Serial.write(0x17);//AT Command Request
       Serial.write((byte)0x0);//Frame ID
       Serial.write((byte)0x0);//Serial Number of Destination
       Serial.write((byte)0x0);
       Serial.write((byte)0x0);
       Serial.write((byte)0x0);
       Serial.write((byte)0x0);
       Serial.write((byte)0x0);
       Serial.write(0xFF);
       Serial.write(0xFF);//End of Serial Number of Destinition

       // 16 bit of recipient or 0xFFFE
       Serial.write(0xFF);
       Serial.write(0xFE);
       Serial.write(0x02);//Apply changes immediately

      //Command name in ASCII charactors
       Serial.write('D');
       Serial.write('1');

      //command data in as many bytes after length bytes
       Serial.write(value);

       //checksum is all bytes after length bytes
       long sum = 0x17 + 0xFF+ 0xFF + 0xFF + 0xFE + 0x02 + 'D' + '1' + value;
       Serial.write (0xFF - ( sum & 0xFF) );
}


Vídeo con la demostración del resultado al ejecutar el código:


No hay comentarios:

Publicar un comentario