Show Posts
Pages: 1 ... 15 16 [17] 18
241  Using Arduino / Programming Questions / Re: Writing from Serial to Char array and then Serial.print(array) not working on: December 31, 2011, 11:52:33 pm
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.
242  Using Arduino / Programming Questions / Re: Writing from Serial to Char array and then Serial.print(array) not working on: December 31, 2011, 11:44:07 pm
Sorry....this is all of the code:

Code:
/*
 * 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;

}
243  Using Arduino / Programming Questions / Writing from Serial to Char array and then Serial.print(array) not working on: December 31, 2011, 10:42:07 pm
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:
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);
}
244  Using Arduino / Programming Questions / Re: simple question about variable creation and initialization in loop() on: October 08, 2011, 03:28:51 pm
Thanks for the replies.  I guess I have always tried to limit the use of global variables only because I had read somewhere that it is poor form to declare lots of global variables.  Of course that was programming a simple application in C# so perhaps that doesn't apply here.
245  Using Arduino / Programming Questions / simple question about variable creation and initialization in loop() on: October 08, 2011, 02:35:02 pm
I am very much an amateur with all things programming related so I am curious about the normal use of the static keyword when declaring a variable.  I have slightly more experience programming with avrstudio in C whereby in my main function I will first declare my variables and then use a 'while' loop to cycle through my code that actually has action items.  I notice that the equivalent process in arduino is to declare my variables all as static to avoid having the loop function create and initialize them on each iteration through my loop.  Is this pretty typical to declare all variables in loop as static if you don't want to reinitialize them every time?  For example it seems that:

Code:
void main()
{
int i = 0;
int j = 10;

while(1)
{
i++;
j--;
}
}

is not equivalent to:

Code:
void loop()
{
int i = 0;
int j = 10;

i++;
j--;

}

but is equivalent to:

Code:
void loop()
{
static int i = 0;
static int j = 10;

i++;
j--;
}

246  Using Arduino / Programming Questions / Re: Writing to SD Card causes MCU to restart after 12 iterations of the loop on: September 03, 2011, 06:25:28 pm
Once again that worked like a charm!  I really appreciate your help!
247  Using Arduino / Programming Questions / Re: Writing to SD Card causes MCU to restart after 12 iterations of the loop on: September 03, 2011, 05:42:22 pm
First off, Nick and Wildbill, your suggestions worked great.  However, I ran into another problem.  Is there any difference between:

Code:
sprintf (buff, "%u,%u", count++, temperature);

and

Code:
sprintf (buff, "%u,%u", temperature, count++);

The reason I ask is that the first method doesn't work.  It adds count++ to the buffer but temperature always gets written to buff as zero such as:

Code:
0,0
1,0
2,0
3,0

The second method works great printing:

Code:
74,0
74,1
74,2
74,3

Here is the whole code segment:

Code:
uint32_t count;
void loop()
{
    uint16_t ADCVal;
   
    uint16_t temperature;
    //String dataString = "";
    char buff[15];
    ADCVal = analogRead(A0);
    temperature = (get_temperature(ADCVal));
    sprintf (buff, "%u,%u", temperature, count++);

It wouldn't be the end of the world to have:

Code:
temperature, count

printed to the SD card but it bugs me as to why one way works and the other doesn't.
248  Using Arduino / Programming Questions / Re: Writing to SD Card causes MCU to restart after 12 iterations of the loop on: September 02, 2011, 08:35:09 am


Yes it does, but the String class uses dynamic memory allocation. This is prone to fragmentation. Especially if you are making counters and things which get larger, it is going to be allocating different sized pieces of memory.

Without checking your code in detail, you are probably better off using a static buffer, eg.

Code:
char buf [20];  // or whatever size you think you need

sprintf (buf, "%i,%i", a, b);  // print two numbers to buf

So would I just do it with:

Code:
sprintf(buf, "%i,%i", count++, temperature);
dataFile.println(buf); //out to sd card
249  Using Arduino / Programming Questions / Re: Writing to SD Card causes MCU to restart after 12 iterations of the loop on: September 02, 2011, 08:23:59 am
Quote
So you said if you replace the SD card writes with Serial prints, it doesnt happen?
When you say reset, does the whole arduino actually reset, or just the count variable?

No, I commented out a few serial prints in order to use less RAM thinking that may be the problem.  If I comment out all references to a "count" variable and just write the temperature to the SD card without a count  I don't have the problem.  Likewise if I don't have the SD card inserted at all (the program skips the code segment dealing with writing to the SD Card) I don't have the problem.

The whole arduino resets.  I thought maybe the writes to the SD card were pulling too much current so I put a check at the beginning of the program to see if the brownout flag was set but that doesn't seem to be the problem.
250  Using Arduino / Programming Questions / Re: Writing to SD Card causes MCU to restart after 12 iterations of the loop on: September 01, 2011, 08:37:12 pm
Doesn't:
Code:
String dataString = "";
release the memory at the beginning of each loop?

It doesn't seem to be creating a longer and longer string each time through the loop because what is being written to the SD card seems to be correct.  If it was appending more and more values to the string it seems like the .txt file would look like this:

Code:
0,74
0,741,74
0,741,742,74
etc. 

How do I insure that the memory taken up by my string is released at the end of each iteration through loop?
251  Using Arduino / Programming Questions / Writing to SD Card causes MCU to restart after 12 iterations of the loop on: September 01, 2011, 05:15:48 pm
I am logging temperature data on an SD Card.  I want to have the arduino write the temperature plus a number that represent the iteration number of the loop so:

Code:
0,74
1,74
2,74
3,74
4,74
5,74
6,74
7,74
8,74
9,74
10,74
11,74
0,74
1,74
2,74
3,74
4,74
5,74
6,74
7,74
8,74
9,74
10,74
11,74

You can see though that it iterates through the loop 12 times and then resets and starts over.  It will do this indefinately.  I thought perhaps I was exceeding RAM so I commented out a few serial.print lines but it still does it.  If I remove the count variable and just write the temperature to the SD card it doesn't reset, or if I just run it without an SD Card it doesn't reset so it is definately something to do with count variable.  Any ideas?  Here is the total code:

Code:
/*
 * 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 <SD.h>

boolean _sd_available = false;
void setup()
{
pinMode(A0,INPUT);
Serial.begin(9600);
        initLCD();
        //check for brownout
          if(MCUSR & (1<<BORF)) {
            LCD_Cursor_Position(84);
            Serial.print("Brownout Reset      ");
            delay(2000);
          }
          //initialize SD card
          LCD_Cursor_Position(84);
          Serial.print("Initializing SD");
          // make sure that the default chip select pin is set to
          // output, even if you don't use it:
          pinMode(10, OUTPUT);
         
          // see if the card is present and can be initialized:
          if (!SD.begin(chipSelect))
          {
            LCD_Cursor_Position(84);
            Serial.println("SD Failed          ");
            _sd_available = false;
          }
         
          else
          {
          LCD_Cursor_Position(84);
          Serial.println("SD Active          ");
          _sd_available = true;
          }
}

uint32_t count;
void loop()
{
    uint16_t ADCVal;
   
    uint16_t temperature;
    String dataString = "";
    ADCVal = analogRead(A0);
    temperature = (get_temperature(ADCVal));
    LCD_Cursor_Position(8);
    Serial.print(temperature);
    Serial.print("    ");
   
    if (_sd_available)
    {
      dataString += String(count++);
      dataString += ",";
      dataString += String(temperature);
     
      // open the file. note that only one file can be open at a time,
      // so you have to close this one before opening another.
      File dataFile = SD.open("logger.txt", FILE_WRITE);
   
       // if the file is available, write to it:
      if (dataFile)
      {
        dataFile.println(dataString);
        dataFile.close();
        // print to the serial port too:
        //Serial.println(dataString);
      } 
      // if the file isn't open, pop up an error:
      else
      {
        LCD_Cursor_Position(84);
        Serial.println("error opening       ");
      }
    }
                       
        delay(500);
}



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
//
//******************************************


uint16_t get_temperature(uint16_t ADCVal){
//temperature in kelvin = beta/ln(R/Rinfinity)
uint16_t beta = 0;
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;

}




void initLCD(){

delay(1000); //wait for LCD to wake up

Serial.print(LCD_Command_B,BYTE);
Serial.print(LCD_Clear_Screen,BYTE); //clear screen
delay(20);
LCD_Cursor_Position(0);
Serial.print("Temp = ");

//LCD_Cursor_Position(64); //cursor move to second line
//Serial.print("ADC = ");

//LCD_Cursor_Position(20); //cursor move to third line
//Serial.print("Beta = ");

}
252  Forum 2005-2010 (read only) / Bugs & Suggestions / Re: Please update OneWire page for DS18B20 on: April 22, 2008, 03:39:44 pm
Quote
temp_frac = temp_raw & 0x0F;

I must say.....that is a really slick way to pull out just the bits that you want and throw out the rest.  I appreciate this.  
253  Forum 2005-2010 (read only) / Bugs & Suggestions / Re: Please update OneWire page for DS18B20 on: April 21, 2008, 09:03:23 pm
Okay I think I understand what most of those lines of code do but can you explain what this line is saying?

temp_frac = temp_raw & 0x0F;

You also have this line but I don't see an integer called "count".

Serial.print(count);

Does this have something to do with the fact that you are using it as a temperature logger?  As in you have an integer called count somewhere in there that increments +1 every iteration of the program to keep track of the temperatures?

Sorry for all of the questions.  I am very green when it comes to any kind of programming but I am catching on slowly.
254  Forum 2005-2010 (read only) / Bugs & Suggestions / Re: Please update OneWire page for DS18B20 on: April 21, 2008, 02:20:23 pm
Excellent.  That worked great.  I did run into the problem of Serial.print dropping off everything after the decimal.  Is there a command similar to Serial.print that doesn't truncate the number?  I put in a few extra lines of code to increase the precision of the number I was seeing but of course there is no decimal.

double tempc, tempf;
tempc = (double)rawtemp / 16;
tempc = tempc * 1000;
tempf = (((tempc / 1000) * 1.smiley-cool + 32.0) * 1000;
    Serial.print("tempc=");
    Serial.print(tempc,DEC);
    Serial.print(" tempf=");
    Serial.print(tempf,DEC);

Now this is the output of my two DS18B20's

tempc=22875 tempf=73175 R=28 9E 91 8C 1 0 0 A6 P=1 6E 1 4B 46 7F FF 2 10  CRC=71

tempc=22875 tempf=73175 R=28 36 71 8C 1 0 0 81 P=1 6E 1 4B 46 7F FF 2 10  CRC=71
255  Forum 2005-2010 (read only) / Bugs & Suggestions / Re: Please update OneWire page for DS18B20 on: April 20, 2008, 04:56:45 pm
Quote
So I am very excited because I plugged a DS18B20 into my arduino and it worked.  It is happily spitting out these values.

R=28 9E 91 8C 1 0 0 A6 P=1 A2 1 4B 46 7F FF E 10  CRC=D8

I know next to nothing about programming and got this to work by simply copying and pasting the code from this page http://www.arduino.cc/playground/Learning/OneWire and changing the family code to 0X28 and adding the one wire library to the arduino programming environment.  The next thing I would like to know is how to write the code to have the arduino convert the hex data above to a readable temperature format (i.e. 35 degrees C or something.....better yet....a farenheit conversion)

Okay I'm starting to figure out the format of the data.  The first two HEX numbers after the P=1 is the LSByte and MSbyte respectively.  So that gives us 1A2 or 418 and converting to Celsius means 418*.0625 = 26.125 degrees celsius.  So the question is how do I write the C code to first pull out those two bytes, switch them around from LSB MSB to MSB LSB and then multiply that number by .0625?  
Pages: 1 ... 15 16 [17] 18