Software reset

Hi,
I’m trying to completely reset the Arduino with software, but can’t quite get it. What is the reset pin called? Reset or reset or 14?

digitalWrite(reset, LOW);

Reset. Bring this line LOW to reset the microcontroller. Typically used to add a reset button to shields which block the one on the board.
:-/

No, you can’t directly perform a hardware reset from software using any digital pin operation. Even if you wire a digital output pin to the reset pin, the output pin will turn off too quick to completely reset the chip and the AVR datasheet says you can’t do it that way. There is a way to do a reset using the watch dog timer, but it’s somewhat complex and may not be compatible with the Arduino bootloader program, at least that’s my take away on past postings.

So why do you need to perform a hardware reset? I think that there is a 99.99% chance that there is another way to structure your program to accomplish what needs to be accomplished.

Why don’t you post your sketch (use the insert code # button) and I’m sure someone here can help you with it.

Lefty

The software reset requires modification to the Arduino bootloader. I have
a writeup here http://tinyurl.com/yzfe74b

If you need to do a software reset you should add an edge triggered
monostable to a digital output. The monostable will guarantee the reset
line stays low long enough to reset.

(* jcl *)


www: http://www.wiblocks.com
twitter: http://twitter.com/wiblocks
blog: http://luciani.org

Thanks for the replies. There probably is a better way to do what I need to but will try the reset jluciani mentions.
I’m trying to get values from Pachube with a WiShield, but can only get them once. I think my memory is full after that and am not sure how to clear it. Ideally I want to get new values every half hour or so.

I think you are better off going with the retrolefty suggestion of posting the code.
If you do have a memory overflow you should determine the source.

(* jcl *)


www: http://www.wiblocks.com
twitter: http://twitter.com/wiblocks
blog: http://luciani.org

Ok here is my code. Any help is appreciated.
I can only get values from Pachube once.

    /*
    * Uses WiServer to put/get pachube feeds via the API
   Thanks to the following people who made this project possible
   Miguel Sanchez for the Arduino mood light code
   Greg Eigsti & Voyager from the Asynclabs boards.
   This project uses 2 Arduino boards and 2 Wishields
   
    */
    #include <WiShield.h>
    #include <WiServer.h>
    #include <MsTimer2.h>
    #define WIRELESS_MODE_INFRA   1
    #define WIRELESS_MODE_ADHOC   2
 #include <stdio.h>

char field [ 64 ];

 // Wireless configuration parameters ----------------------------------------
unsigned char local_ip[] = {192,168,1,2};      // IP address of WiShield
unsigned char gateway_ip[] = {192,168,1,1};      // router or gateway IP address
byte subnet_mask[] = {255,255,255,0};   // your subnet mask for the local network
const prog_char ssid[] PROGMEM    = {"Your Network"};      // max 32 bytes
unsigned char security_type = 0;   // 0 - open; 1 - WEP; 2 - WPA; 3 - WPA2

// WPA/WPA2 passphrase
const prog_char security_passphrase[] PROGMEM = {"xxxxxxxx"};      // max 64 characters
prog_uchar wep_keys[] PROGMEM = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d,      // Key 0
                          0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,      // Key 1
                          0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,      // Key 2
                          0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00      // Key 3
                        };

    // WEP 128-bit keys
    // sample HEX keys
    // setup the wireless mode
    // infrastructure - connect to AP
    // adhoc - connect to another WiFi device
    unsigned char wireless_mode = WIRELESS_MODE_INFRA;
    unsigned char ssid_len;
    unsigned char security_passphrase_len;
    //---------------------------------------------------------------------------
    //global state data
    boolean intTimer;
    boolean gotData;
    int temp1;
    int temp2;
    int sun;
  int x=0;
  int statey =0;
  int resetShield=0;
    unsigned int timercount = 0;
   unsigned int successes = 0;
  // unsigned int failures = 0;

int val;  // use to read a byte from the serial
int state=0;
    int i=0;
    boolean inValidPacket;
    char recvData[400]=" "; 
    // IP Address for Pachube.com 
    uint8 ip[] = {209,40,205,190};
   char hostName[] = "www.pachube.com\nX-PachubeApiKey: "your key"\nConnection: close";
   char url[] = "/api/29.57.csv?_method=put";

