Show Posts
Pages: [1]
1  Using Arduino / Interfacing w/ Software on the Computer / Re: Split Serial Port Character Data to EEPROM on: September 24, 2013, 03:21:41 am
Hello Paul....

Thanks a lot for your help. Moving forward to your suggestion here is my final code.. Its heavily amateurish on my part cause i am not a programmer..  Seems to be working now somehow.. please suggest what more can be done to optimize this.

Code:
/*
 Example to receive data from serial port in format <a,123456> and parse it to store in three eeprom location 10,11,12 by receiving as string then converting to integer.
 Maximum number size to be received from serial port is six digits. for longer number add mode Cyclesint , epri and eprs.
 
 */

#define SOP '<'
#define EOP '>'
#include <EEPROM.h>

bool started = false;
bool ended = false;

int     commaPosition;
String  text;
char inData[10]; //Complete data recieved from Serial port
byte index;
String cyclesStr; // Value part of the recieved string
String command;  // Command part of the recieved string
int Cyclesint; //First two characters of value integer to be stored in EEPROM location 10
int Cyclesint1;//First two characters of value integer to be stored in EEPROM location 11
int Cyclesint2;//First two characters of value integer to be stored in EEPROM location 12
long int epri1;//EEPROM location 10 Value
long int epri2;//EEPROM location 11 Value
long int epri3;//EEPROM location 12 Value
long int eepr; //Concatinated integer from 10~12 EEPROM locations
String eprs1; //First two characters of value string
String eprs2; //Next two characters of value string
String eprs3; //Last two characters of value string


void setup()
{
  Serial.begin(9600);

  epri1 = EEPROM.read(10);
  epri2 = EEPROM.read(11);
  epri3 = EEPROM.read(12);
  eepr = epri1*10000+epri2*100+epri3; // Numbers Concatenation

  Serial.println(eepr); //Print final number to be read from eeprom after concatenation of integers
}

void loop()
{
  // Read all serial data available, as fast as possible
  while(Serial.available() > 0)
  {
    char inChar = Serial.read(); //RAW serial DATA
    if(inChar == SOP)
    {
      index = 0;
      inData[index] = '\0';
      started = true;
      ended = false;
    }
    else if(inChar == EOP)
    {
      ended = true;
      break;
    }
    else
    {
      if(index < 79)
      {
        inData[index] = inChar;
        index++;
        inData[index] = '\0';
      }
    }
  }


  if(started && ended) // The end of packet marker arrived. Process the packet
  {
    text = inData;
    String  message = text; // holds text not yet split
    // the position of the next comma in the string


    do
    {
      commaPosition = message.indexOf(',');
      if(commaPosition != -1)
      {
        command = message.substring(0,commaPosition) ;
        message = message.substring(commaPosition+1, message.length());
      }
      else
      {  // here after the last comma is found
        if(message.length() > 0)
          cyclesStr = message ;

      }
    }
    while(commaPosition >=0);


    eprs1 = cyclesStr.substring(0,2) ; //parse first two charachters
    eprs2 = cyclesStr.substring(2,4) ; //parse next two charachters
    eprs3 = cyclesStr.substring(4,6) ; // parse last two charachters

    Serial.println(command);
    Serial.println(cyclesStr);

    char tarray[3];
    eprs1.toCharArray(tarray, sizeof(tarray));
    Cyclesint = atoi(tarray); 

    char tarray1[3];
    eprs2.toCharArray(tarray1, sizeof(tarray));
    Cyclesint1 = atoi(tarray1); 

    char tarray2[3];
    eprs3.toCharArray(tarray2, sizeof(tarray));
    Cyclesint2 = atoi(tarray2); 

    EEPROM.write(10,Cyclesint); //Write first two characters to location 10
    EEPROM.write(11,Cyclesint1); //Write next two characters to location 11
    EEPROM.write(12,Cyclesint2); //Write last two characters to location 12

      delay(100); //delay for EEPROM write

    // Reset for the next packet
    started = false;
    ended = false;
    index = 0;
    inData[index] = '\0';
  }
}


Data needs to be sent as <command,123456>
2  Using Arduino / Interfacing w/ Software on the Computer / Re: Split Serial Port Character Data to EEPROM on: September 23, 2013, 07:32:21 am
Hello Paul,

Thanks for your reply and suggestion. And yes you are right nothing is simple if u get too deep into it .

Sorry i didnt post it earlier . Here is the code i am trying.

Code:
#include <string.h>

#define MAX_STRING_LEN  10

char *record1;
char *p, *i;

void setup()
{

  Serial.begin(9600);
}

void loop () {
  if(Serial.available());
  {

    while (Serial.available() > 0)
    {
      char string = Serial.read();

      *record1 = string;
      Serial.println(subStr(record1, " ", 1));
      Serial.println(subStr(record1, " ", 2));
    }
    
  }
}



