Show Posts
|
|
Pages: [1] 2 3
|
|
1
|
International / Portugues / Re: Problemas de dessincronização de dados Arduino Xbee+Computador Xbee
|
on: July 10, 2012, 09:38:52 am
|
Em relação ao processing estou napplets, mas aqui fica a parte principal do código onde são tratados os dados recebido na porta série a partir do xbee import napplet.*; import processing.serial.*; Serial port;
PrintWriter dadosSMEP;
int count; //variables to receive on serial port float yeartime, monthtime, daytime; float hourtime, minutetime, secondtime, oldsecondtime ; float presvalue, oldpresvalue; float Irms; float apparentPower,realPower; float kilowattHour; float timeX; //Calculates the total time, using the time function of processing necessary to use on graphs Xaxis
//Size of the window int plotX=1150, plotY=800; //Dimension of each graph window int plotgwX=575, plotgwY=312; //plotmw means the height of the main window int plotmwY=176;
// color bgc=color(0, 153, 0); //backgroung color of each window color graphc=color(#4D4D4A);//graph line color color textc=color(255); //text color on graph label
PFont plotFont; //font used in each graph
void setup() {
size(plotX,plotY); // Print a list of the serial ports, for debugging purposes: println(Serial.list()); // Open whatever port is the one you're using. port = new Serial(this,"COM14", 9600); // don't generate a serialEvent() unless you get a newline character: port.bufferUntil('\n'); NAppletManager nappletManager = new NAppletManager(this);
//define the initial points of each window created in our sketch nappletManager.createNApplet("Irms", 0, 0); nappletManager.createNApplet("RealPower", width/2, 0); nappletManager.createNApplet("kWh", 0, plotgwY); nappletManager.createNApplet("Presence", width/2, plotgwY); nappletManager.createNApplet("MainWindow", 0, height-plotmwY); plotFont = createFont("SansSerif", 20); textFont(plotFont); //setup the text file to be created with descrition of several parameters dadosSMEP = createWriter("dadosSMEP.csv"); dadosSMEP.print("Date"); dadosSMEP.print(";"); dadosSMEP.print("Irms"); dadosSMEP.print(";"); dadosSMEP.print("Apparent_Power"); dadosSMEP.print(";"); dadosSMEP.print("Real_Power"); dadosSMEP.print(";"); dadosSMEP.print("kWh"); dadosSMEP.print(";"); dadosSMEP.println("Presence"); }
void draw() { background(0, 153, 0); timeX=60.0*minutetime+secondtime+hourtime*3600.0; //Calculates the total time, using the time function of processing //necessary to use on graphs Xaxis
//convert the float number to integer String intyeartime = nf(yeartime, 2,0); String intmonthtime = nf(monthtime, 2,0); String intdaytime = nf(daytime, 2,0); String inthourtime = nf(hourtime, 2,0); String intminutetime = nf(minutetime, 2,0); String intsecondtime = nf(secondtime, 2,0);
if(secondtime != oldsecondtime){//just print if the time change using second from Arduino Time //print the data received from Arduino on the text file dadosSMEP.print(intyeartime+"/"+intmonthtime+"/"+intdaytime+" "+inthourtime+":"+intminutetime+":"+intsecondtime); dadosSMEP.print(";"); dadosSMEP.print(Irms); dadosSMEP.print(";"); dadosSMEP.print(apparentPower); dadosSMEP.print(";"); dadosSMEP.print(realPower); dadosSMEP.print(";"); dadosSMEP.print(kilowattHour); dadosSMEP.print(";"); dadosSMEP.println(presvalue); dadosSMEP.flush(); } oldsecondtime = secondtime; } void mousePressed() { println("Coordinates: " + mouseX +"," + mouseY); }
void serialEvent(Serial port) { String input = port.readStringUntil('\n');//le o que chega a porta ate ao enter para importar as variaveis do arduino /*para isto e necessario que no sketch do arduino as variaveis sejam imprimidas com Serial.print(variavel) e seguidas de virgula ou seja outro Serial.print(",").Sendo que a ultima variavel a registar e imprimida com Serial.println(variavel) */ if (input != null)//se a string contiver algo { input = trim(input); // limpa a string de espaços vazios que não sejam necessários // The data is split into an array of Strings with a comma or asterisk as a delimiter and converted into an array of integers. float [] infos =float (split(input, ";")); //preenche uma matriz com as variaveis importadas sendo que as diferentes variaveis vao para locais diferentes da matriz infos [0],infos [1]... if (infos.length >=11) //11 e o numero de variaveis que importo do arduino "ainda nao percebi bem porque e' necessario este if" { daytime = infos [0]; monthtime = infos [1]; yeartime = infos [2]; hourtime = infos [3]; minutetime = infos [4]; secondtime = infos [5]; Irms = infos [6]; apparentPower = infos [7]; realPower = infos [8]; kilowattHour = infos [9]; presvalue = infos [10]; //presence value } } } Espero que possa ajudar e desculpa a possível estrutura do código mas foi o meu primeiro projeto nesta área 
|
|
|
|
|
2
|
International / Portugues / Re: Problemas de dessincronização de dados Arduino Xbee+Computador Xbee
|
on: July 10, 2012, 09:38:19 am
|
Este é o código completo do Arduino que está a recolher os dados e a enviar posteriormente para o Xbee ligado ao meu computador, onde o processing recolhe os mesmos dados e os reproduz em gráficos... #include <Wire.h> //I2C library #include <SD.h> //SD library #define DS1307_ADDRESS 0x68 //RTC library
byte zero = 0x00; //workaround for issue #527 (RTC variable)
//definition of pin on Arduino board const int PIR1 = 2; const int PIR2 = 3; const int LED = 5; const int pinPot = A0; const int CS_pin = 4; const int pow_pin = 8;
//Motion Detection Variables int val1 = 0; //PIR1 value int val2 = 0; //PIR2 value int reading = 0; //US value int oldreading = 0; //oldUS value int count = 0; //counter to keep LED tu int calibrationPIR = 0; //calibration time of PIR sensors int presence_state = 0; //sets initial value of presence as 0, without presence
const int numberOfSamples = 1185;
//Callibration Constants const int Vrms = 230; const float PF = 0.70;//set an average Power Factor value
//CR Magnetics CR31000-3000 CT parameters const float CT_BURDEN_RESISTOR = 180; const float CT_TURNS = 3000;
/*YHDC SCT-013-030 CT parameters const float CT_BURDEN_RESISTOR = 62; const float CT_TURNS = 1800;*/
//Calibration coeficients int calibrationCT = 0; //calibration time of PIR sensors const float ICAL = 0.96; int lastSampleI,sampleI; //Sample variables float lastFilteredI, filteredI; //Filter variables float sqI, sumI; //Power calculation variables float Irms; //Useful value variables float apparentPower,realPower; float kilowattHour;
unsigned long last_kwhTime; unsigned long kwhTime;
//Real Time Clock Variables int second, minute, hour, weekDay, day, month,year; void setup() { Wire.begin(); // start the I2C bus
Serial.begin(9600); // open the serial port: //Definition of input/output values pinMode(PIR1,INPUT); pinMode(PIR2,INPUT); pinMode(LED,OUTPUT); pinMode(pinPot,INPUT); pinMode(CS_pin, OUTPUT); pinMode(pow_pin, OUTPUT); digitalWrite(pow_pin, HIGH);
//Initialize Card if (!SD.begin(CS_pin)) { Serial.println("Card Failure"); return; } //Serial.println("Card Ready"); //Read the Configuration information (COMMANDS.txt) File logFile = SD.open("SMEP_SD.csv", FILE_WRITE); if (logFile) { String header = "day-month-year hour:minute:second;Irms (A);Apparent Power (W);Real Power (W);Energy (kWh);Presence"; logFile.println(header); logFile.close(); Serial.println(header); } else { Serial.println("Could not read command file."); return; } }
void loop() { runRTC(); passiveIR(); ultrasonic(); presence(); if(second == 0 || second == 30 ){ //condition to run functions //only from 30 to 30 seconds printDate(); consumption(); if( calibrationPIR < 1 ){ presence_state=0; calibrationPIR++; } Serial.println(presence_state); File logFile = SD.open("SMEP_SD.csv" , FILE_WRITE); if (logFile) {//print to micro SD card logFile.print(day); logFile.print("/"); logFile.print(month); logFile.print("/"); logFile.print(year); logFile.print(" "); logFile.print(hour); logFile.print(":"); logFile.print(minute); logFile.print(":"); logFile.print(second); logFile.print(";"); logFile.print(Irms,3); logFile.print(";"); logFile.print(apparentPower,1); logFile.print(";"); logFile.print(realPower,1); logFile.print(";"); logFile.print(kilowattHour,3); logFile.print(";"); logFile.println(presence_state);
logFile.close(); }
else { Serial.println("Couldn´t open log file"); } presence_state=0; } delay (500); //set a delay to avoid the print on serail port of two lines of values }
void passiveIR() { val1 = digitalRead(PIR1); val2 = digitalRead(PIR2); delay (200); }
void ultrasonic() { // step 1: instruct sensor to read centimeters Wire.beginTransmission(112); Wire.write(byte(0x00)); Wire.write(byte(0x51)); Wire.endTransmission(); // stop transmitting
// step 2: wait for readings to happen delay(70); // datasheet suggests at least 65 milliseconds
// step 3: instruct sensor to return a particular echo reading Wire.beginTransmission(112); // transmit to device #112 Wire.write(byte(0x02)); // sets register pointer to echo #1 register (0x02) Wire.endTransmission(); // stop transmitting
// step 4: request reading from sensor Wire.requestFrom(112, 2); // request 2 bytes from slave device #112
// step 5: receive reading from sensor if(2 <= Wire.available()) // if two bytes were received { oldreading = reading; reading = Wire.read(); // receive high byte (overwrites previous reading) reading = reading << 8; // shift high byte to be high 8 bits reading |= Wire.read(); // receive low byte as lower 8 bits }
delay(25); // wait before next reading: }
void presence() { //the follow condition memorizes if there was a motion during the 30seconds and print it int f = reading - oldreading; if((val1 == LOW) || (val2 == LOW) || (f > 15)){ presence_state=1; digitalWrite(LED,HIGH); } else{ digitalWrite(LED,LOW); } } void consumption() {
for (int n=0; n < numberOfSamples; n++) {
lastSampleI = sampleI; //Used for current offset removal lastFilteredI = filteredI; //Used for voltage offset removal //sampleI = map(analogRead(pinPot),0,1023,0,30); sampleI = analogRead(pinPot); //Apply digital high pass filters to remove 2.5V DC offset (centered on 0V) filteredI = 0.996*(lastFilteredI+sampleI-lastSampleI); //Root-mean-square method current //1) square current values sqI = filteredI * filteredI; //2) sum sumI += sqI; } //Calculation of the root of the mean of the current squared (rms) //Calibration coeficients applied. //Calculated ratio constants ///I_Ratio=(CT_Turns / CT_BR)* (5V/1024)*ICA float I_RATIO = CT_TURNS / CT_BURDEN_RESISTOR * 5 / 1024 * ICAL; Irms = I_RATIO*sqrt(sumI / numberOfSamples); //condition to avoid when there's no current the code //prints a current value due to resolution of ADC if(Irms <0.3 || calibrationCT < 1 ){ Irms=0.000; Serial.print(Irms,3); Serial.print(';'); // Calculate power values apparentPower = Vrms * Irms; realPower = PF * apparentPower; Serial.print(apparentPower,1); Serial.print(';'); Serial.print(realPower,1); Serial.print(';'); // Calculate running total kilowatt hours // This value will reset in 50 days last_kwhTime = kwhTime; kwhTime = millis(); kilowattHour += (realPower / 1000) * ((kwhTime - last_kwhTime) / 3600000.0); Serial.print(kilowattHour,3); Serial.print(';'); // Reset sample totals sumI = 0; calibrationCT++; } else{ //if(Irms >= 0.14 && calibrationCT >= 1 ){
Serial.print(Irms,3); Serial.print(';'); // Calculate power values apparentPower = Vrms * Irms; realPower = PF * apparentPower; Serial.print(apparentPower,1); Serial.print(';'); Serial.print(realPower,1); Serial.print(';'); // Calculate running total kilowatt hours // This value will reset in 50 days last_kwhTime = kwhTime; kwhTime = millis(); kilowattHour += (realPower / 1000) * ((kwhTime - last_kwhTime) / 3600000.0); Serial.print(kilowattHour,3); Serial.print(';'); // Reset sample totals sumI = 0; calibrationCT++; } }
byte bcdToDec(byte val) { // Convert binary coded decimal to normal decimal numbers return ( (val/16*10) + (val%16) ); }
void runRTC(){
// Reset the register pointer Wire.beginTransmission(DS1307_ADDRESS); Wire.write(zero); Wire.endTransmission();
Wire.requestFrom(DS1307_ADDRESS, 7);
second = bcdToDec(Wire.read()); minute = bcdToDec(Wire.read()); hour = bcdToDec(Wire.read() & 0b111111); //24 hour time weekDay = bcdToDec(Wire.read()); //0-6 -> sunday - Saturday day = bcdToDec(Wire.read()); month = bcdToDec(Wire.read()); year = bcdToDec(Wire.read()); }
void printDate(){
Serial.print(day); Serial.print(";"); Serial.print(month); Serial.print(";"); Serial.print(year); Serial.print(";"); Serial.print(hour); Serial.print(";"); Serial.print(minute); Serial.print(";"); Serial.print(second); Serial.print(";");
}
|
|
|
|
|
4
|
International / Portugues / Problemas de dessincronização de dados Arduino Xbee+Computador Xbee
|
on: July 07, 2012, 05:12:11 am
|
|
Olá pessoal! Eu estou a desenvolver um projeto onde tenho de enviar dados de 30 em 30 segundos de um Arduino ligado a um Xbee para um Xbee ligado à porta série do meu portátil. Por sua vez no portátil através de um programa feito no processing recebo os dados na porta série e coloco-os em gráfico. Acontece que após ter deixado o projeto rolar durante a noite ao analisar os dados recebidos no processing deu para ver que a sua qualidade não era a melhor, pois por vezes havia dessincronização dos dados recebidos pelo xbee ligado ao pc, como podem ver de seguida:
04-07-2012 19:51 0,321 73,9 51,8 0,033 1 04/07/152 00:�:48,6 0,033 4 7 12 19 04-07-2012 19:54 0,274 63,1 44,1 0,034 1 ... 04/07/12 20,26:�:� 7 12 20 36 0 04-07-2012 20:36 NaN 48,1 0,065 NaN 7 04/07/12 230:00,265:60,066 NaN 7 12 20 38 04-07-2012 20:40 0,271 62,3 43,6 0,068 1
Segundo consigo perceber ele não obedece à sequência de valores que implementei, sendo que muitas das vezes salta. Em relação ao problema ele só pode estar relacionado com o Xbee pois estou a usar um cartão sd no arduino para armazenar os dados recolhidos e neste caso está tudo bem... Em relação à configuração dos Xbees estão usando ambos o modo AT, sendo um o router e outro o coordenador. Em relação ao código não estou usando nada diretamente ligado ao xbee, pelo que apenas imprimo os valores na porta série do arduino/xbee e os recolho pela porta série do computador. Acredito que no código vou ter de implementar outra estratégia ou utilizar o modo API, mas gostava que alguém me pudesse dar umas dicas, ou então dizer onde posso encontrar projetos semelhantes...
|
|
|
|
|
5
|
Using Arduino / Networking, Protocols, and Devices / Arduino Xbee-Computer Xbee Communication problem
|
on: July 06, 2012, 04:15:07 am
|
|
Hey guys!!
I am developing a project where I am sending some values from 30 to 30 seconds from an Arduino to a computer, using xbee. I had left my arduino during all night sending data, but today when I was analyzing the data received on my computer I observed that there are a lot of mistakes on it:
04-07-2012 19:51 0,321 73,9 51,8 0,033 1 04/07/152 00:�:48,6 0,033 4 7 12 19 04-07-2012 19:54 0,274 63,1 44,1 0,034 1 ... 04/07/12 20,26:�:� 7 12 20 36 0 04-07-2012 20:36 NaN 48,1 0,065 NaN 7 04/07/12 230:00,265:60,066 NaN 7 12 20 38 04-07-2012 20:40 0,271 62,3 43,6 0,068 1
It seems that it is not taking all values using the sequence that I program, sometimes it jump values. I'm sure that it is a problem from Xbee because I am using also a SD card to save this data and here it is right.
I am using two Xbee's Series 2 configured for AT mode, one as router and the other as coordinator. Concerning the code it doesn't have nothing related with xbee because I am just printing the data in serial port of one xbee(Arduino) while the one that is connected with xbee receives this data in serail port of the computer...
I hope somebody can help me
|
|
|
|
|
6
|
Using Arduino / Sensors / Re: Question regarding High Pass Filter
|
on: June 01, 2012, 01:35:40 pm
|
Hi!! Seems that I discovered somebody with the same doubts as me... I understand almost everything, not at all the concept of "internal ADC offset". In other hand if DC bias do all this stuff of negative signal filter, why it is used the voltage divider?? I'm getting so confused with all this concepts 
|
|
|
|
|
7
|
International / Portugues / Re: Arduino + Xbee + Internet
|
on: May 21, 2012, 07:50:39 am
|
Quase todo o pessoal usa o ethernet shield mas os meus €€ já chegaram ao fim  Sendo assim, não há forma de exportar os dados, que estão a chegar ao xbee conectado ao computador, diretamente para a internet sem um roteador? Sabes onde posso aprender a fazê-lo...
|
|
|
|
|
8
|
International / Portugues / Arduino + Xbee + Internet
|
on: May 21, 2012, 06:39:33 am
|
Olá pessoal! Ultrapassado o problema do interface no computador, onde vou utilizar em princípio ou processing ou KST, gostaria de avançar para a comunicação do arduino com a internet. O meu arduino está conectado aos vários sensores, e tem uma shield xbee. Desta forma tendo o xbee no arduino (via shield) e tendo outro xbee ligado ao computador (via usb explorer-sparkfun), posso fazer comunicação dos dados recolhidos diretamente para a porta série do computador. Acontece que sou mesmo muito principiante em tudo o que esteja relacionado com webservers, html, pelo que ainda não encontrei nenhum exemplo simples e elucidativo de como fazer esta exportação dos dados para a internet. Encontrei diversos tópicos mas que utilizavam a shield ethernet. O meu objetivo seria utilizar o google apps ou outro género de aplicação gráfica para poder visualizar os dados em gráfico em tempo real, tal como já estou a fazer no kst... Agradeço a vossa ajuda 
|
|
|
|
|
11
|
Using Arduino / Sensors / Re: Using Ultrasonic Range Finder SRF02 as Motion Detector
|
on: May 15, 2012, 07:05:45 pm
|
No comments for all my mistakes on code... The problem was just linked with a wrong position of variable declaration... Now I hope I can use this on Processing Great thanks for your support #include <Wire.h>
//definition of pin on Arduino board const int PIR1 = 2; const int PIR2 = 3; const int LED1 = 4; const int LED2 = 5; const int pinPot=A0;
//Definition of variables to use int val1 = 0; //PIR1 value int val2 = 0; //PIR2 value int reading = 0; //US value int oldreading = 0; //oldUS value int count = 0; //counter to keep LED turned on int calibrationTime = 5; //calibration time of PIR sensors
void setup() { Wire.begin(); // start the I2C bus
Serial.begin(9600); // open the serial port: //Definition of input/output values pinMode(PIR1,INPUT); pinMode(PIR2,INPUT); pinMode(LED1,OUTPUT); pinMode(LED2,OUTPUT);
//give the sensor some time to calibrate Serial.println("Sensor Calibration in Progress"); Serial.println("------------------------------"); for(int i = 0; i < calibrationTime; i++){ Serial.print("."); digitalWrite(LED1, HIGH); delay(250); digitalWrite(LED2, HIGH); delay(250); digitalWrite(LED1, LOW); delay(250); digitalWrite(LED2, LOW); delay(250); }
Serial.println(""); Serial.println("Sensor Calibration Completed"); Serial.println("Sensor Reading Active"); delay(100); }
void loop() { passiveIR(); ultrasonic(); presence(); }
void passiveIR() { val1 = digitalRead(PIR1); val2 = digitalRead(PIR2); }
void ultrasonic() { // step 1: instruct sensor to read centimeters Wire.beginTransmission(112); // transmit to device #112 (0x70) // the address specified in the datasheet is 224 (0xE0) // but i2c adressing uses the high 7 bits so it's 112 Wire.write(byte(0x00)); // sets register pointer to the command register (0x00) Wire.write(byte(0x51)); // command sensor to measure in "inches" (0x50) // use 0x51 for centimeters // use 0x52 for ping microseconds Wire.endTransmission(); // stop transmitting
// step 2: wait for readings to happen delay(70); // datasheet suggests at least 65 milliseconds
// step 3: instruct sensor to return a particular echo reading Wire.beginTransmission(112); // transmit to device #112 Wire.write(byte(0x02)); // sets register pointer to echo #1 register (0x02) Wire.endTransmission(); // stop transmitting
// step 4: request reading from sensor Wire.requestFrom(112, 2); // request 2 bytes from slave device #112
// step 5: receive reading from sensor if(2 <= Wire.available()) // if two bytes were received { oldreading = reading; reading = Wire.read(); // receive high byte (overwrites previous reading) reading = reading << 8; // shift high byte to be high 8 bits reading |= Wire.read(); // receive low byte as lower 8 bits }
delay(25); // wait before next reading: }
void presence() { int f = reading - oldreading; if((val1 == HIGH) && (val2 == HIGH) && (f < 15)){ Serial.print('1'); Serial.print(';');
} if((val1 == LOW) || (val2 == LOW) || f >15){ Serial.print('0'); Serial.print(';'); count = 1; if(count > 0){ count=count+1; digitalWrite(LED1, HIGH); } if(count == 50){ digitalWrite(LED1, LOW); count = 0; } } }
|
|
|
|
|
12
|
Using Arduino / Sensors / Re: Using Ultrasonic Range Finder SRF02 as Motion Detector
|
on: May 15, 2012, 05:29:08 pm
|
Sorry. Seems that I'm almost sleeping  Although I think the next code is right if(val1 == HIGH && val2 == HIGH && f < 15){ Serial.println('1'); //Serial.print(','); } if(val1 == LOW || val2 == LOW || f >15){ Serial.println('0'); //Serial.print(','); the first condition if(val1 == HIGH && val2 == HIGH && f < 15) isn't being detected. Arduino is just printing 0s I will get crazy at the end of this... seems simple but there's always a little problem
|
|
|
|
|
13
|
Using Arduino / Sensors / Re: Using Ultrasonic Range Finder SRF02 as Motion Detector
|
on: May 15, 2012, 05:11:10 pm
|
I think you are right. I wrote 250 cause was the delay suggest in Arduino playgroung for this sensor. I will change it and see what happen... I already updated the code but I'm still having problems #include <Wire.h>
//definition of pin on Arduino board const int PIR1 = 2; const int PIR2 = 3; const int LED1 = 4; const int LED2 = 5;
//Definition of variables to use int val1 = 0; int val2 = 0; int count1 = 0; int count2 = 0;
int calibrationTime = 20; //calibration time of PIR sensors
void setup()
{ Wire.begin(); // start the I2C bus
Serial.begin(9600); // open the serial port: //Definition of input/output values pinMode(PIR1,INPUT); pinMode(PIR2,INPUT); pinMode(LED1,OUTPUT); pinMode(LED2,OUTPUT);
//give the sensor some time to calibrate Serial.println("Sensor Calibration in Progress"); Serial.println("------------------------------"); for(int i = 0; i < calibrationTime; i++){ Serial.print("."); digitalWrite(LED1, HIGH); delay(250); digitalWrite(LED2, HIGH); delay(250); digitalWrite(LED1, LOW); delay(250); digitalWrite(LED2, LOW); delay(250); }
Serial.println(""); Serial.println("Sensor Calibration Completed"); Serial.println("Sensor Reading Active"); delay(100); }
int reading = 0; int oldreading = 0;
void loop()
{ passiveIR(); ultrasonic(); presence();
}
void passiveIR() { int val1 = digitalRead(PIR1); int val2 = digitalRead(PIR2); Serial.print(val1); Serial.print(','); Serial.print(val2); Serial.print(','); }
void ultrasonic()
{
// step 1: instruct sensor to read centimeters Wire.beginTransmission(112); // transmit to device #112 (0x70) // the address specified in the datasheet is 224 (0xE0) // but i2c adressing uses the high 7 bits so it's 112 Wire.write(byte(0x00)); // sets register pointer to the command register (0x00) Wire.write(byte(0x51)); // command sensor to measure in "inches" (0x50) // use 0x51 for centimeters // use 0x52 for ping microseconds Wire.endTransmission(); // stop transmitting
// step 2: wait for readings to happen delay(70); // datasheet suggests at least 65 milliseconds
// step 3: instruct sensor to return a particular echo reading Wire.beginTransmission(112); // transmit to device #112 Wire.write(byte(0x02)); // sets register pointer to echo #1 register (0x02) Wire.endTransmission(); // stop transmitting
// step 4: request reading from sensor Wire.requestFrom(112, 2); // request 2 bytes from slave device #112
// step 5: receive reading from sensor if(2 <= Wire.available()) // if two bytes were received { oldreading = reading; //Serial.print(oldreading); // Serial.print(','); reading = Wire.read(); // receive high byte (overwrites previous reading) reading = reading << 8; // shift high byte to be high 8 bits reading |= Wire.read(); // receive low byte as lower 8 bits
}
delay(25); // wait before next reading:
}
void presence() { Serial.print(reading); // print the reading Serial.print(','); Serial.print(oldreading); // print the reading Serial.print(','); int f = reading - oldreading; Serial.print(f); // print the reading Serial.print(','); if(val1 == HIGH && val2 == HIGH && f < 15){ Serial.println('1'); //Serial.print(','); } if(val1 == LOW || val2 == LOW || f >15){ Serial.println('0'); //Serial.print(','); } for(int i=0; i<50; i++){ } } I forgot to tell that initially I was thinking to do the treatment of each sensor on processing sketch, but concerning that I will use the final value in a graph and in an interface, I thought that it is better if the value is simplified...
|
|
|
|
|
15
|
Using Arduino / Sensors / Re: Using Ultrasonic Range Finder SRF02 as Motion Detector
|
on: May 15, 2012, 04:30:56 pm
|
Hi Tesla!! Thanks again for your answer. You are right, but sometimes is difficult for me to explain everything... I'm using all this sensors to get a better result of motion inside a room... So each sensor will be oriented for a different direction. If one of this sensors detects motion (PIR) or distance variation (US), it should report me 0. When it detects it should report the value 0 for some seconds. In other hand when it not detect any movement it returns 1. The better way is present my entire code (still have a lot of mistakes  ) #include <Wire.h>
const int PIR1 = 2; const int PIR2 = 3; const int LED1 = 4; const int LED2 = 5;
//Definition of variables to use int val1 = 0; int val2 = 0; int count1 = 0; int count2 = 0;
int calibrationTime = 20; //calibration time of PIR sensors
void setup()
{ Wire.begin(); // start the I2C bus
Serial.begin(9600); // open the serial port: //Definition of input/output values pinMode(PIR1,INPUT); pinMode(PIR2,INPUT); pinMode(LED1,OUTPUT); pinMode(LED2,OUTPUT);
//give the sensor some time to calibrate Serial.println("Sensor Calibration in Progress"); Serial.println("------------------------------"); for(int i = 0; i < calibrationTime; i++){ Serial.print("."); digitalWrite(LED1, HIGH); delay(250); digitalWrite(LED2, HIGH); delay(250); digitalWrite(LED1, LOW); delay(250); digitalWrite(LED2, LOW); delay(250); }
Serial.println(""); Serial.println("Sensor Calibration Completed"); Serial.println("Sensor Reading Active"); delay(100); }
int reading = 0; int oldreading = 0;
void loop()
{ passiveIR(); ultrasonic(); presence();
}
void passiveIR() { int val1 = digitalRead(PIR1); int val2 = digitalRead(PIR2);
}
void ultrasonic()
{
// step 1: instruct sensor to read centimeters Wire.beginTransmission(112); // transmit to device #112 (0x70) // the address specified in the datasheet is 224 (0xE0) // but i2c adressing uses the high 7 bits so it's 112 Wire.write(byte(0x00)); // sets register pointer to the command register (0x00) Wire.write(byte(0x51)); // command sensor to measure in "inches" (0x50) // use 0x51 for centimeters // use 0x52 for ping microseconds Wire.endTransmission(); // stop transmitting
// step 2: wait for readings to happen delay(70); // datasheet suggests at least 65 milliseconds
// step 3: instruct sensor to return a particular echo reading Wire.beginTransmission(112); // transmit to device #112 Wire.write(byte(0x02)); // sets register pointer to echo #1 register (0x02) Wire.endTransmission(); // stop transmitting
// step 4: request reading from sensor Wire.requestFrom(112, 2); // request 2 bytes from slave device #112
// step 5: receive reading from sensor if(2 <= Wire.available()) // if two bytes were received { oldreading = reading; //Serial.print(oldreading); // Serial.print(','); reading = Wire.read(); // receive high byte (overwrites previous reading) reading = reading << 8; // shift high byte to be high 8 bits reading |= Wire.read(); // receive low byte as lower 8 bits //Serial.println(reading); // print the reading // Serial.print(',');
}
delay(250); // wait before next reading:
}
void presence() { int f = reading - oldreading; if(val1 == HIGH || val2 == HIGH || f >15){ Serial.print('0'); Serial.print(','); } if(val1 == LOW || val2 == LOW || f < 15){ Serial.print('1'); Serial.print(','); for(int i=0; i<50; i++){ } } } I hope you can reach my goal, if can try to explain better. Obrigado 
|
|
|
|
|