GETrequest getPachube(ip, 80, "www.pachube.com", "/api/2957.csv HTTP/1.1\nHost: pachube.com\nX-PachubeApiKey: "Your key"\nConnection: Close\n");
int r=0;
int g=0;
int b=0;

    //MsTimer2 one minute timer ISR
    void timerISR() {
       intTimer = true;
    }

   void printGetData(char* data, int len) {

   //if we are not in the middle of a packet AND we find the OK token
   if(false == inValidPacket && NULL != strstr(data, "HTTP/1.1 200 OK")) {
      //mark that we are in a valid packet
      inValidPacket = true;
      //temporary for debugging purposes
      Serial.println("Data reception started");
   }
   else {
      //temporary for debugging purposes
     // Serial.println("How did we get here?");
   }

   //if we were in a valid packet AND we get the ending 0 == len call
   if(true == inValidPacket && 0 == len) {
      //mark that we are no longer in a valid packet
      inValidPacket = false;

      //grab out the values with something like sscanf()
      // Need to point sscanf to the right place
      
//char *data; //data from the WiShield - set to something like the "re-hydrated" string above
//char *begin;
//char *end;

const char *ptr = recvData;

   int n;
   while ( sscanf(ptr, "%64[^\n]%n", field, &n) == 1 )
  
   {
      printf("field = \"%s\"\n", field);
      Serial.print ("Field = ");
      Serial.println(field);
  
      if(strlen(field) <= 15 && (strlen(field) > 4))
         {
           scan2();   //parse out last bit of data 
             
         }
     
      delay(10);
      ptr += n; /* advance the pointer by the number of characters read */
      if ( *ptr != '\n' )
      {
         break; /* didn't find an expected delimiter, done? */
      }
    while ( *ptr == '\n' )
        {
        ++ptr; /* skip the delimiter */
          }
   }
      //temporary for debugging purposes
      Serial.println("Data reception ended");
      Serial.println("Data string  = ");
      Serial.println(recvData);
      Serial.println(" ");
      Serial.println("Values from sscanf");
      Serial.print("Temp 1 ");
      Serial.println(temp1);
      Serial.print("Temp 2 =  ");
      Serial.println(temp2);
      Serial.print("Sun =  ");
      Serial.println(sun);

      //reset the received data buffer to all 0s - clean up...memset 400
      memset(recvData, 0, 400 * sizeof(char));
   }
   else {
      //just being anal... probably not needed
      memset(recvData, 0, 400 * sizeof(char));
      //stash away the last bit of data received
      memcpy(recvData, data, len);
      //temporary for debugging purposes
      Serial.println("Received data:");
     // Serial.println(data);
   }

}
   
    void setup() {
     
     Serial.begin(57600);
    Serial.println("Connecting...");
   // WiFi.init();
     rgb(5,255,25);// write values
   // gotData=false;
      WiServer.enableVerboseMode(false);
     // Initialize WiServer (we'll pass NULL for the page serving function since we don't need to serve web pages)
      WiServer.init(NULL);
     //init global data; add to, not replace, what is in your setup() function
      inValidPacket = false;
      memset(recvData, 0, 400 * sizeof(char));
      
      // Have the processData function called when data is returned by the server
      getPachube.setReturnFunc(printGetData);
     
      //set up global state data
      intTimer = false;

      //setup the timerISR to be called every xxxxx ms
      MsTimer2::set(20000, timerISR); // 60000ms/1min period
      MsTimer2::start();
       
Serial.println("Connect Timer 20 sec");
}

    void loop()
   {
if(statey==1)
{

   colorchange();
}
      //--------------------------------------------------------------------------------------------
       //handle the timer overflow
       //--------------------------------------------------------------------------------------------
       if(true == intTimer) {  
           //Time has has passed
          intTimer = false;
          Serial.println();
          Serial.println("== Timer fired ==");
          MsTimer2::set(20000, timerISR); // 60000ms/1min period
          MsTimer2::start();
          Serial.print("timer 20 sec");
          Serial.print("\nTotal calls= ");
          Serial.println(++timercount);
          getPachube.submit();
          if (getPachube.isActive()) {   
          Serial.print("\nSuccess calls= ");
          Serial.println(++successes);
          }
       }
            if(successes >=4)
            {
              Serial.println("Timer 1 min");
              successes=0;
               MsTimer2::set(60000, timerISR); // 60000ms/1min period
               MsTimer2::start();
               Serial.println("Reset Connection");
               WiFi.init();
               gotData=false;
            }
            if(gotData==true)
            {
              Serial.println("Timer 1 min");
              successes=0;
               MsTimer2::set(60000, timerISR); // 60000ms/1min period
               MsTimer2::start();
               Serial.println("Reset Connection");
               WiFi.init();
               gotData=false;
            }

      
      // Run WiServer
      WiServer.server_task();

      delay(10);
     
    }
    void scan2()
{

  Serial.println("Parsing..");
  sscanf(field, "%d,%d,%d", &temp1, &temp2, &sun);
  Serial.print("Variable 1-3 = ");
  Serial.println(temp1);
 // Serial.print("Variable 2 = ");
  Serial.println(temp2);
//  Serial.print("Variable 3 = ");
  Serial.println(sun);
  gotData=true;
  statey=1;
   Serial.println("Time 1 min ");
   MsTimer2::set(60000, timerISR); // 60000ms/1min period
  MsTimer2::start();
// memset(field, 0, 100 * sizeof(char));
  
if (temp1<=32)
{

  b=temp1;
  r=(temp2*2+25);
  g=sun;
}
 else if (temp1<=60)
{

  b=temp1*2;
  r=temp2*2;
  g=sun+10;
} 
  
  else
  {

  b=temp1*2+25;
  r=temp2-25;
  g=sun+75; 
  successes=0;
  }
}

