Updated specific locations to be searchable, take a look at Las Vegas as an example.

This post has been de-listed

It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.

1
Question about calculating time of pause and duration. Not a delay() question.
Post Body

I've gotten some help from a friend but it still is not clear to me and I hate to keep bugging someone.

Simple question really: When I run the below application, the goal is to use time to delay something from happening and then again to make that thing happen for a specified duration. In the case of the demo application, it senses an object. If that object remains in range for 4 seconds, it activates an LED for 5 seconds.

It works and it doesn't. I can change the duration of wait and of enabled but time isn't really right. And, if I remove my delay(500) at the end of the loop, it really fires up and it is like time doesn't exist. I'm looking for a little insight into what I either don't understand and or am doing incorrectly. I can fake it but I'd like it to be accurate.

Code in it's entirety, comments in black.

//----Setup OLED Display ----//

#include <SPI.h>

#include <Wire.h>

#include <Adafruit_GFX.h>

#include <Adafruit_SSD1306.h>

#define OLED_RESET 4 // Reset pin # (or -1 if sharing Arduino reset pin)

#define SCREEN_WIDTH 128 // OLED display width, in pixels

#define SCREEN_HEIGHT 32 // OLED display height, in pixels

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32

//Initialize I/O pins

int trigger_Pin = 8;

int echo_Pin = 9;

int fanRelay = 5;

int greenLed = 10;

int yellowLed = 3;

int redLed = 2;

Here's where the variables get setup

//Setup Fan Distance and Timer

float fanCountDown = 0.0; //float vs int - not sure I _need_ float but I see no harm

float fanOnSeconds = 0.0;

int minFanOnInches = 0;

int maxFanOnInches = 0;

//Default Vehicle State

bool isCarHome = false;

Here's where the value of the timed delay is set.

//Keep Fan From Coming On Immediately (ie: walking in range while the car is away)

float fanDelayCountDown = 0.0;

float fanDelaySeconds = 4.0;

bool isFanDelayApplied = false;

// for ultrasonic calcs

int inches = 0; // I'm an American...

int cm = 0;

//setup Ultrasonic sensor

long readUltrasonicDistance(int triggerPin, int echoPin)

{

pinMode(trigger_Pin, OUTPUT);

// Clear the trigger

digitalWrite(trigger_Pin, LOW);

delayMicroseconds(2);

// Sets the trigger pin to HIGH state for 10 microseconds

digitalWrite(trigger_Pin, HIGH);

delayMicroseconds(10);

digitalWrite(trigger_Pin, LOW);

pinMode(echo_Pin, INPUT);

// Reads the echo pin, and returns the sound wave travel time in microseconds

return pulseIn(echo_Pin, HIGH);

}

void setup() {

Serial.begin(9600); //Initialize Serial communication

// Pin and display setup

pinMode(echo_Pin, INPUT); //Echo pin as Input

pinMode(trigger_Pin, OUTPUT); //Trigger pin as Output

pinMode(fanRelay, OUTPUT); //LED represents fan

pinMode(redLed, OUTPUT);

pinMode(yellowLed, OUTPUT);

pinMode(greenLed, OUTPUT);

display.begin(SSD1306_SWITCHCAPVCC, 0x3C);

display.clearDisplay();

//OLED First run "Booting..."

display.clearDisplay();

display.setTextSize(1);

display.setTextColor(WHITE);

display.setCursor(0,0); //left, top

display.println("Status");

display.setTextSize(2);

display.setCursor(0,10); //left, top

display.println("Booting...");

display.display();

delay(3000);

}

//Let's get started...

void loop() {

//Fan Distance and Duration Variables

minFanOnInches = 5; // test value

maxFanOnInches = 10; // test value

This is where the run time value is set

fanOnSeconds = 5; // test value, actual is 15min / 900000MS

//measure distance and convert to cm

digitalWrite(trigger_Pin, LOW);

digitalWrite(trigger_Pin, HIGH);

cm = 0.01723 * readUltrasonicDistance(echo_Pin, echo_Pin);

// convert to inches by dividing by 2.54

inches = (cm / 2.54);

digitalWrite(trigger_Pin, LOW);

// LED INDICATOR LOGIC

//check distance and light LED indicators

//If car is out of range

if (inches > maxFanOnInches){

digitalWrite(yellowLed, HIGH);

} else {

digitalWrite(yellowLed, LOW);

}

//If car is in range to start fan

if (inches <= maxFanOnInches && inches >= minFanOnInches){

digitalWrite(greenLed, HIGH);

} else {

digitalWrite(greenLed, LOW);

}

//If car is home and fan should not start

if (inches < minFanOnInches){

digitalWrite(redLed, HIGH);

} else {

digitalWrite(redLed, LOW);

}

// FAN RELAY LOGIC

// Check if car within range...

// ------- Car is home -------------

if (inches <= maxFanOnInches && inches >= minFanOnInches){

// 1. Start fan delay count down

if (! isFanDelayApplied) {

isFanDelayApplied = true;

fanDelayCountDown = fanDelaySeconds;

}

// 2. Car is here, start fan run count down

else if (! isCarHome && fanDelayCountDown == 0)

{

// Turn On Fan and Init Count Down Timer

digitalWrite(fanRelay, HIGH);

//Timer amd CarStatus

fanCountDown = fanOnSeconds;

isCarHome = true;

//OLED Fan On"

display.clearDisplay();

display.setTextSize(1);

display.setTextColor(WHITE);

display.setCursor(0,0); //left, top

display.println("Countdown Running");

display.setTextSize(2);

display.setCursor(0,10); //left, top

display.print("Fan is on");

display.display();

}

}

// -------------- Car is away ---------------

else {

isCarHome = false;

isFanDelayApplied = false;

fanDelayCountDown = 0;

}

// Count down delay timer

if (fanDelayCountDown > 0) {

fanDelayCountDown--;

}

// Count Down Fan Timer

if (fanCountDown > 0) {

fanCountDown--;

}

// Turn Off Fan

else {

digitalWrite(fanRelay, LOW);

//OLED Fan State.

display.clearDisplay();

display.setTextSize(1);

display.setTextColor(WHITE);

display.setCursor(0,0); //left, top

display.println("Status");

display.setTextSize(2);

display.setCursor(0,10); //left, top

display.println("Fan is off");

display.display();

}

// Display variable values in serial console for testing

Serial.print(" fan = ");

Serial.print( digitalRead(12) );

Serial.print(" isCarHome = ");

Serial.print(isCarHome);

Serial.print(" inches = ");

Serial.print(inches);

Serial.print(" fanCountDown = ");

Serial.print(fanCountDown);

Serial.print(" fanDelayCountDown = ");

Serial.print(fanDelayCountDown);

Serial.print(" isFanDelayApplied = ");

Serial.println(isFanDelayApplied);

And here's a delay I have at the end of the loop. I intended to remove it but... then time has no meaning.

// Pause for a second

delay(500); // Wait for 500 millisecond(s)

}

Anyhow, I'm trying to learn and if someone can tell me what I'm not getting, that would be appreciated. Thanks.

Author
Account Strength
70%
Account Age
3 years
Verified Email
Yes
Verified Flair
No
Total Karma
4,938
Link Karma
1,529
Comment Karma
3,344
Profile updated: 4 days ago
Posts updated: 3 months ago

Subreddit

Post Details

We try to extract some basic information from the post title. This is not always successful or accurate, please use your best judgement and compare these values to the post title and body for confirmation.
Posted
2 years ago