Go Down

Topic: Gobetwino code for temperature measurement (Read 5183 times) previous topic - next topic

MikMo

No you probably also commented out the serial statements that send GoBetwino commands.

Can you post your code as it is now, so i can take a look at it.

golfmon

Here is the code
Code: [Select]

// TCMux shield demo by Ocean Controls
// Sends the data to a serial terminal
// Type @NS1<cr> (don't type <cr> it's the carriage return character, just hit enter) to set the number of sensor to 1
// Type @NS8<cr> to set the number of sensors to 8
// Type @UD1<cr> to set the update delay to 1 second
// Type @SV<cr> to save the number of sensors and update delay variables to eeprom



#include <string.h> //Use the string Library
#include <ctype.h>
#include <EEPROM.h>

#define PINEN 7 //Mux Enable pin
#define PINA0 4 //Mux Address 0 pin
#define PINA1 5 //Mux Address 1 pin
#define PINA2 6 //Mux Address 2 pin
#define PINSO 12 //TCAmp Slave Out pin (MISO)
#define PINSC 13 //TCAmp Serial Clock (SCK)
#define PINCS 9  //TCAmp Chip Select Change this to match the position of the Chip Select Link

unsigned int Temp[8], SensorFail[8];
unsigned int Mask;
//char data[16];
char i, j, NumSensors =1, UpdateDelay;
char Rxchar, Rxenable, Rxptr, Cmdcomplete, R;
char Rxbuf[15];
char adrbuf[3], cmdbuf[3], valbuf[12];
int val = 0, Param;     
unsigned long time;
int serInLen = 25;
char serInString[25];
void setup()   
{     
  Serial.begin(9600); 
// Serial.println("TCMUX");
  if (EEPROM.read(511)==1)
  {
    NumSensors = EEPROM.read(0);
    UpdateDelay = EEPROM.read(1);
  }
  pinMode(PINEN, OUTPUT);     
  pinMode(PINA0, OUTPUT);   
  pinMode(PINA1, OUTPUT);   
  pinMode(PINA2, OUTPUT);   
  pinMode(PINSO, INPUT);   
  pinMode(PINCS, OUTPUT);   
  pinMode(PINSC, OUTPUT);   
 
  digitalWrite(PINEN, HIGH);   // enable on
  digitalWrite(PINA0, LOW); // low, low, low = channel 1
  digitalWrite(PINA1, LOW);
  digitalWrite(PINA2, LOW);
  digitalWrite(PINSC, LOW); //put clock in low
 
Serial.println("#S|CPTEST|[]#");
readSerialString(serInString,1000);
}