void colorchange()

{
  int color,i,value,time=25;  // how long does it take each step
  color=random(1,4);    // which color to dim now
  value=random(temp1,256);  // new value of that color
 /* Serial.print("Value = ");
  Serial.println(value);
    Serial.print ("R = ");
Serial.println(r);
 Serial.print ("B = ");
Serial.println(b); */
  switch(color) {
  case 1:
    if(r>value) for(i=r;i>value;i--) {
      rgb(i,g,b);
      delay(time);
    }
    else for(i=r;i<value;i++) {
      rgb(i,g,b);
      delay(time);
    }
    r=value;
    break;
  case 2:
    if(g>value) for(i=g;i>value;i--) {
      rgb(r,i,b);
      delay(time);
    }
    else for(i=g;i<value;i++) {
      rgb(r,i,b);
      delay(time);
    }
    g=value;
    break;
  case 3:
    if(b>value) for(i=b;i>value;i--) {
      rgb(r,g,i);
      delay(time);
    }
    else for(i=b;i<value;i++) {
      rgb(r,g,i);
      delay(time);
    }
    b=value;
    break;
  }
    rgb(r,g,b);


  delay(100);   // some time before next change
}

void rgb(int r, int g, int b) {
  analogWrite(6,r);
  analogWrite(5,g);
  analogWrite(3,b);
}

Not sure if this is what you are looking for, but I use this code to reset Arduino. I found this somewhere on the forums here.

At the place where you want the reset to occur:

void (*softReset) (void) = 0; //declare reset function @ address 0
softReset();

but I use this code to reset Arduino

That’s not a processor reset.

The difference between a processor reset and a jump through the reset vector is that non of the hardware assumes it’s initial default conditions. However the software / firmware does.

Ok here is my code. Any help is appreciated.

By my count, there are at least 1002 bytes of global data. Most Arduinos have limited SRAM (where global data resides). Because the data is so large, there is a good possibility that the stack is overrunning the data area. If you’re using something like an Arduino Mega, the amount of global data probably isn’t the problem.

Which board are you using?

I am using the duemilanove with a 328 chip.

Which gives you 2K of SRAM. Given the behaviour you’ve described and the limited amount of SRAM available, there’s a very good chance that the stack is bumping into the data area.

Copy get_free_memory into your Sketch…

In setup, call get_free_memory and print the result.

If the value is negative, you have definately run out of memory. If the value is small, there may or may not be a free-memory problem. If the value is large, the problem is probably something else. Unfortunately, it’s difficult to define “small” and “large”. It depends on how many functions call other functions and how many automatic variables are used by each function.

Oh, and get rid of this…

printf("field = \"%s\"\n", field);

Thanks. I’ll give that a try.