Show Posts
Pages: 1 ... 3 4 [5] 6 7 8
61  Topics / Home Automation and Networked Objects / Re: Avoid strange characters from serial buffer on: March 01, 2013, 05:57:13 pm
Thanks it's always good to know another and better options. In my case and because i'm expecting only receive data from sensor and since there is at least 5 bytes in serial buffer, the sensor is disabled i will not receive no other data after that, but sure that the options you gave are more efficient and prevents this possible issue that for loop have if data is received after var gets the number of bytes of serial buffer at that time.
62  Topics / Home Automation and Networked Objects / Re: Avoid strange characters from serial buffer on: March 01, 2013, 02:39:26 pm
I've made it. After a lots of tests i could make the code get always good readings.
For people who may have this issue i let the new code here:
Code:
#include <SPI.h>
#include <VirtualWire.h>
#define TEMPO_LIMITE 200  //Tempo de espera por dados na serie

//DECLARAÇÃO DAS VARIÁVEIS GLOBAIS
//ENABLE/DISABLE PARA O SENSOR
const int sensor = 7;

//LED'S
const int ledTransmissao = 4;
const int ledActivacao = 5;

//ENABLE/DISABLE DO TRANSMISSOR E PINO DE TRANSMISSÃO
const int pinoTransmissao = 8;
const int pinoActTransm = 3;

//VARIÁVEL QUE GUARDA A MENSAGEM A TRANSMITIR
char mensagem[5];

//VARIÁVEIS PARA DADOS LIDOS DO SENSOR
int dados;
char buffer[4];
boolean estado = false;
unsigned long inicioContagem;
unsigned char limpezaSerial;

void setup(){
  
//DEFINIÇÃO DOS PINOS DE ENTRADA E DE SAÍDA
  pinMode(sensor, OUTPUT);
  pinMode(ledTransmissao, OUTPUT);
  pinMode(ledActivacao, OUTPUT);
  digitalWrite(sensor, LOW);
  //digitalWrite(pinoActTransm, LOW);
  digitalWrite(ledTransmissao, LOW);
  digitalWrite(ledActivacao, LOW);
    
//CONFIGURAÇÃO DO VIRTUALWIRE -TRANSMISSÃO-
  vw_set_tx_pin(pinoTransmissao);
  vw_set_ptt_pin(pinoActTransm);
  vw_setup(2000);  //VELOCIDADE DE TRANSMISSÃO DE 2000 BITS POR SEGUNDO

//INICIAR A UART PARA DEBUG
  Serial.begin(9600);
  delay(1000);
}

void loop() {
 
//ENABLE DO SENSOR E LED DE ACTIVAÇÃO LIGADO

  digitalWrite(sensor, HIGH);
  digitalWrite(ledActivacao, HIGH);
  
//LEITURA DOS DADOS ENVIADOS PELO SENSOR
 inicioContagem = millis();
  while ((Serial.available () < 5) && ((millis() - inicioContagem) < TEMPO_LIMITE))
  {
    //Espera que o buffer tenha pelo menos 5 bytes para ler e que não demore mais que 1 segundo
  }
  if (Serial.available() < 5) {
    Serial.println("Nao foram recebidos 5 bytes de dados");
    delay(5000);
  } else {
    digitalWrite(sensor, LOW);
    digitalWrite(ledActivacao, LOW);
    char c = Serial.read();
    if (c == 'R') {
     Serial.readBytesUntil ('\r', &buffer[0], 4);
    
  dados = atoi (buffer);  //CONVERSÃO DOS DADOS RECEBIDOS PARA VALORES INTEIROS
  
  //ENVIO PARA A SÉRIE DO VALOR, PARA DEPURAÇÃO
  Serial.print("BUFFER:");
  Serial.print(buffer);
  Serial.print("  ");
  Serial.print("DADOS:");
  Serial.println(dados);
  memset(&buffer, 0, sizeof(buffer)); //LIMPEZA DO BUFFER
  Serial.print("BUFFER:");
  Serial.println(buffer);
  
/*VERIFICAÇÃO SE OS DADOS RECEBIDOS DO SENSOR SÃO VÁLIDOS. O MÁXIMO
VALOR É 465 -ALTURA MÁXIMA DA ÁGUA- E O MÍNIMO É ZERO -TANQUE VAZIO-*/
  if(dados >= 1 && dados <= 465) {
    mensagem[0] = 'R';
    itoa (dados, &mensagem[1], 10);
    Serial.print("MENSAGEM A TRANSMITIR:");
    Serial.println(mensagem);
    
//LED DE TRANSMISSÃO ACTIVA LIGADO
    digitalWrite(ledTransmissao, HIGH);

//ENVIO DA MENSAGEM 2 VEZES PARA GARANTIR QUE É RECEBIDA  
    for (int i = 0; i<2; i++) {
    vw_send ((uint8_t*)mensagem, strlen(mensagem)); //ENVIO DA MENSAGEM
    vw_wait_tx(); //ESPERA QUE MENSAGEM SEJA TOTALMENTE ENVIADA
    delay(2000);
    }
    digitalWrite(ledTransmissao, LOW);
    memset(&mensagem, 0, sizeof(mensagem)); //LIMPA O BUFFER DA MENSAGEM ENVIADA
}
//LED DE TRANSMISSÃO ACTIVA DESLIGADO
    Serial.print("MENSAGEM:");
    Serial.println(mensagem);
    delay(5000);
  }
   }
      limpezaSerial = Serial.available();
      for (int s=limpezaSerial; s>=0; s--) {
      Serial.read();
      }
}

