Go Down

Topic: Overworking the Arduino? (Read 439 times) previous topic - next topic

wilshire

I'm attempting to have a button activate an LED and then read from a sensor. Once the button is deactivated I want to take those sensor readings and store them to a text file on an Micro SD Card. In pieces I can get everything to function. I can get the button to activate/deactivate the LED, I can get readings off of the sensor and I can write to a text file. Its when I put everything together I get the issues.

My direct issues are in "void loop()" section:
1. The LED doesn't activate when the button is depressed which is puzzling
2. The Loop to record the Force Sensor does not end correctly when the button is no longer pressed

Code: [Select]
//This include is for the Micro SD Card Library
#include <SD.h>

// These variables for code below
const int buttonPin = 2;        // the number of the pushbutton pin
const int ledPin =  13;          // the number of the LED pin
int sensorFrontLeft = 0;       // variable to store the sensor value
boolean isLightOn = false;     // flag used to tell the SD Card code if the button was previously pressed
String dataString = ""; // make a string for assembling the data to log
int buttonState = 0;             // variable for reading the pushbutton status
File dataFile; // initialize outputFile object which is the data log

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);     
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);     
  // initialize serial communications at 9600 bps:
  Serial.begin(9600);
  //initialize SD Card
  pinMode(10, OUTPUT);
  if(!SD.begin(4))
  {
     Serial.println("failed");
  }
}

void loop(){
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // This if statement constantly checks to see if the pushbutton is pressed.
  // if it is, the buttonState is HIGH or 1:
  if (buttonState == 1) { 
 
     // turn LED on so that we know we should now be recording. [Issues #1]
     digitalWrite(ledPin, 1); 
     //This flag helps us know in the else statement if we ever actually pressed the button
     isLightOn = true;
   
    //This do while loop should run until the button is no longer depressed
    do
    {
        // reads the values from the force sensor
        sensorFrontLeft = analogRead(0);
        delay(1000);
        // adds the force sensor reading to the string used for logging
        dataString += String(sensorFrontLeft);
//separates the readings with a ","
        dataString += ",";
    } while (digitalRead(buttonPin) == 1); //[Issue #2, the loop never ends]
}
  // The else button should run constantly while the button is not depressed
  else {
    // turn LED off:
    digitalWrite(ledPin, 0);

//If the light is off we want to know if the button was depressed earlier.
//This if statement checks the flag set above when the button is initially depressed
if (isLightOn == true) {
// open the file we want to write the force sensor string log too
File dataFile = SD.open("log.txt", FILE_WRITE); 

// if the file is available, write to it:
if (dataFile) {
dataFile.println(dataString);

// if the file isn't open, pop up an error:
else {
Serial.println("error opening datalog.txt");
}
//close the text file
dataFile.close();
//reset the flag so we will know when its depressed again
isLightOn = false;
}
  }
}


Thanks for any help anyone can provide!

marco_c

Use HIGH and LOW when comparing or setting digital values read. These may or my not be 1 and 0.

Not sure I like the delay() in your code. I would consider changing your code to be more like a finite state machine, as you can describe the states reasonably well.

Arduino libraries http://arduinocode.codeplex.com
Parola for Arduino http://parola.codeplex.com

Go Up