Referring to Serial

Is it possible to refer to what is printed in Serial with your program? If so, how?

int fsrPin = 0;
int fsrReading;
int fsrPinB = 1;
int fsrReadingB;
int LEDpin = 5;

void setup(void) {
Serial.begin(9600);
pinMode (LEDpin, OUTPUT);
}

void loop(void) {
fsrReading = analogRead(fsrPin);
fsrReadingB = analogRead(fsrPinB);

if ((fsrReading < 10) && (fsrReadingB > 10)) {
digitalWrite(LEDpin, HIGH);
Serial.println("Child alone in car!");
} else if ((fsrReading > 10) && (fsrReadingB > 10)) {
digitalWrite(LEDpin, LOW);
Serial.println("Child with driver in car.");
} else if ((fsrReading > 10) && (fsrReadingB < 10)) {
digitalWrite(LEDpin, LOW);
Serial.println("No child in car.");
} else if ((fsrReading < 10) && (fsrReadingB < 10)) {
digitalWrite(LEDpin, LOW);
Serial.println("Car empty.");
}
delay(1000);
}

Can you be a little clearer about what you mean?

You know what you've just printed; why would you want to refer back to it?

Have a look at the blink without delay example in the IDE; it contains the answer to your problem.

The Arduino edit/compile environment.

What process would have sent the "child is in car" to the serial port?

See the blink without delay example here.

SoapyBoat:
Void setup includes "Serial.begin (9600)", and void loop includes "Serial.println("Child alone in car!"). I believe those send it.

Well since your setup has sent it, there's no need to look to see if it's there.

This is the issue, you never need to look at the serial port to work out what you've sent because at the point you send it you know that you've sent it.

What ChilliTronix said. Also, since I've already written it... :smiley:

millis() is an Arduino function that gives you the number of milliseconds that have passed since the last time the Arduino was reset, or connected to power.

So, for instance, if you want a LED to light up after one second* of resetting the Arduino, you'd write something like this:

// Here's a loop that waits until a second has passed
while (millis() < 1000) ;
// Now the loop has ended, time has come to light up the LED
  • Actually it'll be a bit longer, because the Bootloader runs for a little while after reset, and only then yields control to your own code.

Now let's say you want to measure 1 second from external event A, and then start event B:

unsigned long timeStamp; 
// We learned that event A happened, so...

timeStamp = millis(); // record onset time of event A

// The waiting loop, checks current time against onset of A
while (millis() - timeStamp < 1000) {
 // Do whatever you want while you're waiting
}

// Loop has ended, meaning a second has passed, fire up event B