/*End of Program*/

I had two wrong things. The first was the char array "Buffer" that only had 3 bytes and it needed 4 bytes (i have ever this problem of calculating the number of vectors needed   smiley-confuse  );
The second was the Serial.readBytesUntil function that was only reading 3 bytes and needed to read 4.
The last was the Serial.flush that i thought that was clearing the serial buffer, but it was changed since version 1.0 of IDE, so i cleared it this way:
Code:
limpezaSerial = Serial.available();
      for (int s=limpezaSerial; s>=0; s--) {
      Serial.read();
      }

Cheers!
63  Topics / Home Automation and Networked Objects / Re: Help please..why can't i use the Ethernet.h and the Ethernet library files? on: February 28, 2013, 05:06:27 pm
Take a look at this post -> http://arduino.cc/forum/index.php?topic=122779.0
64  Topics / Home Automation and Networked Objects / Re: Avoid strange characters from serial buffer on: February 28, 2013, 05:03:40 pm
After reading Serial.flush() documentation i realize that it just not clear serial buffer anymore   smiley-eek-blue  So maybe this could be the problem because i always think that it is clearing serial buffer but it's not!
How can i clear the entire serial buffer now?
65  Topics / Home Automation and Networked Objects / Avoid strange characters from serial buffer on: February 28, 2013, 01:51:41 pm
Hi everyone, need your very important help again... i have a ultrasonic sensor from Maxbotix(mb7060) which output delivers asynchronous serial with an RS232 format, except voltages are 0-Vcc. The output is an ASCII capital “R”, followed by three ASCII character digits representing the range in centimeters up to a maximum of 765, followed by a carriage return (ASCII 13). The baud rate is 9600, 8 bits, no parity, with one stop bit.
Because of RS232 format with TTL level (5 volts its a '0' and 0 volts its a '1') i have to put an TTL inverter to invert this voltages to arduino to understand it when it receives the readings on RX pin.

Sometimes i get some strange characters reading (maybe because i was disable the sensor in the middle of reading sended by sensor) so i make some changes in my code, that follows:
Code:
#include <SPI.h>
#include <VirtualWire.h>
#define TEMPO_LIMITE 200  //Tempo de espera por dados na serie

//DECLARAÇÃO DAS VARIÁVEIS GLOBAIS
//ENABLE/DISABLE PARA O SENSOR
const int sensor = 7;

//LED'S
const int ledTransmissao = 4;
const int ledActivacao = 5;

//ENABLE/DISABLE DO TRANSMISSOR E PINO DE TRANSMISSÃO
const int pinoTransmissao = 8;
const int pinoActTransm = 3;

//VARIÁVEL QUE GUARDA A MENSAGEM A TRANSMITIR
char mensagem[5];