void loop()                     
{
  if (millis() > (time + ((unsigned int)UpdateDelay*1000)))
  {
    time = millis();
    //for(j=0;j<NumSensors;j++)
    //{
    if (j<(NumSensors-1)) j++;
    else j=0;
     
      switch (j) //select channel
      {
        case 0:
          digitalWrite(PINA0, LOW);
          digitalWrite(PINA1, LOW);
          digitalWrite(PINA2, LOW);
        break;
        case 1:
          digitalWrite(PINA0, HIGH);
          digitalWrite(PINA1, LOW);
          digitalWrite(PINA2, LOW);
        break;
        case 2:
          digitalWrite(PINA0, LOW);
          digitalWrite(PINA1, HIGH);
          digitalWrite(PINA2, LOW);
        break;
        case 3:
          digitalWrite(PINA0, HIGH);
          digitalWrite(PINA1, HIGH);
          digitalWrite(PINA2, LOW);
        break;
        case 4:
          digitalWrite(PINA0, LOW);
          digitalWrite(PINA1, LOW);
          digitalWrite(PINA2, HIGH);
        break;
        case 5:
          digitalWrite(PINA0, HIGH);
          digitalWrite(PINA1, LOW);
          digitalWrite(PINA2, HIGH);
        break;
        case 6:
          digitalWrite(PINA0, LOW);
          digitalWrite(PINA1, HIGH);
          digitalWrite(PINA2, HIGH);
        break;
        case 7:
          digitalWrite(PINA0, HIGH);
          digitalWrite(PINA1, HIGH);
          digitalWrite(PINA2, HIGH);
        break;
      }
     
      delay(5);
      digitalWrite(PINCS, LOW); //stop conversion
      delay(5);
      digitalWrite(PINCS, HIGH); //begin conversion
      delay(250);  //wait 250 ms for conversion to complete
      digitalWrite(PINCS, LOW); //stop conversion, start serial interface
      delay(1);
      Temp[j] = 0;
   
      for (i=15;i>=0;i--)
      {
          digitalWrite(PINSC, HIGH);
          delay(1);
         
          /*if (digitalRead(PINSO)==1)
          {
            //Temp[j] += Mask;
            Serial.print("1");
          }
          else
          {
            Serial.print("0");
          }*/
        if ((i<15) && (i>2))
        {
         
          Mask = 1<<(i-3);
          if (digitalRead(PINSO)==1)
          {
            Temp[j] += Mask;
            //Serial.print("1");
          }
          else
          {
           // Serial.print("0");
          }
        }
        if (i==2)
        {
          SensorFail[j] = digitalRead(PINSO);
        }
        digitalWrite(PINSC, LOW);
        delay(1);
        //delay(1);
      }
      //Serial.println();
   
      //Serial.println(Temp,BIN);
      //Serial.print("#");
      //Serial.print(j+1,DEC);
     // Serial.print(": ");
      if (SensorFail[j] == 1)
      {
     //   Serial.println("FAIL");
      }
      else
      {
      //  Serial.print(Temp[j]>>2);
     //   Serial.print(".");
        if ((Temp[j] & 0b11) == 0b00)
        {
       //   Serial.print("00");
        }
        if ((Temp[j] & 0b11) == 0b01)
        {
        //  Serial.print("25");
        }
        if ((Temp[j] & 0b11) == 0b10)
        {
       //   Serial.print("50");
        }
        if ((Temp[j] & 0b11) == 0b11)
        {
        //  Serial.print("75");
        }
      //  Serial.println(176,BYTE);
      //}
        //delay(1000);
    }//end reading sensors
  }//end time
  if (Serial.available() > 0)    // Is a character waiting in the buffer?
  {
    Rxchar = Serial.read();      // Get the waiting character

    if (Rxchar == '@')      // Can start recording after @ symbol
    {
      if (Cmdcomplete != 1)
      {
        Rxenable = 1;
        Rxptr = 1;
      }//end cmdcomplete
    }//end rxchar
    if (Rxenable == 1)           // its enabled so record the characters
    {
      if ((Rxchar != 32) && (Rxchar != '@')) //dont save the spaces or @ symbol
      {
        Rxbuf[Rxptr] = Rxchar;
        //Serial.println(Rxchar);
        Rxptr++;
        if (Rxptr > 13)
        {
          Rxenable = 0;
        }//end rxptr
      }//end rxchar
      if (Rxchar == 13)
      {
        Rxenable = 0;
        Cmdcomplete = 1;
      }//end rxchar
    }//end rxenable

  }// end serial available


   
  if (Cmdcomplete == 1)
  {
    Cmdcomplete = 0;
     cmdbuf[0] = toupper(Rxbuf[1]); //copy and convert to upper case
     cmdbuf[1] = toupper(Rxbuf[2]); //copy and convert to upper case
     cmdbuf[2] = 0; //null terminate        Command = Chr(rxbuf(3)) + Chr(rxbuf(4))
     //   Command = Ucase(command)
  //Serial.println(cmdbuf);
     valbuf[0] = Rxbuf[3]; //        Mystr = Chr(rxbuf(5))
        R = Rxptr - 1;
            for (i = 4 ; i <= R ; i++)//For I = 6 To R
            {
                valbuf[i-3] = Rxbuf[i]; //Mystr = Mystr + Chr(rxbuf(i))
            }
     valbuf[R+1] = 0; //null terminate
     Param = atoi(valbuf);//   Param = Val(mystr)

     //Serial.println(Param); //   'Print "Parameter: " ; Param

       
              if (strcmp(cmdbuf,"NS")==0)       //NumSensors
              {
                   //'Print "command was ON"
                   if ((Param <= 8) && (Param > 0))
                   {
                      NumSensors = Param;                   
                   }
                   
              }
              if (strcmp(cmdbuf,"UD")==0)       //UpdateDelay
              {
                   //'Print "command was ON"
                   if ((Param <= 60) && (Param >= 0))
                   {
                      UpdateDelay = Param;                   
                   }
                   
              }
              if (strcmp(cmdbuf,"SV")==0)       //Save
              {
                   EEPROM.write(0,NumSensors);
                   EEPROM.write(1,UpdateDelay);
                   EEPROM.write(511,1);
              }
         }
}


void logData( int Temp)
{
   char buffer[5];
 
   Serial.print("#S|GOGETTEMP|[");
   Serial.print(itoa((Temp), buffer, 10));
   Serial.print(";");
      Serial.println("]#");
   readSerialString(serInString,1000);
   // There ought to be a check here for a non 0 return value indicating an error and some error handeling
}


void readSerialString (char *strArray,long timeOut)
{
   long startTime=millis();
   int i;

   while (!Serial.available()) {
      if (millis()-startTime >= timeOut) {
         return;
      }
   }
   while (Serial.available() && i < serInLen) {
      strArray[i] = Serial.read();
      i++;
   }
}



Thank you so much for helping

MikMo

I will look it over later today, got a score some "daddy points" first :-)

MikMo

Hi golfmon.

I just took a quick look at the code.  There are serveral problems.

First:
you did not remove all the print statements that is not related to GoBetwino command, a few are still in there.

Second:  this section of code  :

Code: [Select]
if (Serial.available() > 0)    // Is a character waiting in the buffer?
  {
    Rxchar = Serial.read();      // Get the waiting character

    if (Rxchar == '@')      // Can start recording after @ symbol
    {
      if (Cmdcomplete != 1)
      {
        Rxenable = 1;
        Rxptr = 1;
      }//end cmdcomplete
    }//end rxchar
    if (Rxenable == 1)           // its enabled so record the characters
    {
      if ((Rxchar != 32) && (Rxchar != '@')) //dont save the spaces or @ symbol
      {
        Rxbuf[Rxptr] = Rxchar;
        //Serial.println(Rxchar);
        Rxptr++;
        if (Rxptr > 13)
        {
          Rxenable = 0;
        }//end rxptr
      }//end rxchar
      if (Rxchar == 13)
      {
        Rxenable = 0;
        Cmdcomplete = 1;
      }//end rxchar
    }//end rxenable

  }// end serial available




Expects to read a @ char s from the serial port but where will it come from?  GoBetwino can not send it, and as i said before you can not use the serial monitor or any other program to send data to Ardino at the same time as you use GoBetwino, so the @ will never be read.


Thirdly you declared a function LogData, but you never call it, so it will never send the GoBetwino command to log the data.

I think you need to rewrite the code so it dosen't expect to read data from the serial port, except answers coming from GoBetwino.




golfmon

Hello MikMo

i will revise those code and try what u suggested then tell u the result

thx so much

Go Up