Reset Button = restart code?

Please tell us which board you are using.
For CircuitPython issues, ask in the Adafruit CircuitPython forum.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
SkyRyder
 
Posts: 45
Joined: Fri Mar 06, 2020 6:05 pm

Reset Button = restart code?

Post by SkyRyder »

Hi all, with various example code projects for my metro, I have found that pressing the reset button causes the code to either resume or restart (I assume restart)... which is awesome.

However, with my own project... this behavior is not happening. If I press reset, no code runs.

My goal/intent is to have my project survive a reset, but ESPECIALLY a power-cycle.

Any help appreciated.

User avatar
dastels
 
Posts: 15665
Joined: Tue Oct 20, 2015 3:22 pm

Re: Reset Button = restart code?

Post by dastels »

What programming language are you using?

Can you post your code?

Dave

User avatar
SkyRyder
 
Posts: 45
Joined: Fri Mar 06, 2020 6:05 pm

Re: Reset Button = restart code?

Post by SkyRyder »

Hi Dave... yessir!

all in c

Code: Select all

#include "Adafruit_MCP9601.h"       //for thermocouple.  This is a Header file.

#include "SPI.h"                    //for display
#include "Adafruit_GFX.h"           //for display
#include "Adafruit_ILI9341.h"       //for display

#include <Wire.h>                   //for relay
#define COMMAND_RELAY_OFF 0x00      //for relay
#define COMMAND_RELAY_ON 0x01       //for relay
#define COMMAND_CHANGE_ADDRESS 0x03 //for relay

//The actual default address is SUPPOSED to be 0x18, but 0x19 is what works!
volatile byte qwiicRelayAddress = 0x19;     // Set Address for relay


// For the Adafruit shield, these are the default.
#define TFT_DC 9                    // for display but what?
#define TFT_CS 10                   // for display but what?

#define TEMP1_ADDRESS (0x67)        //for thermocouple
Adafruit_MCP9601 mcp;               //for thermocouple

// Use hardware SPI (on Uno, #13, #12, #11) and the above for CS/DC
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
// If using the breakout, change pins as desired
//Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_MOSI, TFT_CLK, TFT_RST, TFT_MISO);


void setup()
{
    Serial.begin(9600);           //sets the rate for the serial monitor output only! (not I2C)
    while (!Serial) {               //  wait for serial to come up
      delay(10);                    // 10ms delay 
    }

    // This may not be needed since the TC module is also using Wire
    Wire.begin();                              // join the I2C Bus (relay)
    
    Wire.beginTransmission(qwiicRelayAddress); // transmit to device
    //check here for an ACK from the slave
    Serial.println("begin wire transmission test");
    if (Wire.endTransmission() != 0 ) {
      Serial.println("Check Connections. Slave not found.");
      }
    else {
       Serial.println("Qwiic Relay found at 0x19!");
     }

    
    Serial.println("MCP9601 TC + Display test");        // Send to serial monitor 

   /* Initialise the driver with XXX_ADDRESS and the default I2C bus. */
    if (! mcp.begin(TEMP1_ADDRESS)) {
        Serial.println("Thermocouple not found. Check wiring!");
        while (1);
    }

  mcp.setADCresolution(MCP9600_ADCRESOLUTION_18);   //set TC resolution to 18 bits
  mcp.setThermocoupleType(MCP9600_TYPE_K);          //set TC type to K
  mcp.setFilterCoefficient(3);                      //not sure what this does
  mcp.enable(true);                                 //not sure what this does
   
    
  tft.begin();                      // start display process?
  delay(10);                        // delay needed to prevent display inversion?
  tft.setRotation(1);               // initialize the desired screen rotation

  delay(10);                             //not sure why delay is here
  
  Serial.print(F("Screen fill              "));
  Serial.println(testFillScreen());
  delay(500);

//  Serial.print(F("Text                     "));
//  Serial.println(testText());
//  delay(3000);

  tft.fillScreen(ILI9341_BLACK);
  yield();
}


//why is void in the arguments?
void loop(void) {

//  uint8_t status = mcp.getStatus();
//  Serial.print("MCP Status: 0x"); 
//  Serial.print(status, HEX);  
//  Serial.print(": ");
//  if (status & MCP9601_STATUS_OPENCIRCUIT) { 
//    Serial.println("Thermocouple open!"); 
//    return; // don't continue, since there's no thermocouple
//  }
//  if (status & MCP9601_STATUS_SHORTCIRCUIT) { 
//    Serial.println("Thermocouple shorted to ground!"); 
//    return; // don't continue, since the sensor is not working
//  }


  tft.setCursor(0, 0);
  //text color to green and background to black
  tft.setTextColor(ILI9341_GREEN,ILI9341_BLACK); 
  tft.setTextSize(2);
  tft.println("Tiny Recirculator");
  tft.println(mcp.readThermocouple()*1.8+32);   //print calculated value
  relayOn();
  delay(2000);
  relayOff();
      
//  for(uint8_t rotation=0; rotation<4; rotation++) {
//    tft.setRotation(1);
//    testText();
//    delay(1000);
// }
 
//    tft.setRotation(1); 
//    testText();
//    delay(1000);
    
 }


  
 /* Begin function definitions....   */

