Data Logger With Button

Hi All,
I am an Engineering student who is working on making a temperature data logger that will record temperatures for a certain amount of time until I push and hold my button to have the arduino spill all of the data to my serial port. I am having trouble getting my code to work correctly. So far I am able to get random symbols at a random time when I hold down my button, I am not sure why. I have made a simple sample code and checked to see that my Temp sensor will report numbers to the serial port which it does, and my button appears to be working as well. Getting them to work together in this code is my problem, any help would be great!

int tempPin = A0;         //temp sensor analog 0
int tempData[] = {200};  //array of my data slots to store in
int button = 2;          //button is in digital pin 2
int buttonState = 0;      

void setup()
{
  pinMode(tempPin, INPUT);    
  pinMode(button, INPUT);
  for(int counter = 0; counter < 201; counter++)  //telling to set all of my array slots to outputs
  {
  pinMode(tempData[counter], OUTPUT);
  }
}

void loop()
{
  buttonState = digitalRead(button);     //telling to look at the button pin in order to see what to do
  storetemp();                            //calling my function
  if (buttonState == HIGH)                //if the button was being held down print what my function was reading
{
  Serial.begin(9600);
  for (int i = 0; i < 201; i++)
  {
  Serial.print(tempData[i]);
  }
}
  
}

void storetemp()                      //defining my function and telling it what to run
{
  int temp = analogRead(tempPin);      //my temperature is going to be what my temperature sensor reads
  temp = map(temp, 0, 1023, 0, 240);      //constrain my temperature to these values
  for(int Slot = 0; Slot < 201; Slot++)    //for loop telling to save my temperature to my array to the next slot in line
  {
  digitalWrite(tempData[Slot], temp);
  delay(100);                              //delay to record another temperature
  }
}
  1. Are you using a pull-down (or pull-up) resistor with your button?

  2. This code doesn’t make sense:

temp = map(temp, 0, 1023, 0, 240);      //constrain my temperature to these values
  for(int Slot = 0; Slot < 201; Slot++)    //for loop telling to save my temperature to my array to the next slot in line
  {
  digitalWrite(tempData[Slot], temp);

The digitalWrite line is complete nonsense. You are telling the Arduino to write to a Pin (which is returned from whatever is at position ‘Slot’ inside of ‘tempData’), the value of temp.

I think you meant your code to say: “tempData[Slot] = temp;”

digitalWrite() is only for I/O pins and has nothing to do with storing values into a variable (or array).

Thank you for the response.

  1. I am new to circuits so I am not sure how to tell if my resistor is a pull up or pull down. It is part of the arduino kit that I have (10k ohm).
  2. After I deleted my “digitalWrite” line and uploaded your solution I am still getting about six symbols that look like the “alpha” symbol followed by “u” then a bunch of nothing that goes on forever (through my serial port).
  1. http://www.arduino.cc/en/Tutorial/Pushbutton
    Just remember when using a pull-up configuration that the logic is inverted. (unpressed the pin is HIGH, pressed, the pin goes LOW.)

  2. The logic your storetemp() is probably not storing anything useful. You are storing the same value of "temp" in all 200 locations of your array.

As for the strange characters it is either because junk is stored in your array or there is a communication issue. It is probably a good idea in setup() to run through the array and force zeros into all of the locations.

The problem is probably related to the fact that you are calling Serial.begin(9600) in loop(). You should only call this once in setup().

You probably want to be using Serial.println() instead of Serial.print(). Otherwise, the values will just print one after another.

try: int tempData[200] ; Then you will actualy have 200 locations allocated to store data

Serial.begin in setup, then leave it alone

in the loop try
{
storetemp();
if (digitalRead(button)){
... conditional code ....

This code is no more legitimate than yours, but I will like it better

In the startup code write a few integers into the array and then see if they read back

If the switch is between an input pin and ground, it should be okay, if you remember to write a 1 to it.

The input will read HIGH, 1, and boolean true when the button is up. The opposite when depressed. Be certain the logic works want it to. You might intend to say:
if (! digitalRead(button))