Trying to make code wait.

What I’m trying to do is get some code to take an analogue reading from sensor (got that) and turn on a motor once it reaches a certain limit (got that too).
But what I NEED it to do, is wait a second after it takes the reading, and then if the reading stays above the threshold I set, the motor should turn on for one second, and then stop, and not act for full minute before doing it again.

There’s no point reading the code I put it, because it’s simple if statements, analogRead and digitalWrite to get the basics. I know I need to do a LOT more than that, and it’s making my brain fizzle. I’ve tried the Blink Without Delay example, and it became an utter mess. I have pretty much no idea what I’m doing once I head into it. I’ve spent longer than I should at it, and I’m completely at a loss. Any help would be GREATLY appreciated.

I’ll put up the code anyways.
What I have now;

#include <MeetAndroid.h>

MeetAndroid meetAndroid;
int sensor = A5;
int motor = A3;

void setup()
{
Serial.begin(115200);

pinMode(sensor, INPUT);
pinMode(motor, OUTPUT);
}
void loop()
{
if (analogRead(A5) > 800)
digitalWrite(A3, HIGH);
else digitalWrite(A3, LOW);

meetAndroid.receive();

// read input pin and send result to Android
meetAndroid.send(analogRead(sensor));

delay(100);
}

And what I tried to get to work. Doesn’t work <_<

#include <MeetAndroid.h>

MeetAndroid meetAndroid;
int sensor = A5;
int motor = 7;
const int ledPin = 13;
int ledState = LOW;
long previousMillis = 0;
long interval = 100;

void setup()
{
Serial.begin(115200);
pinMode(ledPin, OUTPUT);
pinMode(sensor, INPUT);
pinMode(motor, OUTPUT);
}
void loop()
{
if (analogRead(A5) > 800)
digitalWrite(7, HIGH);
if (analogRead(A5) < 750)
digitalWrite(7, LOW);

unsigned long currentMillis = millis();
if(currentMillis - previousMillis > interval) {
previousMillis = currentMillis;
if (ledState == LOW)
ledState = HIGH;
else
ledState = LOW;
digitalWrite(ledPin, ledState);
meetAndroid.receive();
meetAndroid.send(analogRead(sensor));

}
}

Even I can see, the second one is just a mess and makes no sense at all.
So you see. I’m pretty lost.

Thanks in advance, guys!

Edit: It’s also running a bit of code to send the sensors reading to an Android app that will graph out the measurement. That hasn’t caused problems so far, I think. I hope. It never did before, anyway, regardless or where I stuck it in. Maybe it is the problem.
It, and it’s code and Arduino examples, can be found on http://www.amarino-toolkit.net/ if you’re wondering about it.

#include <MeetAndroid.h>

#define THRESHOLD    800

MeetAndroid meetAndroid;
int sensor = A5;
int motor = A3;
bool wait_analog; 
unsigned long now, then; 

unsigned char state; 

#define BEGINNING                0
#define MOTOR_ON                3
#define WAIT_MINUTE            4


unsigned int AnalogValue=0, temp=1; 

void setup()  
{
  Serial.begin(115200); 

  pinMode(sensor, INPUT);
  pinMode(motor, OUTPUT);
  state = 0;
}



void loop()
{

switch (state) {
    case BEGINNING: {
        now = millis(); 
        if (now - then >1000) {
            then = millis(); //new second. 
            if (AnalogValue == (temp = analogRead(A5)) ) {
                state = MOTOR_ON;
                then = millis(); 
                digitalWrite(motor, HIGH);        
            } else {
               AnalogValue = temp; }
        }
        break;}
    case MOTOR_ON: {
        if ((now = millis()) - then >1000) {
            state = WAIT_MINUTE;
            digitalWrite(motor, LOW);
            then = millis;
            }
        
        break;}
    case WAIT_MINUTE: {
         if ((now = millis()) - then >60000) { //1 minute.
             state = BEGINNING; 
             then = millis();}
        break;}
   }
}

Something like this? I left out the stuff, but you should have all you need there. This is called a state-machine.

You need to start with the blink without delay example.

On each pass through loop, read the sensor.

If it is above the threshold, and the aboveThresholdTime is not set, record the time. If it is above the threshold, and the aboveThresholdTime is set, and the current time is greater than that time by the required amount (1 second), do something and reset the aboveThresholdTime (to 0).

When you do that something, you can either delay for one minute OR note the time that you last read the sensor and not read again unless one minute has gone by.