// Function to return a substring defined by a delimiter at an index
char* subStr (char* str, char *delim, int index) {
  char *act, *sub, *ptr;
  static char copy[MAX_STRING_LEN];
  int i;

  // Since strtok consumes the first arg, make a copy
  strcpy(copy, str);

  for (i = 1, act = copy; i <= index; i++, act = NULL) {
    sub = strtok_r(act, delim, &ptr);
    if (sub == NULL) break;
  }
  return sub;

}

if i put @a,500000/ is input, I get all the characters in different lines. One more thing. I am not writing anything to eeprom right now as it can be done once i split the strings completely. Thanks again
3  Using Arduino / Interfacing w/ Software on the Computer / Split Serial Port Character Data to EEPROM on: September 23, 2013, 06:47:49 am
Hello,

I am making a controller for switching of and off a relay . The other part is working fine but am stuck at something . I need to receive data from serial port and split is into two variable. like @a,500000/. First is a string and the second is a long integer. @ is to tell arduino to read new command and / is command termination character

After reading the data from the string i need to store the value 500000 to EEPROM. "a" is the command string which needs to tell arduino to store variable to a particular address. If arduino  receive @b,500000/ the data needs to be stored in other address to EEPROM. Address of eeproms can be configured to anything. Data value will be six digits always.

I have been searching and googling for hours and hours trying various example but cant get anything to work. Can anyone  suggest a simple solution.

4  Using Arduino / Networking, Protocols, and Devices / Re: Sending Data through Xbee+Arduino to PC on: March 22, 2013, 02:47:02 am
Hello Paul,
Thanks for looking into my problem. I think I was not very clear in explaining. So here it is again . The receiving arduino do get the data through Xbee and it displays values perfectly on the LCD( Attached PIC). I also need to receive the data on a PC attached to the receiving arduino through its USB/Serial port.

When i try to use the serial monitor the display on LCD vanishes and the serial monitor displays garbled values. I thing the Serial.print(s) is sending back the data to the XBEE  as both the DO and DI LED starts blinking on the XBEE SHIELD.

You are right the Xbee don't know the PC is talking to it. Unfortunately my coding is very primitive and I do not know how to do both the things at the same time, ie receive data and display on LCD as well as put data out for PC to read it .

I have used X-CTU to configure both the Xbees to 9600 Baud rate.

5  Using Arduino / Networking, Protocols, and Devices / Sending Data through Xbee+Arduino to PC on: March 21, 2013, 12:42:01 am
Hello,

I have an application where I am using a MCP3421 18bit ADC to read analog data. The setup is Xbee+Xbee Sheild+Arduino + MCP3421 as Transmitter.   This I am reading and transmitting to a remote xbee+arduino module with LCD. The data is displayed fine on the LCD.  however I want to receive the data on the Serial port. When i try tp Do a Serial.println(s); on the receiving code the data which i get on serial port is garbled. Would appreciate any help

Here is my Code

Transmitting

Code:
#include  <Wire.h>
#define TRUE 1
#define FALSE 0
void setup(void)
{
  Serial.begin(9600);
  Wire.begin();
  delay(100);
  Serial.println(">>>>>>>>>>>>>>>>>>>>>>>>");  // just to be sure things are working

}

void loop(void)
{

  byte address, Hi, Lo, Config;
  int ADVal;

  while(1)
  {
    address = 0x68;
    Wire.beginTransmission(address);
    Wire.write(0x88);   // config register %1000 1000
    // /RDY = 1, One Conversion, 15 samples per, PGA = X1

    Wire.endTransmission();

    delay(1);
    Wire.requestFrom((int)address, (int) 3);
    Hi = Wire.read();
    Lo = Wire.read();
    Config = Wire.read();
    Wire.endTransmission();

    ADVal = Hi;
    ADVal = ADVal * 256 + Lo;

    Serial.print("<"); // Sending a marker for value start
    Serial.print(ADVal);
    Serial.print(">"); // sending a marker for value end

    delay(10);
  }
}

and this is the receiving code

Code:
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // initialize the library with the numbers of the interface pins
bool started = false;
bool ended= false;
char inData[10]; // Leave plenty of room
byte index;
float i;

//char inData[24]; // Or whatever size you need
//byte index = 0;

void setup(){
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  // initialize the serial communications:
  Serial.begin(9600);
}

void loop()
{
  //Serial.println(s);
  while(Serial.available() > 0)
  {
    char aChar = Serial.read();
    if(aChar == '<')
    {
      // Start of packet marker read
      index = 0;
      inData[index] = '\0'; // Throw away any incomplete packet
      started = true;
      ended = false;
    }
    else if(aChar == '>')
    {
      // End of packet marker read
      ended = true;
      break; // Done reading serial data for now
    }
    else
    {
      if(index < 10) // Make sure there is room
      {
        inData[index] = aChar; // Add char to array

        index++;
        inData[index] = '\0'; // Add NULL to end

      }
    }
  }

  // When we get here, there is no more serial data to read,
  // or we have read an end-of-packet marker
  if(started && ended)
  {
    // We've seen both markers - do something with the data here
    lcd.setCursor(0,0);
    i = atoi (inData);
    float s = (i * 0.3051851); //multiplying with calibration factor
    float value = ( s / 1000 );

    lcd.setCursor(1,0);
    lcd.print(value,3); // print value after multiplying with calibration factor to LCD

    lcd.setCursor(0,1 );
    lcd.print(i); // Print raw ADC counts as recieved from transmitter

    index = 0;
    inData[index] = '\0';
    started = false;
    ended = false;
    
  }
}

What I am doing here is reading the MCP3421 on I2C bus and transmitting the raw counts. with a start and end marker  as <DATA> This on recieving I am parsing to DATA enabling me to reject and incomplete data value .

Thanks
6  Using Arduino / Displays / Re: Serial to LCD - How to display second line on 16 x 2 on: August 04, 2012, 02:56:21 pm
Hello,

Wow! Thanks for the info. I will try that and will let you know how it worked. smiley

 smiley
7  Using Arduino / Displays / Re: Serial to LCD - How to display second line on 16 x 2 on: July 18, 2012, 08:35:05 am
Hello Don,

Thanks you for your suggestion. Here is the output from my 16x2 LCD ( Sorry of bad camera quality of my blackberry)

I think i got the idea of how it can be done when sending data from my arduino. But when i am sending data from my serial port the thing get truncated. Anyways i will try this code in that as well and will post the result.
Thanks again.
8  Using Arduino / Displays / Serial to LCD - How to display second line on 16 x 2 on: July 15, 2012, 09:33:32 pm
I am using the standard serial display example in arduino to write a string on lcd. However if the string is longer then 16 chars, the rest seems truncated. Also it does not wrap to second line.

Can anyone suggest.

Arduino V 1.0.1 | Arduino UNO | Standard LCD library
9  Using Arduino / Displays / Re: how to refresh lcd properly on: July 15, 2012, 09:15:13 pm
I guess you are trying to make a Data Logger. I had a similar problem. Here is what i did.

Fix the position of the value and the counter on the LCD before you print the data to the LCD. In  your case it will be
lcd.setCursor(col,rows);

lcd.clear();
lcd.setCursor(0,0);
lcd.print(val);
lcd.setCursor(5,0);
lcd.print(counter);

Quote
and can i make it somehow look like always _0.00 instead of 0.00 and moving all chars one space to the right after it reaches 10?

for this i added a trailing zero in case the value is less then 10. Use if-else statement

lcd.print("0"); \\ if value less then 10
lcd.print("");    \\ if value is more then 10

I am a beginner myself so i am sure this is a really crude method and lot of people will suggest more efficient coding but well , we all are learning ..aint we...
10  Using Arduino / Programming Questions / Re: Arduino Uno Start/Stop from Computer on: May 10, 2011, 02:02:58 am
Thank you for your help. I will try that.
11  Using Arduino / Programming Questions / Re: Arduino Uno Start/Stop from Computer on: May 10, 2011, 01:56:58 am
smiley Well at-least u noticed the Shift key . Sorry for that . Here is the code.

const int numReadings = 10;

int readings[numReadings];      // the readings from the analog input
int index = 0;                  // the index of the current reading
int total = 0;                  // the running total
int average = 0;                // the average

int inputPin = A0;

void setup()
{
  // initialize serial communication with computer:
  Serial.begin(19200);                   
  // initialize all the readings to 0:
  for (int thisReading = 0; thisReading < numReadings; thisReading++)
    readings[thisReading] = 0;         
}

void loop() {
  // subtract the last reading:
  total= total - readings[index];         
  // read from the sensor: 
  readings[index] = analogRead(inputPin);
  // add the reading to the total:
  total= total + readings[index];       
  // advance to the next position in the array: 
  index = index + 1;                   

  if (index >= numReadings)             
    // ...wrap around to the beginning:
    index = 0;                           

  // calculate the average:
  average = total / numReadings;         
  Serial.println(average, DEC);               
}

This is the sample sketch from  smoothing example in arduino
12  Using Arduino / Programming Questions / Re: Arduino Uno Start/Stop from Computer on: May 10, 2011, 01:48:24 am
Very Well.

I need to Start and Stop Data Transmission from Arduino UNO from a PC. I am using the Smoothing Example right now. This is continuously polling the data on the USB Port. Is there a way I can Stop this Transmission when I send a command from PC? Also I would require to Start the transmission back on when i send another command.

Thanx
13  Using Arduino / Programming Questions / Arduino Uno Start/Stop from Computer on: May 10, 2011, 01:40:00 am
Hello Everyone,

I am new to micro-controller development. I am designing a product using UNO. In this I need to start /Stop analog read from Labview. I can manage Labview very well but i have not much idea about how to program UNO for that.

Right now i am able to read data continuously transmitted from UNO. But I am unable to Stop the transmission from Computer.

Can anyone suggest the code for such an application.

Thankx
Pages: [1]