igendel, what you are doing is essentially the same as using delay(), your code is doing nothing (and can't do) while waiting. The point of the Blink without delay example is to actually allow to make other things without blocking in a while loop.

uint32_t prevTime = 0;
uint32_t curTime = 0;

void loop()
{
    curTime = millis();

    if ( curTime - prevTime >= 1000 )
    {
        prevTime = curTime;
        Serial.println( "One second elapsed" );
    }

    Serial.println( "I'm not waiting..." );
}

guix:
igendel, what you are doing is essentially the same as using delay(), your code is doing nothing (and can't do) while waiting.

That was the first code in my reply, just to demonstrate what millis() is. The second one already has an option to do something while waiting - and, of course, one can change that "while" declaration into an "if" with the opposite condition, and no more blocking :slight_smile:

The demo several things at a time is an extended example of BWoD. The technique is also used in the Thread planning and implementing a program

...R

SoapyBoat:
If the child has been placed in the seat, I don't want the light to go off, because the child has to be placed in the seat before the driver can sit. The LED is soon to be replaced with a motor that will vibrate, so I don't want to motor to vibrate just because the driver hasn't had a chance to sit.

Then you need to record in a variable the fact that you are sending the message and refer to that later if you need to check. Indeed you should use the variable to decide whether to send the message.

For example (these lines won't be one after the other)

boolean childInSeat;
childInSeat = true;
if (childInSeat == true) {

...R

this is a millis based solution, did a rewrite of some code

//
//    FILE: .ino
//  AUTHOR: 
// VERSION: 0.1.00
// PURPOSE: demo
//    DATE: 2014-11-09
//     URL:
//
// Released to the public domain
//
int childSensor;
int driverSensor;

const int childPin = 0;
const int driverPin = 1;
const int alarmPin = 5;

unsigned long lastCheck = 0;
unsigned long interval = 1000;

void setup(void) 
{
  Serial.begin(115200);
  pinMode (alarmPin, OUTPUT);
}

void loop(void) 
{
  if (millis() - lastCheck >= interval)
  {
    lastCheck += interval;
    childSensor = analogRead(childPin);
    driverSensor = analogRead(driverPin);

    if (childSensor < 10) 
    { 
      if (driverSensor > 10)
      {
        digitalWrite(alarmPin, HIGH);
        Serial.println("Child alone in car!");
      }
      else
      {
        digitalWrite(alarmPin, LOW);
        Serial.println("Child with driver in car.");
      }    
    }
    else
    {
      if (driverSensor > 10)
      {
        digitalWrite(alarmPin, LOW);
        Serial.println("Car empty.");
      }
      else
      {
        digitalWrite(alarmPin, LOW);
        Serial.println("Driver alone in car.");
      }
    }    
  }
}

better would be a state machine (I'll be back)

state machine version, it will monitor the state of the car safety continuously.
It reports immediately when it detects a change.

//
//    FILE: carSafety.ino
//  AUTHOR: Rob Tillaart
// VERSION: 0.1.00
// PURPOSE: demo state machine
//    DATE: 2014-11-09
//     URL:
//
// Released to the public domain
//

const int childPin = 0;
const int driverPin = 1;
const int alarmPin = 5;

enum car { 
  EMPTY, DRIVER, CHILD, DRIVERCHILD, INIT };
car previousState = INIT;
car state;

void setup(void) 
{
  Serial.begin(115200);
  pinMode (alarmPin, OUTPUT);
}

void loop(void) 
{
  // READ SENSORS
  bool child = analogRead(childPin) < 10;
  bool driver = analogRead(driverPin) < 10;

  // DETERMINE NEW STATE
  state = EMPTY;
  if (!child && driver) state = DRIVER;  // ! == not
  if (child && !driver) state = CHILD;
  if (child && driver) state = DRIVERCHILD;

  // STATE CHANGED => ACT
  if (state != previousState)
  {
    previousState = state;  // remember
    switch(state)
    {
    case EMPTY: 
      digitalWrite(alarmPin, LOW);
      Serial.println("Car empty.");
      break;
    case CHILD:
      digitalWrite(alarmPin, HIGH);
      Serial.println("Child alone in car!");
      break;
    case DRIVER:
      digitalWrite(alarmPin, LOW);
      Serial.println("Driver alone in car.");
      break;
    case DRIVERCHILD:
      digitalWrite(alarmPin, LOW);
      Serial.println("Child with driver in car.");
      break;
    default:
      digitalWrite(alarmPin, LOW);
      Serial.println("unknown state");
      break;
    }
  }

  // DO OTHER THINGS HERE

}

Robin2:

boolean childInSeat;

childInSeat = true;
if (childInSeat == true) {

Ah, I see, SoapyBoat has a point. You forgot to send it to the serial port :slight_smile:

KenF:
Ah, I see, SoapyBoat has a point. You forgot to send it to the serial port :slight_smile:

If this is a humorous comment I am perfectly happy.

If not, perhaps you would explain.

...R

PS Now that I have taken notice of the OP's name I can see why he is having a problem getting to grips with his project. But it's not worth getting into a lather over it.

Have you looked up blink without delay?

How do I add a delay for "Child alone in car!" without stopping the entire program (so, using millis(), which I haven;t quite grasped the concept of.)

Suppose your girlfriend called, and said "Meet me at my place in three hours, for wild passionate sex". Knowing that it takes 10 minutes to get there, would you sit and stare at the clock for over two hours? Or would you continue with whatever you were doing for almost three hours?

Sitting and staring at the clock is what delay() does.

Doing other things, and occasionally looking at the clock, is what the blink without delay example shows. "The clock" is the millis() function. It returns a time. Record the time when the "child alone in car" event is first detected. Periodically, check the time (that is, on every pass through loop()), to see if the "child alone in car" event happened long enough ago to be a problem.

Obviously, "child alone in car" while "parent pumps gas into tank" is not a problem, but "child alone in car" while "parent plays bingo for two hours" is.

Say, 1350000) and use that as a delay for "Child alone in car!". This gives 22.5 minutes for the adult to finish pumping gas, or to get their child out of the car to bring them to daycare.

Fine. Then use delay(). But, you keep saying that you don't want to use delay.

I give up. I've done everything possible to explain how to use millis().