But what I NEED it to do, is wait a second after it takes the reading, and then if the reading stays above the threshold I set, the motor should turn on for one second, and then stop, and not act for full minute before doing it again.

So think about how you would do that if you had to do it manually yourself.

1) You would take a reading. 2) Write down the time on your clock 3) Take the second reading after a second has elapse.

So what prevents you from doing 1 & 2 while waiting for enough time to pass that it is time for 3? You use a variable (a state variable) that notes 1 & 2 have been done and you are waiting for 3. When 3 has been done, make a note of the time and also change your state variable to show the next thing to do is step 4. 4) Wait 60 seconds. All you need to do at step 4 is to change this state variable to indicate you should do 1) next time round the loop.

So in front of each of those actions is a if statement seeing if the state variable is in a state where you should do the action.

Try drawing out a flow diagram of what is happening.

I regret not frequently saving my sketch, because that’s not exactly what I was last working on.

I have gone over the Blink Without Delay, and I tried to incorporate it. But I had no such success. I also found some code that would make the Arduino work as a stopwatch. I can’t remember at the moment where it was, but I know it was on these forums and I stumbled across it whilst browsing Google. I tried massacring that and turning some of the code (It was well described and seemed like it would work) into something that would work for me.

I looked again and discovered the code I was most recently using had a different, yet similar name. It’s this;

#include <MeetAndroid.h>

const int ledPin = A3; // the number of the LED pin

MeetAndroid meetAndroid;
int sensor = A5;

// Variables will change:
int ledState = LOW; // ledState used to set the LED
long previousMillis = 0; // will store last time LED was updated
long startTime;
long elapsedTime;
// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long interval = 100; // interval at which to blink (milliseconds)

void setup() {

Serial.begin(115200);
// set the digital pin as output:
pinMode(ledPin, OUTPUT);
pinMode(sensor, INPUT);
}

void loop()
{
if(ledState == LOW && analogRead(A5) >= 800)
{
startTime = millis();

}
if (ledState == LOW && analogRead(A5) >= 800 && elapsedTime >= 1000)
{
elapsedTime = millis() - startTime;
ledState == HIGH;
digitalWrite(A3, ledState);
}
if (ledState == HIGH && analogRead(A5) >= 800 && elapsedTime >= 2000)
{
ledState == LOW;
digitalWrite(A3, ledState);
}
meetAndroid.receive(); // you need to keep this in your loop() to receive events

// read input pin and send result to Android
meetAndroid.send(analogRead(sensor));

// add a little delay otherwise the phone is pretty busy

delay(100);
}

And it still doesn’t work. At all.

By the way, Bubulindo, I tried running your code to see if it would solve it for me, and I got an error. A lot of what’s in your code I’ve never seen before. I’ll study the ever loving hell out of it! Thank you.
SensorMotorMillis:50: error: invalid conversion from ‘long unsigned int (*)()’ to ‘long unsigned int’

I suppose, in the end, all I need to do is figure out how to get the code to wait a few times. Waiting once, I can do. More than that, and my IDE just turns red. <_<

Waiting once, I can do. More than that, and my IDE just turns red. <_<

The Arduino waiting does NOT cause an application running on another computer to turn read. You need to explain this statement.

I mean, it fills with errors. It was just an expression.

The error messages say things. From that you can figure out what you did wrong.

Well, with the last piece of code I put up, I get no errors, but it doesn't work. So, I got pretty lost from there.

with the last piece of code I put up, I get no errors,

That means the syntax is correct.

but it doesn't work

That means you told it to do the wrong thing.

So, I got pretty lost from there.

So read the replies you have already had and ask about them if you don't understand anything.

Alternatively keep waiting until some one writes the code for you. Is that your idea of fun?

Just noticed my code doesn’t make use of the Threshold… so you have to put that in.

If you post the errors here, maybe we can figure this out. :wink:

Is that your idea of fun?

Of course it is, Condescending Member.
No, the point is, I have no idea what’s wrong when I don’t know what’s wrong. I’m not getting errors, and I’m not able to read what’s wrong.

And to you, Bubulindo, the only errors I get are

SensorMotorMillis.cpp: In function ‘void loop()’:
SensorMotorMillis:50: error: invalid conversion from ‘long unsigned int (*)()’ to ‘long unsigned int’

No idea what that means. I’m. Not exactly amazing at this <_< All I know is, line 50 is wrong. That said, it’s usually the line before that that I have to fix when something like this occurs. But I literally have no idea. I could mash in something at random and hope that works, but I’m afraid it’ll ruin it. Can you tell by looking at the last piece of code I put up what’s wrong with it? Since I’m getting no errors, I’m pretty lost, as I said. I’d like to keep using the code I already was using. But, obviously if it’s completely worthless then I can start from scratch. Thanks!

