Exiting a while loop, but stuck in it.....

Hi i'm rewriting the LowLatencyLogger from the SdFat library to work with my MMA8451 Sensor.
I've got almost everything working.
Eventually i have no access to the serial monitor, so i added a LED to indicate logging is running. (WORKS)
I added a button to simulate a key press to stop logging. (WORKS)
But i want to use the same button to start logging. But that part of the code is in a do..while loop, but can't find the good spot to put my(if digitalRead(13) ==LOW) statement in.

Someone who can help me with that. So the code waits for serial input, or buttonpress to start logging.
I attached my code.

LowLatencyLogger.ino (18.8 KB)

mma8451lowlatencylogger.ino (73 Bytes)

UserFunctions.cpp (1.33 KB)

UserTypes.h (323 Bytes)

But that part of the code is in a do..while loop, but can't find the good spot to put my(if digitalRead(13) ==LOW) statement in.

Why are you using a do/while loop? On any given pass through loop(), the switch has become pressed or it has become released or it has not changed state. Make use of that, as shown on the state change detection example.

If you think you need to circumvent the loop() function's being called over and over, you will need to change which loop you are detecting the state change in.

The do...while..loop was already in the examples. So I left them there. I want to leave them there also so I can test and do other stuff with it. Only need to know how to implement the switch and keeping the rest intact and functional.

Only need to know how to implement the switch and keeping the rest intact and functional.

You already know how to read a pin that has a switch connected to it. You already know where you need to read the state, What else do you need to know?

I'm a bit in the woods with all of the brackets if s and else if that I don't know where to put my when button is low execute logging part. Tried several places but doesn't seem to work. Thought maybe someone could give me that piece of code to make it work. Apparently no one can. So I should leave it then. Thanks anyway....

Apparently no one can

That's not true. We don't write code for you, though.

Where is the do/while loop?

The last 20 or so lines of lowlatencylogger.ino.
There's the loop where I have tried to put in my if digitalRead(13) == LOW command.

So i now made a button which i would like to use to start logging.
In the example the sketch waits until button "r"is pressed to start logging.

  char c = tolower(Serial.read());

  // Discard extra Serial data.
  do {
    delay(10);
  } while (Serial.available() && Serial.read() >= 0);

  if (ERROR_LED_PIN >= 0) {
    digitalWrite(ERROR_LED_PIN, LOW);
  }
  if (c == 'b') {
    openBinFile();
  } else if (c == 'c') {
    binaryToCsv();
  } else if (c == 'd') {
    dumpData();
  } else if (c == 'e') {
    checkOverrun();
  } else if (c == 'l') {
    Serial.println(F("\nls:"));  
    sd.ls(&Serial, LS_SIZE);  
  } else if (c == 'r') {
    logData();
  } else if (c == 't') {
    testSensor();    
  } else {
    Serial.println(F("Invalid entry"));
  }

Where do i put my piece of

if (button == LOW){
logData();
}

I've tried a lot of different positions inside and outside the loop and the only option i got working was powering the arduino with the button pressed. But not while the arduino is running i can start logging on button press.

I really like to get some help with this one, because i do not understand the loop and the conditions completely.

Kind regards,

Bert.

delchrys:
I'm a bit in the woods with all of the brackets if s and else if that I don't know where to put my when button is low execute logging part. Tried several places but doesn't seem to work. Thought maybe someone could give me that piece of code to make it work. Apparently no one can. So I should leave it then. Thanks anyway....

Ok. What I am getting here is that you aren't making an effort to understand what the code actually does. I mean, lets say someone was trying to fix their car, and said "well, I have tried connecting some of these tubes to some of the other tubes, but it's not going". That's the impression I get when I read "Tried several places but doesn't seem to work".

In general, you cannot do computer programming by trying to make your code look right. You have to understand what it actually does. That's what the IDE tutorial examples are for.

thanks, will ask elsewhere

what i meant was that i tried it at many different places within the void loop.
So in the do..while loop, out of it etc.
only got it working when booting with button pressed, now i'm about two weeks busy with this silly part and still not getting anywhere. I really try to understand examples and other sketches, but the while loops keep bugging me.
But aparently you think i'm not putting any effort in it, guess you know me better than me.

delchrys:
what i meant was that i tried it at many different places within the void loop.
So in the do..while loop, out of it etc.
only got it working when booting with button pressed, now i'm about two weeks busy with this silly part and still not getting anywhere. I really try to understand examples and other sketches, but the while loops keep bugging me.
But aparently you think i'm not putting any effort in it, guess you know me better than me.

No, just think you're going about it the wrong way. Instead of just trying a bunch of places, get in there and figure out how that code is working. Where to put it becomes kind of obvious. Sometimes I like to pretend I'm the little man inside the chip getting the instructions and work through each one, line by line, and see what I'd have to do.