unsigned long testFillScreen() {
  unsigned long start = micros();
  tft.fillScreen(ILI9341_BLACK);
  yield();
  tft.fillScreen(ILI9341_RED);
  yield();
  tft.fillScreen(ILI9341_GREEN);
  yield();
  tft.fillScreen(ILI9341_BLUE);
  yield();
  tft.fillScreen(ILI9341_BLACK);
  yield();
  return micros() - start;          // this calculates how long it took to fill screen
}

//The following is a whack text display... needs much work
unsigned long testText() {
  tft.fillScreen(ILI9341_BLACK);
  unsigned long start = micros();
  tft.setCursor(0, 0);
//  tft.setTextColor(ILI9341_WHITE);  tft.setTextSize(1);
//  tft.println("Hello World!");
//  tft.setTextColor(ILI9341_YELLOW); tft.setTextSize(2);
//  tft.println(1234.56);
//  tft.setTextColor(ILI9341_RED);    tft.setTextSize(3);
//  tft.println(0xDEADBEEF, HEX);
//  tft.println();
  tft.setTextColor(ILI9341_GREEN);
//  tft.setTextSize(5);
//  tft.println("Groop");
  tft.setTextSize(2);
  tft.println("Tiny Recirculator");
  tft.setTextSize(1);
  tft.println("Ver 1.0");
//  tft.println("And hooptiously drangle me");
//  tft.println("with crinkly bindlewurdles,");
//  tft.println("Or I will rend thee");
//  tft.println("in the gobberwarts");
//  tft.println("with my blurglecruncheon,");
//  tft.println("see if I don't!");
  return micros() - start;
}

void relayOn() {
 Wire.beginTransmission(qwiicRelayAddress);
 Wire.write(COMMAND_RELAY_ON);
 if (Wire.endTransmission() != 0) {
 Serial.println("Check Connections. No slave attached");
 }
}


// RelayOff() turns off the relay at the qwiicRelayAddress
// Checks to see if a slave is connected and prints a
// message to the Serial Monitor if no slave found.
void relayOff() {
 Wire.beginTransmission(qwiicRelayAddress);
 Wire.write(COMMAND_RELAY_OFF);
 if (Wire.endTransmission() != 0) {
 Serial.println("Check Connections. No slave attached");
 }
}
 

User avatar
dastels
 
Posts: 15665
Joined: Tue Oct 20, 2015 3:22 pm

Re: Reset Button = restart code?

Post by dastels »

You might need to have a terminal program running & connected to the USB/Serial port of the board.

Dave

User avatar
SkyRyder
 
Posts: 45
Joined: Fri Mar 06, 2020 6:05 pm

Re: Reset Button = restart code?

Post by SkyRyder »

Oh? what does that do for me? Not sure where this is leading...

User avatar
dastels
 
Posts: 15665
Joined: Tue Oct 20, 2015 3:22 pm

Re: Reset Button = restart code?

Post by dastels »

Your setup starts with

Code: Select all

Serial.begin(9600);           //sets the rate for the serial monitor output only! (not I2C)
while (!Serial) {               //  wait for serial to come up
  delay(10);                    // 10ms delay
}
That often/generally (I'm not sure of the details behind it) waits for a connection to be established before continuing on.

Dave

User avatar
SkyRyder
 
Posts: 45
Joined: Fri Mar 06, 2020 6:05 pm

Re: Reset Button = restart code?

Post by SkyRyder »

Thanks, Dave...

So what you're saying is that a port needs to be available for the code to escape that loop...

So with the arduino connected to my pc and the IDE... I do have an open port window... but it still doesn't do anything.

But you are thinking that if I didn't start up the serial device... then it might just boot properly?
[Edit: I tested this... and you are entirely correct. That is the part that was biffing it.]

I'm pretty sure I've seen example code that starts up fine on reset while running the serial port...

Is there a way to write to the serial port in a way that is entirely benign to all other code execution?

User avatar
dastels
 
Posts: 15665
Joined: Tue Oct 20, 2015 3:22 pm

Re: Reset Button = restart code?

Post by dastels »

I've seen code that uses a call to delay to wait a bit after the Serial.begin() call. I generally use a different approach: selectively using Serial based on a build flag. I have a logging package that I configure through build flags: what to log and where to put the output. So a release build will just log errors and put it on an SD card and/or indicate using a NeoPixel, while I debug build will log everything and send it to Serial.

You can see my latest use of it in https://github.com/dastels/wrist_computer. Look in src at the logging and handler classes.

Dave

User avatar
SkyRyder
 
Posts: 45
Joined: Fri Mar 06, 2020 6:05 pm

Re: Reset Button = restart code?

Post by SkyRyder »

Thank you, Dave!!

Locked
Please be positive and constructive with your questions and comments.

Return to “Metro, Metro Express, and Grand Central Boards”