That is a strange error message...

line 50 is the delay... have you tried commenting that line? Just to see if there's something wrong with the linking on the delay() ?

By the way, you are testing values instead of assigning them throughout that example.

KoyaanisqatsiL:

ledState == HIGH; ledState == LOW;

I didn't compile my code, but it's a possible solution.

Next time use the CODE tags and not the quote. But I appreciate the patience to color code your post.

Maybe it’s a cultural thing, but what does “<_<” mean?
I thought it was meant to be a highlight in the code, but I don’t see it anywhere, so I assume it is some sort of emoticon.

Rather than just bleating “it doesn’t work”, or “it’s wrong”, how about tackling the problem, by stripping out the stuff that most of us don’t have (namely “MeetAndroid.h”), and figure out your problem with a known working solution, like the serial monitor?

Rather than just bleating "it doesn't work", or "it's wrong"

As I said, that's literally all I know. I don't know what else to do. I've done a few revisions, and to put it in words, the delaying doesn't work for reasons I don't understand, no matter how long I spend altering and reading it on the fly. To document all the changes I make would take days, because that's how long I've been tearing apart and building this back again. My knowledge base is fairly low, I'm still learning and reading books. But I've so far not encountered anything that resembles what I'm trying to do. In words, what I want it to do is; I have an LDR wired to A5 as 'sensor.' I have a small DC motor connected to A3, 'motor.' You can ignore the meet android stuff, it works. It sends the data I get from the sensor to an Android phone, which uses an app someone else wrote to graph the data, as a line with two extremes in values, from 0-1023. When I shine an LED torch on the LDR, the reading is sent to the board as an analogue signal, and goes over the 800 mark, up to 9xx depending on how close I am. I want it to wait one second after it does that, then turn on the motor for one second. After the motor has run for one second, it turns off for a minute. Loop. What I have is an instantaneous response from the motor; it turns on exactly as the signal goes over 800, and off when it's under 750, or 800 or whatever I declare. I want it to wait. That's literally it. I have no idea how to get it to wait. I've tried numerous things, to no success. Most of the stuff I came up with from simple code doesn't work at all. Just ignores the code I thought would make it work, and turns on the motor. So it seems. If you can read invisible data, great. I can't. What I have achieved thus far has been more or less without help. About 2 months ago I didn't know what Arduino was. I'm pretty impressed with myself for being able to achieve what I have with no background in this area at all. So, please. Help me with this waiting business. It's making my head burn, and my eyes hurt, going over code that tells me it's fine, and doesn't work the way I want or need it to. I'm completely lost.

Also

Maybe it's a cultural thing

It is. I'm from the internet.

Edit:

By the way, you are testing values instead of assigning them throughout that example.

Really? Aw damnit. Well, that MIGHT be the reason it won't turn on in that code. Thank you, I'll try and rectify that right the hell now!

Quote Maybe it's a cultural thing It is. I'm from the internet.

I looked it - according to Wikipedia (is there any other definitive source of information?), it means "High five".

I thought high fives were associated with celebration.

It's meant to be, basically, shifty eyes.

Regarding my code. I tried changing some stuff there. Didn't work. In a couple of codes I have for attempting this, neither worked. I don't want to mess around with this too much, because I've made 2 different types of optical encoders die, in a tiny little explosion. It's not fun.

_>

  if (ledState == LOW && analogRead(A5) >= 800 && elapsedTime >= 1000)

Look at when and where elapsedTime is valued, while you are looking at the code.

I’ve made 2 different types of optical encoders die, in a tiny little explosion. It’s not fun.

It’s not? I always have fun when I blow stuff up.

On purpose, anyway.

If it's this part you mean;

if (ledState == LOW && analogRead(A5) >= 300 && elapsedTime >= 1000) { elapsedTime = millis() - startTime; ledState == HIGH; digitalWrite(A3, ledState); }

I tried placing it in the above block of code;

if(ledState == LOW && analogRead(A5) >= 300) { startTime = millis(); elapsedTime = millis() - startTime; }

And I thought that would fix it. But it didn't! I'm not good at this, am I? Anyways, I don't fully understand what you mean. Oh and blowing stuff up is fun, but it was kinda unexpected. It was loud for such a little thing. Thank God I haven't gone near that HUGE capacitor on the printer I took apart.