//VARIÁVEIS PARA DADOS LIDOS DO SENSOR
int dados;
char buffer[4];
boolean estado = false;
unsigned long inicioContagem;

void setup(){
  
//DEFINIÇÃO DOS PINOS DE ENTRADA E DE SAÍDA
  pinMode(sensor, OUTPUT);
  pinMode(ledTransmissao, OUTPUT);
  pinMode(ledActivacao, OUTPUT);
  digitalWrite(sensor, LOW);
  //digitalWrite(pinoActTransm, LOW);
  digitalWrite(ledTransmissao, LOW);
  digitalWrite(ledActivacao, LOW);
    
//CONFIGURAÇÃO DO VIRTUALWIRE -TRANSMISSÃO-
  vw_set_tx_pin(pinoTransmissao);
  vw_set_ptt_pin(pinoActTransm);
  vw_setup(2000);  //VELOCIDADE DE TRANSMISSÃO DE 2000 BITS POR SEGUNDO

//INICIAR A UART PARA DEBUG
  Serial.begin(9600);
  delay(1000);
}

void loop() {
 
//ENABLE DO SENSOR E LED DE ACTIVAÇÃO LIGADO

  digitalWrite(sensor, HIGH);
  digitalWrite(ledActivacao, HIGH);
  
//LEITURA DOS DADOS ENVIADOS PELO SENSOR
 inicioContagem = millis();
  while ((Serial.available () < 5) && ((millis() - inicioContagem) < TEMPO_LIMITE))
  {
    //Espera que o buffer tenha pelo menos 5 bytes para ler e que não demore mais que 1 segundo
  }
  if (Serial.available() < 5) {
    Serial.println("Não foram recebidos 5 bytes de dados");
    Serial.flush();
    delay(5000);
  } else {
    char c = Serial.read();
    if (c == 'R') {
     Serial.readBytesUntil ('\r', &buffer[0], 4);
  
 //DISABLE DO SENSOR E LED DE ACTIVAÇÃO DESLIGADO
  digitalWrite(sensor, LOW);
  digitalWrite(ledActivacao, LOW);
  Serial.flush();
  dados = atoi (buffer);  //CONVERSÃO DOS DADOS RECEBIDOS PARA VALORES INTEIROS
  
  //ENVIO PARA A SÉRIE DO VALOR, PARA DEPURAÇÃO
  Serial.print("BUFFER:");
  Serial.print(buffer);
  Serial.print("  ");
  Serial.print("DADOS:");
  Serial.println(dados);

  memset(&buffer, 0, sizeof(buffer)); //LIMPEZA DO BUFFER
  Serial.print("BUFFER:");
  Serial.println(buffer);
  
/*VERIFICAÇÃO SE OS DADOS RECEBIDOS DO SENSOR SÃO VÁLIDOS. O MÁXIMO
VALOR É 465 -ALTURA MÁXIMA DA ÁGUA- E O MÍNIMO É ZERO -TANQUE VAZIO-*/
  if(dados >= 1 && dados <= 465) {
    mensagem[0] = 'R';
    itoa (dados, &mensagem[1], 10);
    Serial.print("MENSAGEM A TRANSMITIR:");
    Serial.println(mensagem);
    
//LED DE TRANSMISSÃO ACTIVA LIGADO
    digitalWrite(ledTransmissao, HIGH);

//ENVIO DA MENSAGEM 2 VEZES PARA GARANTIR QUE É RECEBIDA  
    for (int i = 0; i<2; i++) {
    vw_send ((uint8_t*)mensagem, strlen(mensagem)); //ENVIO DA MENSAGEM
    vw_wait_tx(); //ESPERA QUE MENSAGEM SEJA TOTALMENTE ENVIADA
    delay(2000);
    }
    digitalWrite(ledTransmissao, LOW);
    memset(&mensagem, 0, sizeof(mensagem)); //LIMPA O BUFFER DA MENSAGEM ENVIADA
}
//LED DE TRANSMISSÃO ACTIVA DESLIGADO
    Serial.print("MENSAGEM:");
    Serial.println(mensagem);
    Serial.flush();
    delay(5000);
  }
   }
}

