// DRESKE // ARDUINO MEGA // TO DO: // - Mapear led de bateria // - Configuracion con IP manual // // COFIGURACION SET COMM SIZE 1024 // COFIGURACION SET COMM TIME 1 #include "WiFlyHQ_.h" #include //const char mySSID[] = "gira_li"; //const char myPassword[] = "wifpanspermia"; //const char *IP = "192.168.1.31"; const char mySSID[] = "hangar_nau3"; const char myPassword[] = "m1cr0fug4s"; const char *IP = "172.26.0.46"; const uint16_t outPort = 8000; const uint16_t localPort = 9000; #define debug 0 #define bat A8 #define ledBat 22 #define ledDatos 2 #define batThreshold 155 uint32_t analogPin[] = { A2,A3,A4,A5,A6,A7}; uint32_t analogIn[] = { 0,0,0,0,0,0}; uint32_t analogInOld[] = { 0,0,0,0,0,0}; uint8_t pwmOut[] = { 5,6,7,8}; uint8_t digitalPin[] = { 23,25,27,29,31,33,35,37,39,41,43,45,47,49,51,53}; uint8_t digitalIn[] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; uint8_t digitalInOld[] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; boolean datoPrevio = true; uint32_t counter=0; uint32_t error=0; uint8_t tick=0; uint8_t tempByte = 0; uint8_t serialCount=0; uint32_t lastSend = 0; uint32_t lastEvent = 0; uint32_t intValue1, intValue2, intValue3, intValue4; unsigned long prevMillis = 0; unsigned long prevMillis2 = 0; #define MESSAGE_SIZE 16 char incomming_message[] = { // Message template commands '/', 'd', 'r', '/', // ANALOG OUT TEMPLATE 'p' , 'w' , 'm', B0, ',', 'i', B0, B0, B0 , B0 , B0, B0 // 4 analog outs = 4 integers }; char digi[] = { // Message template commands '/', 'd', 'r', '/', // DIGITAL OUT TEMPLATE 'd' , '1' , B0, B0, ',', 'i', B0, B0, B0 , B0 , B0, B0 // 16 digital outs = 16 bit (2 integers) }; char sensor[MESSAGE_SIZE] = { // Message template '/', 'd', 'r', '/', // SENSOR MESSAGE TEMPLATE 's' , '1' , B0, B0, ',', 'i', B0, B0, B0 , B0 , B0, B0 }; char pingBat[MESSAGE_SIZE] = { // Message template '/', 'd', 'r', '/', // PING MESSAGE TEMPLATE 'b' , 'a' , 't', B0, ',', 'i', B0, B0, B0 , B0 , B0, B0 }; char err[MESSAGE_SIZE] = { // Message template '/', 'd', 'r', '/', // ERROR MESSAGE TEMPLATE 'e' , 'r' , 'r', B0, ',', 'i', B0, B0, B0 , B0 , B0, B0 }; WiFly wifly; void setup() { char buf[32]; Wire.begin(); pinMode(ledDatos, OUTPUT); // LED DATOS pinMode(ledBat, OUTPUT); // LED BATERIA digitalWrite(ledBat, LOW); // LED BATERIA for(int j=0; j 30000) { // interrupccion cada 30s para enviar estado de bateria y actualizacion de led digitalWrite(ledDatos, LOW); long bateria = analogRead(bat)/4; //Serial.print("bat "); //Serial.print(bateria); //delay(1000); if( bateria < batThreshold) // Bat MAX 15V = 213 digitalWrite(ledBat, LOW); // Bat 11V = 155 -> threshold LED else // Bat min 10V = 141 digitalWrite(ledBat, HIGH); bateria = map(bateria, 141, 213, 0, 100); if( bateria > 100 ) bateria = 100; else if( bateria < 0 ) bateria = 0; pingBat[MESSAGE_SIZE-1] = (int)bateria; for(int b=0 ; b < MESSAGE_SIZE; b++) wifly.write(pingBat[b]); lastSend = millis(); digitalWrite(ledDatos, HIGH); } if ((millis() - prevMillis) > 5) { prevMillis = millis(); checkAnalogOne(counter); counter++; if(counter == 6) counter = 0; } if ((millis() - prevMillis2) > 23) { prevMillis2 = millis(); checkDigital(); } //checkAnalog(); // chequeamos entradas analogicas!!! checkIncomming(); // chequeamos los mensajes por wifi!!! } void checkAnalog() { digitalWrite(ledDatos, LOW); // led de datos ON for(int i=0 ; i<6 ; i++) { //analogIn[i] = (int)(average(analogPin[i])); analogIn[i] = analogRead(analogPin[i]); if ( ( analogIn[i] != analogInOld[i] ) && (analogIn[i] > analogInOld[i] + 8 ) && (analogIn[i] < analogInOld[i] - 8 ) ) { sensor[MESSAGE_SIZE-1] = analogIn[i]/4; sensor[5] = i + 1 +'0'; for(int b=0 ; b < MESSAGE_SIZE; b++) { Serial1.write((unsigned char)sensor[b]); //Serial.print((unsigned char)sensor[b]); //Serial.print(" "); } //Serial.println(); //delay(100); analogInOld[i] = analogIn[i]; if(debug) { //Serial.println(sensor); Serial.print("sensor "); Serial.print(i+1, DEC); Serial.print(" > "); Serial.println( (unsigned char)sensor[MESSAGE_SIZE-1] , DEC); } } } digitalWrite(ledDatos, HIGH); // led de datos ON } void checkAnalogOne(int i) { digitalWrite(ledDatos, LOW); // led de datos ON analogIn[i] = (int)(average(analogPin[i])); //analogIn[i] = analogRead(analogPin[i]); if ( ( analogIn[i] != analogInOld[i] ) ) { sensor[MESSAGE_SIZE-1] = analogIn[i]/4; sensor[5] = i + 1 +'0'; for(int b=0 ; b < MESSAGE_SIZE; b++) { Serial1.write((unsigned char)sensor[b]); //Serial.print((unsigned char)sensor[b]); //Serial.print(" "); } //Serial.println(); //delay(100); analogInOld[i] = analogIn[i]; if(debug) { //Serial.println(sensor); Serial.print("sensor "); Serial.print(i+1, DEC); Serial.print(" > "); Serial.println( (unsigned char)sensor[MESSAGE_SIZE-1] , DEC); } } digitalWrite(ledDatos, HIGH); // led de datos ON } int average(int anaPin) { int lecturas = 4; long total = 0; long average = 0; int count = 0; for(int i=0; i 15) // Compruebo una vez recibido todo el array { datoPrevio = 0; serialCount = 0; for(int j=0; j dato valido datoPrevio = true; else datoPrevio = false; serialCount++; if( (serialCount >= MESSAGE_SIZE) && !datoPrevio ) { error++; Serial.print("Error:"); Serial.println(error); serialCount=0; for(int j=0; j= MESSAGE_SIZE) && datoPrevio ) { // Check OSC Message /* SALIDAS DIGITALES OK if( (incomming_message[0] == '/') && ( incomming_message[1] == 'd' ) && ( incomming_message[2] == 'r' ) && ( incomming_message[3] == '/' ) && ( incomming_message[4] == 'd' ) && ( incomming_message[5] == 'i' ) && ( incomming_message[6] == 'g' ) ) // MENSAJE DE CONTROL DE MOTOR { intValue2 = incomming_message[14]; intValue1 = incomming_message[15]; for(int j=0; j<8 ; j++) { digitalWrite(digiOut[j], bitRead(intValue1, j)); digitalWrite(digiOut[j+8], bitRead(intValue2, j)); } Serial.print("OK: "); Serial.print((unsigned char)intValue1); Serial.print(" "); Serial.println((unsigned char)intValue2); */ if( (incomming_message[0] == '/') && ( incomming_message[1] == 'd' ) && ( incomming_message[2] == 'r' ) && ( incomming_message[3] == '/' ) && ( incomming_message[4] == 'p' ) && ( incomming_message[5] == 'w' ) && ( incomming_message[6] == 'm' ) ) // MENSAJE DE CONTROL DE MOTOR { intValue4 = incomming_message[12]; intValue3 = incomming_message[13]; intValue2 = incomming_message[14]; intValue1 = incomming_message[15]; analogWrite(pwmOut[0], (unsigned char)intValue1); analogWrite(pwmOut[1], (unsigned char)intValue2); analogWrite(pwmOut[2], (unsigned char)intValue3); analogWrite(pwmOut[3], (unsigned char)intValue4); if(debug) { Serial.print("OK: "); Serial.print((unsigned char)intValue1); Serial.print(" "); Serial.print((unsigned char)intValue2); Serial.print(" "); Serial.print((unsigned char)intValue3); Serial.print(" "); Serial.println((unsigned char)intValue4); } } //----------------------------------- //Serial.print(incomming_message); //Serial.print(" "); //Serial.print((unsigned char)incomming_message[14], DEC); //Serial.print(" "); //Serial.println((unsigned char)incomming_message[15], DEC); } digitalWrite(ledDatos, HIGH); //APAGAMOS LED DE DATOS } }