Push button to start serial print

Hi Folks,

I'm just getting myself started with Arduino and I'm having trouble with the code to get a push button to start printing serial data.

The project I'm working on is a thermocouple data logger for integration into the nosecone of a high power rocket. The code is based on Ryan McLaughlin's code for the MAX6675 thermocouple temperature chip with just minor changes to apply it to my project. The push button will eventually be replaced with a g-switch with the idea being that when the rocket launches, it triggers the g-switch which in turn starts printing serial data to the OpenLog logger that I'm using.

I have included code that I thought would acomplish this, however I'm tearing my hair out trying to get it to work, I'f anybody can spot where I am going wrong I would be very grateful. Also, I plan to have the serial data continue to print for a given length of time, maybe 15 seconds, after the push button/g-switch has been released, allowing the data to continue logging while the rocket is coasting upwards. I have no idea how this would be done, so any tips would be much appreciated.

Code is below.

Thanks for your help,

Chris

//rocket nosecone temperature logger

#include <MAX6675.h>


int CS = 10;              // CS pin on MAX6675
int SO = 12;              // SO pin of MAX6675
int SCK = 13;             // SCK pin of MAX6675
int units = 0;            // Units to readout temp (0 = [ch730]F, 1 = [ch730]C)
float error = 0.0;        // Temperature compensation error
float temperature = 0.0;  // Temperature output variable
const int buttonPin = 2;  //button connected to pin 2

// Initialize the MAX6675 Library for chip

MAX6675 temp0(CS,SO,SCK,units,error);




// Setup Serial output and LED Pin  
// MAX6675 Library already sets pin modes for MAX6675 chip!

void setup() {
  Serial.begin(9600);
  
  pinMode(buttonPin, INPUT);// initialize the pushbutton pin as an input:
}

void loop() {
  
  
  temperature = temp0.read_temp(5);         // Read the temp 5 times and return the average value to the var
  
  temperature = (temperature -32)/1.8;       // convert temp from Farenheit to Celsius
  

  

  if(temperature == -1) {                   // If there is an error with the TC, temperature will be -1
    Serial.println("Thermocouple Error!!"); // Temperature is -1 and there is a thermocouple error
    
  } else {
    
    digitalRead (buttonPin);
    
    if (buttonPin == HIGH)
    Serial.print("nose temp: ");
    Serial.println( temperature );          // Print the temperature to Serial 
    
  }
        
  delay(200);      // Wait 0.2 seconds before reading again
}

Welcome!

When you post code (or long lists) please use "code tags". They can be inserted into the edit window using the # button.

Is the "g-switch" a switch or a button? In other words, once it triggers, does it get "stuck on"?

Do you have a pull-up or pull-down resistor on pin 2?

Should the following print whenever the error occurs or only after the g-switch triggers...

  Serial.println("Thermocouple Error!!"); // Temperature is -1 and there is a thermocouple error

This should get the Sketch working a bit better...

   // digitalRead (buttonPin);  // Remove this line
   
   if ([glow]digitalRead (buttonPin)[/glow]== HIGH)
   [glow]{[/glow]
   Serial.print("nose temp: ");
   Serial.println( temperature );          // Print the temperature to Serial 
   [glow]}[/glow]

digitalRead is a function that returns a LOW or HIGH values based on the state of the pin. The value returned from the digitalRead function is used in the if-statement. The extra braces are the then-clause of the if-statement. Without the braces only the statement immediately after the "if" is the then-clause.

Hi Coding Badly, thanks for your reply and the tip about the code tags.

The g-switch effectively is the same as a momentary push button switch, i.e. does not latch, so as soon as the g-force drops off it becomes open again.

I have a pull down resistor on pin 2.

The thermocouple error should print whenever there is an error with the thermocouple, regardless of whether the g-switch has triggered. I'm also going to include an LED to indicate as such so I can see a thermocouple error without having to look at the serial data.

Thanks for your help with the code, I will give it a go now.

Chris

The g-switch effectively is the same as a momentary push button switch, i.e. does not latch, so as soon as the g-force drops off it becomes open again.

Should logging start and continue when the g-switch triggers or should logging only occur when the g-switch triggers?

I made the modification to the code that you suggested and it is now logging serial data when the button is pressed, thanks.

Should logging start and continue when the g-switch triggers or should logging only occur when the g-switch triggers?

Logging should start when the g-switch triggers (closes), log for the duration of the switch being closed and continue to log for a further 15 seconds after the switch opens again.

Got it. If you can't get it working, I suspect someone here will be able to help.

Good luck and feel free to let us know about your project. The Exhibition section is a good place to brag.

Thanks, I'll post all the details in the exhibition section once I have it finished and ready to fly.

if(temperature == -1) { // If there is an error with the TC, temperature will be -1
    Serial.println("Thermocouple Error!!"); // Temperature is -1 and there is a thermocouple error
    
  }

Hello. I'm a beginner as well, but I'm not sure that the Serial.Print above will ever be triggered.

Your temperature value is a float, hence to compare it with a fixed value is unwise. You need to ask 'is the absolute value of the difference between temperature and -1 less than some small, but arbitrary value (perhaps 0.0001)'?

In addition, because you do some maths on temperature before you do the test, your thermocouple would need to return 30.2 (if my maths is correct) for temperature to be -1.