/*FIM DO PROGRAMA*/
I expected to get good data from serial, but sometimes it doesn't.
I'm asking if you could help me to do some changes to the code to prevent receiving strange characters.
Adding to this i realize that somethimes i get in serial monitor the same reads (like 8 or 9 times) even pointing sensor to another object at another distance... Got no clues at this time that what could be do this.
Thanks in advance!
66  Topics / Home Automation and Networked Objects / Re: Why's SD -> Ethernet Card -> Webbrowser so slow? on: February 28, 2013, 01:38:20 pm
Ok, its working fine! Many thanks for this post   smiley-wink
67  Using Arduino / Programming Questions / Re: Receiving RX data with circuit powered off?!?? on: February 27, 2013, 03:51:50 pm
I've tested the pull-up resistor today and it worked. With inverter and sensor powered off it just stops getting data (as expected).
I've experienced another problem with my circuit as it sometimes, with everything working (powered on), reads strange values like some strange character, sometimes RRR or 70R  ?!?
Can you check my code on the first post and check if i'm doing something wrong? Today i've tried many other possibilities and changed some parts of the code (essencially the serial read part) and keeps getting this issue sometimes and it is  an intermittent issue.   smiley-confuse
68  Topics / Home Automation and Networked Objects / Re: Why's SD -> Ethernet Card -> Webbrowser so slow? on: February 26, 2013, 06:12:56 pm
Thanks again. I'll test it tomorrow and give a feedback. thanks! (again) smiley-lol
69  Topics / Home Automation and Networked Objects / Re: Why's SD -> Ethernet Card -> Webbrowser so slow? on: February 26, 2013, 05:59:07 pm
ok, so no serial prints mixed with that part of the code, is it?
70  Topics / Home Automation and Networked Objects / Re: Why's SD -> Ethernet Card -> Webbrowser so slow? on: February 26, 2013, 05:50:23 pm
Serial monitor keeps showing 64bytes package sended a lot of times and images (108Kb is the biggest one) were much slower than it was before... It makes no sense because zoomkat tested in his sketch and says it worked, but in mine i really love it to work too, because it takes about 15 seconds to load that image...
71  Using Arduino / Programming Questions / Re: Receiving RX data with circuit powered off?!?? on: February 26, 2013, 05:46:07 pm
Ok that is something i will test tomorrow. So at program level there's nothing that i could do (because current consumption minimize if i use the pull-up resister solution).
72  Using Arduino / Programming Questions / Re: Receiving RX data with circuit powered off?!?? on: February 26, 2013, 05:18:50 pm
Could a pull-up resistor be a solution to this? Or maybe i can add something to my code to prevent it (I think serial data waiting state is 5v isn't it?)
73  Using Arduino / Programming Questions / Re: Receiving RX data with circuit powered off?!?? on: February 26, 2013, 04:04:46 pm
Yes, i've degub it, and keep getting readings of R0 and sometimes R1 or R3 with inverter and sensor powered off, and i'm trying to understand why, if my code only look at serial input starting with an 'R'. And i'm thinking if this can messed up with readings when sensor and inverter are working, because i'm trying to use virtualwires to send this value to another arduino by RF 433MHz, but i couldn't get any message received...
Thanks for being helping me   smiley-wink
74  Using Arduino / Programming Questions / Re: Receiving RX data with circuit powered off?!?? on: February 26, 2013, 03:55:54 pm
Quote
When the power to the inverter is off, you have nothing to clear or reset "c" back to NULL. So Rxxx will stay inside the buffer until new data is available, to overwrite it.
but, memset(&buffer, 0, sizeof(buffer)); makes buffer cleared isn't it? (just trying to understand this  smiley-roll-sweat)
75  Using Arduino / Programming Questions / Re: Receiving RX data with circuit powered off?!?? on: February 26, 2013, 03:43:01 pm
But i expected as i have in my program that only data started with an capital R must be looked at, and avoid any other data that could get in. if inverter and sensor are powered off any data that arduino can receive in RX pin must be all of thet you've said before. It's strange as it must be an R to arduino look at it. Am i right?
Pages: 1 ... 3 4 [5] 6 7 8