While loops are easy, they start by evaluating the thing in parenthesis, if it is true then they run all the code in the curly braces. Then they evaluate the thing in the parenthesis and if it is true they run all the code in the curly braces. Then they evaluate the thing in the parenthesis and if it is true they run all the code in the curly braces. Then they evaluate the thing in the parenthesis and if it is true they run all the code in the curly braces. Then they evaluate the thing in the parenthesis and if it is true they run all the code in the curly braces. Then they evaluate the thing in the parenthesis and if it is true they run all the code in the curly braces. Then they evaluate the thing in the parenthesis and if it is true they run all the code in the curly braces. Then they evaluate the thing in the parenthesis and if it is true they run all the code in the curly braces. Then they evaluate the thing in the parenthesis and if it is true they run all the code in the curly braces. Then they evaluate the thing in the parenthesis and if it is true they run all the code in the curly braces. Then they evaluate the thing in the parenthesis and if it is true they run all the code in the curly braces. Then they evaluate the thing in the parenthesis and if it is true they run all the code in the curly braces. Then they evaluate the thing in the parenthesis and if it is true they run all the code in the curly braces. Then they evaluate the thing in the parenthesis and if it is true they run all the code in the curly braces. Then they evaluate the thing in the parenthesis and if it is true they run all the code in the curly braces. Then they evaluate the thing in the parenthesis and if it is true they run all the code in the curly braces. Then they evaluate the thing in the parenthesis and if it is true they run all the code in the curly braces. Then they evaluate the thing in the parenthesis and if it is true they run all the code in the curly braces. Then they evaluate the thing in the parenthesis and if it is true they run all the code in the curly braces. Then they evaluate the thing in the parenthesis and if it is true they run all the code in the curly braces. Then they evaluate the thing in the parenthesis and if it is true they run all the code in the curly braces. Then they evaluate the thing in the parenthesis and if it is true they run all the code in the curly braces. Then they evaluate the thing in the parenthesis and if it is true they run all the code in the curly braces. Then they evaluate the thing in the parenthesis and if it is true they run all the code in the curly braces. Then they evaluate the thing in the parenthesis and if it is true they run all the code in the curly braces.

And when the evaluation step of the thing in parenthesis turns out to be false then they don't run the stuff in the curly braces and skip over whats in the curly braces.

A do while loop is slightly different. It goes like this,

First it runs all the code in the curly braces and THEN it evaluates the condition. If it is true it goes back up and runs all the code in the curly braces and then it evaluates the condition. If it is true it goes back up and runs all the code in the curly braces and then it evaluates the condition. If it is true it goes back up and runs all the code in the curly braces and then it evaluates the condition. If it is true it goes back up and runs all the code in the curly braces and then it evaluates the condition. If it is true it goes back up and runs all the code in the curly braces and then it evaluates the condition. If it is true it goes back up and runs all the code in the curly braces and then it evaluates the condition. If it is true it goes back up and runs all the code in the curly braces and then it evaluates the condition. If it is true it goes back up and runs all the code in the curly braces and then it evaluates the condition. If it is true it goes back up and runs all the code in the curly braces and then it evaluates the condition. If it is true it goes back up and runs all the code in the curly braces and then it evaluates the condition. If it is true it goes back up and runs all the code in the curly braces and then it evaluates the condition.

When it does the evaluation step and the thing in the parenthesis gives false then it doesn't go back to the top but gets on with the next line after the do-while.

One of the greatest presentations I've ever seen a teacher do on programming involved absolutely zero code. He asked the students to write out a set of instructions to do some simple task. I think it was sharpening a pencil or something like that. And then he followed their instructions to the letter like a computer would. He took the steps one at a time and did exactly what they said. It was quite funny as everyone made assumptions at certain points that he would just know what they meant or would just know what to do but he would go ahead with exactly how they wrote it no matter how stupid it looked. It was a great class because it taught people that you have to think first in logic and second in code.

Code isn't some magical mix of esoteric words that if combined in just the right order produce some result. That's called magic and it doesn't exist. Code is just a list of step by step instructions. And you should be able to write the step by step instructions out without code and get it straight and then write the code from that.

Some examples of what he got were like this. Say the first person said, "1. Walk over to the pencil sharpener" and the teacher, who was sitting in the chair, just yelled "Error: I can't walk while I am sitting down." This was corrected to, "1. Stand up, 2. walk over to pencil sharpener" at which point he got up and walked to the pencil sharpener, but without the pencil. They had forgot to tell him to pick up the pencil. "1. stand up. 2. grab pencil 3. walk to pencil sharpener.
4. Stick pencil in sharpener" so he did that and stuck the eraser end in the sharpener. You get what he was up to I hope. With computers you have to give them every single exact step just like they are completely stupid. Because they are. They will just run your set of instructions, in order, line by line, exactly as you write it. Once you really realize that programming gets a lot easier.

delchrys:
But aparently you think i'm not putting any effort in it, guess you know me better than me.

Or you can get your little feelings hurt and get all pissy and just quit. I'm OK with that too. If that's what you do then it just means you weren't going to make it anyway. So it's not really any loss to anyone.