Show Posts
Pages: 1 ... 16 17 [18] 19
256  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.
257  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
258  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.
259  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?
260  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 = ");

}
261  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.  
262  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.
263  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
264  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?  
265  Forum 2005-2010 (read only) / Bugs & Suggestions / Re: Please update OneWire page for DS18B20 on: April 20, 2008, 01:04:09 pm
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)
266  Forum 2005-2010 (read only) / Syntax & Programs / Re: Creating AC from DC using PWM on: July 29, 2008, 08:56:56 pm
Quote
You can get PWM frequencies much faster than 490 Hz.  Look at the ATmega168 datasheet or search this forum for posts I've made about generating PWMs with arbitrary frequencies (searching for posts by bens containing "PWM frequency" should probably bring them up).

As far as what you're planning on doing, I'm not sure how you expect to get a PWM pin to output a negative voltage.  I assume your plan is to use MOSFETs or transistors to deliver the current to the motor (since the mega168 most likely cannot deliver what will be needed), and I guess you will be using a negative regulator to generate the negative half of the sine wave?

- Ben


Yes the plan would be to use the arduino to handle the switching and use transistors to handle the actual current.  I am very novice with this so the plan was to try it out on a small 12V AC motor first so that I don't kill myself playing with the full 120V outlets.
267  Forum 2005-2010 (read only) / Syntax & Programs / Re: Creating AC from DC using PWM on: July 28, 2008, 01:29:45 pm
Sure.  I am looking to control the speed of an AC motor (low power....far less than 1 amp) and I want the arduino to control the speed.  With an AC motor I know that the speed is really a function of the frequency of the AC and the number of poles (windings).  I want to vary the frequency and voltage to slow the motor down or speed it up.  I have read that you can create a pseudo sine wave with pulse width modulation.  In other words the duty cycle would be low where the sine wave crosses zero volts and then increase as the sine wave reaches its apex and then decrease back through 0 volts and then increase again (except using a negative voltage (possibly on a different pin) as the sine wave reaches its trough and then decreases again through 0 volts.  I probably don't need to break up the sine wave into 40 steps to achieve what I am trying to do but I was thinking higher resolution would create a closer approximation of a sine wave.
268  Forum 2005-2010 (read only) / Syntax & Programs / Creating AC from DC using PWM on: July 27, 2008, 06:21:27 pm
I am trying to use PWM to create an AC current from a DC source.  I was hoping to use about 40 steps per cycle.  The issue I am thinking about is that a standard wall outlet is 60 HZ so if I use 40 steps per HZ that will equal 2400 total steps and the PWM pins on the Arduino operate at 490 HZ.  Correct me if I am wrong but the program will be incrementing about 5 steps for every pulse that comes out of the PWM pins.  Do I just have to use less steps or is there a trick that I can use to get the frequency of the PWM signal up?
269  Forum 2005-2010 (read only) / Syntax & Programs / Re: attachInterrupt question on: July 21, 2008, 09:25:04 am
That looks pretty good.  I'm gonna try it.  Thanks!
270  Forum 2005-2010 (read only) / Syntax & Programs / attachInterrupt question on: July 20, 2008, 09:34:45 pm
I am using a DS18B20 temperature sensor to display temperature data onto a serial LCD.  I have a simple push button that I use to toggle back and forth between celsius and farenheit.  I am using the attachInterrupt command to read the push button.  My problem is that the pushbutton seems to need some sort of debounce because occassionally it will toggle from celsius to farenheit and back to celsius on one push.  Would the debounce routine go somewhere in the function that is called by the interrupt?  
Pages: 1 ... 16 17 [18] 19