Command is 'received' after 3-4 attempts

Hello guys,

I am working on a project, where I have connected 3 sensors and a motor on an Arduino UNO. I use a bluetooth module (sparkfun silver mate) to communicate with a computer. I created a code for the system but i have a problem.

As you can see in the code, the user inserts a command (one of: 'u', 't', 'd', 'm') and in the code through a switch command I get an output.

To communicate with the bluetooth I use CoolTerm.

When I enter a command, for example 't' sometimes the code does not receive the command. I think it is delayed in another point but i cannot quite figure out where. I attach the code so you can check it out.. However, every once in a while it actually gets the command and produces the correct output.

Any ideas why?!

Code:

//Motor
#include <Servo.h>

//Thermometer
#include <OneWire.h>

//Motor
Servo myservo; // create servo object to control a servo
// a maximum of eight servo objects can be created

//Thermometer
int DS18S20_Pin = 12; //DS18S20 Signal pin on digital 12

int pos = 0; // variable to store the servo position

// Pressure Sensor
int FSR_Pin = A0; //analog pin 0

//Temperature chip i/o
OneWire ds(DS18S20_Pin); // on digital pin 12

int incomingByte = 0;

void setup() {
myservo.attach(9);
// initialize serial:
Serial.begin(115200);
// // initialize the led pin
// pinMode(13, OUTPUT);
}

void loop() {

if (Serial.available() > 0) {
// read the incoming byte:
incomingByte = Serial.read();
}

while (Serial.available()) {
char inChar = (char)Serial.read();

switch(inChar) {

case 'u':
for(pos = 0; pos < 90; pos += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15);
}
break;
//Motor Reverse
case 'd':

for(pos = 90; pos>=1; pos-=1) // goes from 180 degrees to 0 degrees
{
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}

break;

//Pressure Sensor
case 'p':
{
int FSRReading = analogRead(FSR_Pin);

Serial.println(FSRReading);

delay(250); //just here to slow down the output for easier reading
}
break;

//Thermistor
case 'm':
{int ThermistorPin = 1; // Analog input pin for thermistor voltage
int Vo; // Integer value of voltage reading
float R = 9870.0; // Fixed resistance in the voltage divider
float logRt,Rt,T;
float c1 = 1.009249522e-03, c2 = 2.378405444e-04, c3 = 2.019202697e-07;
Vo = analogRead(ThermistorPin);
Rt = R*( 1023.0 / (float)Vo - 1.0 );
logRt = log(Rt);
T = ( 1.0 / (c1 + c2logRt + c3logRtlogRtlogRt ) ) - 273.15;
Serial.print(" "); Serial.print(Vo);
Serial.print(" "); Serial.print(Rt);
Serial.print(" "); Serial.println(T);
delay(200);
}
break;

//Liquid Temperature Sensor
case 't':
float temperature = getTemp();
Serial.println(temperature);

delay(100); //just here to slow down the output so it is easier to read
break;
}
Serial.println(inChar);
}

}

//returns the temperature from one DS18S20 in DEG Celsius
float getTemp(){

byte data[12];
byte addr[8];

if ( !ds.search(addr)) {
//no more sensors on chain, reset search
ds.reset_search();
return -1000;
}

if ( OneWire::crc8( addr, 7) != addr[7]) {
Serial.println("CRC is not valid!");
return -1000;
}

if ( addr[0] != 0x10 && addr[0] != 0x28) {
Serial.print("Device is not recognized");
return -1000;
}

ds.reset();
ds.select(addr);
ds.write(0x44,1); // start conversion, with parasite power on at the end

byte present = ds.reset();
ds.select(addr);
ds.write(0xBE); // Read Scratchpad

for (int i = 0; i < 9; i++) { // we need 9 bytes
data = ds.read();

  • }*

  • ds.reset_search();*

  • byte MSB = data[1];*

  • byte LSB = data[0];*

  • float tempRead = ((MSB << 8) | LSB); //using two's compliment*

  • float TemperatureSum = tempRead / 16;*

  • return TemperatureSum;*
    }

