Go Down

Topic: Writing from Serial to Char array and then Serial.print(array) not working (Read 1 time) previous topic - next topic

jerseyguy1996

I have written a routine that is executed when serial data is available.  First it waits to make sure all bytes have been received and then it writes those bytes to a character array.  For debugging I wanted to then serially print that data back to the serial monitor to make sure that the data was coming in correctly.  When I execute the code I just get junk back on the Serial monitor.  I hope someone can tell me what I might be doing wrong.  My code is below:

Code: [Select]
void receive_serial()
{
  int i = Serial.available();
  delay(10);
 
  //wait to make sure that all bytes have been received
  while(i != Serial.available())
  {
    i = Serial.available();
    delay(10);
  }
 
  char myString[Serial.available()];
 
 
  while(Serial.available())
  {
    int j = 0;
    myString[j] = Serial.read();
    ++j;
     
  }
 
  Serial.print(myString);
}
Arduino Uno;
Mega328

WizenedEE

That doesn't compile and not just because of the lack of setup() and loop() (the compiler can't find the length of myString)

Anyway, your problem is that you're Serial.print()ing the pointer to the start of the array without saying how long it is. The second argument to print when you're trying to do that is the number of characters to print.

jerseyguy1996

Sorry....this is all of the code:

Code: [Select]

/*
* BBQControl.c
*
* Created: 8/7/2011 12:34:32 PM
*  Author: Saleem and Leslie
*/



#define LCD_Command_A 0x7C
#define LCD_Command_B 0xFE
#define LCD_Clear_Screen 0x01
#define LCD_Cursor_Position(a) {Serial.print(LCD_Command_B,BYTE); Serial.print(a+128,BYTE);}
#define chipSelect 10

#include <math.h>
#include <stdint.h>
#include <PID_v1.h>
#include <stdio.h>

//PID Variables
double Input = 0;  //Our Current Temperature
double Output = 0;  //Our Control Variable
double Setpoint = 0;  //Our Setpoint
double Kp = 10;      //Proportional
double Ki = .1;      //Integral
double Kd = 50;      //Derivative

PID myPID(&Input, &Output, &Setpoint, Kp, Ki, Kd, DIRECT);
 
void setup()
{
pinMode(A0,INPUT);
Serial.begin(9600);
        myPID.SetMode(AUTOMATIC);
}       

uint32_t count;


void loop()
{
    uint16_t ADCVal;
    uint16_t temperature;
    //String dataString = "";
   
    ADCVal = analogRead(A0);
   
    temperature = (get_temperature(ADCVal));
   
    for(int i = 0;i<98;i++)
    {
      ADCVal = analogRead(A0);
      temperature += (get_temperature(ADCVal));
    }
   
    temperature = temperature/100;
    Serial.println(temperature);
    Input = (double)temperature;
    myPID.Compute();
    Serial.println(Input);
    Serial.println(Output);
   
   if(Serial.available())
    {
      receive_serial();
    }
   
   
     
    delay(1000);
}

void receive_serial()
{
  int i = Serial.available();
  delay(10);
 
  //wait to make sure that all bytes have been received
  while(i != Serial.available())
  {
    i = Serial.available();
    delay(10);
  }
 
  char myString[Serial.available()];
 
 
  while(Serial.available())
  {
    int j = 0;
    myString[j] = Serial.read();
    ++j;
     
  }
 
  Serial.print(myString);
}

void get_parameters(uint16_t ADCvalue, uint16_t *beta, float *r_infinity)
{

if      (ADCvalue < 15)   {*beta = 5191; *r_infinity=0.07028;}
else if (ADCvalue <= 39)  {*beta = 5085; *r_infinity=0.08597;}
else if (ADCvalue <= 158) {*beta = 4942; *r_infinity=0.11637;}
else if (ADCvalue <= 530) {*beta = 4767; *r_infinity=0.17766;}
else if (ADCvalue <= 577) {*beta = 4671; *r_infinity=0.23249;}
else if (ADCvalue <= 670) {*beta = 4641; *r_infinity=0.25289;}
else if (ADCvalue <= 757) {*beta = 4604; *r_infinity=0.28168;}
else if (ADCvalue <= 832) {*beta = 4566; *r_infinity=0.31618;}
else if (ADCvalue <= 892) {*beta = 4526; *r_infinity=0.35779;}
else if (ADCvalue <= 938) {*beta = 4485; *r_infinity=0.40755;}
else if (ADCvalue <= 1023){*beta = 4453; *r_infinity=0.45243;}
return;
}


//*************Hardware SetUp***************
//
//     ADC Input
//        |
//        Rpad    |  Rtherm
// Vcc--------/\/\/\------/\/\/\------ground
//
//******************************************


float get_temperature(uint16_t ADCVal){
//temperature in kelvin = beta/ln(R/Rinfinity)
uint16_t beta = 0;
        uint8_t servo_command;
float Ri = 0;
float Rpad = 100000;
float Rtherm = Rpad/(1-((float)ADCVal/1024.0)) - 100000;
float Temperature;
float t;

get_parameters(ADCVal, &beta, &Ri);

t=beta/log(Rtherm/Ri);
t = ((9.0/5.0)*(t-273))+32;
Temperature = t;


//Out to LCD
//LCD_Cursor_Position(72);
//Serial.print(ADCVal);
        //Serial.print("    ");
//LCD_Cursor_Position(28);
        //Serial.print(beta);
        //Serial.print("    ");
//LCD_Cursor_Position(8);
return Temperature;

}
Arduino Uno;
Mega328

jerseyguy1996

That makes sense that the compiler needs to know how much memory to allocate to myString but is there any way to do it on the fly?  I won't know before hand how many bytes are going to be sent.  It will be comma delimited data that I will need to break up into separate variables to update my Setpoint, Kp, Ki, and Kd variables.
Arduino Uno;
Mega328

WizenedEE


That makes sense that the compiler needs to know how much memory to allocate to myString but is there any way to do it on the fly?  I won't know before hand how many bytes are going to be sent.  It will be comma delimited data that I will need to break up into separate variables to update my Setpoint, Kp, Ki, and Kd variables.


Ok, I guess that does compile, but I have no idea why. The general way to do it is to make the char array as big as the biggest set of data you'll need and then go from there. The String class does dynamically allocate memory, but that can lead to fragmentation and bad stuff.

Go Up