if (Serial.available() > 0) {
      // read the incoming byte:
      incomingByte = Serial.read();
      }



  while (Serial.available()) {
    char inChar = (char)Serial.read();

Think carefully again.

And code tags. Thanks for demonstrating why we ask you to use them.

Thanks for the answer!

You are right about the tags, my mistake, i ll use them in the future! Thanks again!

svoronos:
You are right about the tags, my mistake, i ll use them in the future! Thanks again!

That's a relief! I thought for a moment I was missing the library that let me shift left 8) times!

 //Motor
#include <Servo.h> 

//Thermometer
#include <OneWire.h> 


//Motor
Servo myservo;  // create servo object to control a servo 
                // a maximum of eight servo objects can be created 
 
//Thermometer
int DS18S20_Pin = 12; //DS18S20 Signal pin on digital 12

int pos = 0;    // variable to store the servo position 


// Pressure Sensor
int FSR_Pin = A0; //analog pin 0

//Temperature chip i/o
OneWire ds(DS18S20_Pin);  // on digital pin 12


int incomingByte = 0;

void setup() {
  myservo.attach(9); 
  // initialize serial:
  Serial.begin(115200);
//  // initialize the led pin
//  pinMode(13, OUTPUT);
}

void loop() {
  
  
  if (Serial.available() > 0) {
      // read the incoming byte:
      incomingByte = Serial.read();
      }



  while (Serial.available()) {
    char inChar = (char)Serial.read();
    
    switch(inChar) {
      
      case 'u':
         for(pos = 0; pos < 90; pos += 1)  // goes from 0 degrees to 180 degrees 
  {                                  // in steps of 1 degree 
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15); 
  }
      break;
      //Motor Reverse
      case 'd':
      
      for(pos = 90; pos>=1; pos-=1)     // goes from 180 degrees to 0 degrees 
  {                                
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);                       // waits 15ms for the servo to reach the position 
  } 
  
      break;
      
//Pressure Sensor
      case 'p':
      {
        int FSRReading = analogRead(FSR_Pin); 

        Serial.println(FSRReading);
      
        delay(250); //just here to slow down the output for easier reading
      }
      break;
      
      
//Thermistor
    case 'm':
      {int   ThermistorPin = 1;   //  Analog input pin for thermistor voltage
  int   Vo;                  //  Integer value of voltage reading
  float R = 9870.0;          //  Fixed resistance in the voltage divider
  float logRt,Rt,T;
  float c1 =  1.009249522e-03, c2 = 2.378405444e-04, c3 = 2.019202697e-07;
  Vo = analogRead(ThermistorPin);
  Rt = R*( 1023.0 / (float)Vo - 1.0 );
  logRt = log(Rt);
  T = ( 1.0 / (c1 + c2*logRt + c3*logRt*logRt*logRt ) ) - 273.15;
  Serial.print("  ");  Serial.print(Vo);
  Serial.print("  ");  Serial.print(Rt);
  Serial.print("  ");  Serial.println(T);
  delay(200);
      }
      break;
      
      //Liquid Temperature Sensor
       case 't':
      float temperature = getTemp();
      Serial.println(temperature);
  
       delay(100); //just here to slow down the output so it is easier to read
    break;
    }
    Serial.println(inChar);
  }
  
}

//returns the temperature from one DS18S20 in DEG Celsius
float getTemp(){
  
  byte data[12];
  byte addr[8];

  if ( !ds.search(addr)) {
      //no more sensors on chain, reset search
      ds.reset_search();
      return -1000;
  }

  if ( OneWire::crc8( addr, 7) != addr[7]) {
      Serial.println("CRC is not valid!");
      return -1000;
  }

  if ( addr[0] != 0x10 && addr[0] != 0x28) {
      Serial.print("Device is not recognized");
      return -1000;
  }

  ds.reset();
  ds.select(addr);
  ds.write(0x44,1); // start conversion, with parasite power on at the end

  byte present = ds.reset();
  ds.select(addr);    
  ds.write(0xBE); // Read Scratchpad

  
  for (int i = 0; i < 9; i++) { // we need 9 bytes
    data = ds.read();
  }
  
  ds.reset_search();
  
  byte MSB = data[1];
  byte LSB = data[0];

  float tempRead = ((MSB << smiley-cool | LSB); //using two's compliment
  float TemperatureSum = tempRead / 16;
  return TemperatureSum;
}

What you're doing doesn't seem consistent with this: