1

So I have created this project to have an LED blinking at a constant speed whenever the program is running. The first LED (green) will blink faster and faster the closer an object gets to the sensor. The second LED (Blue(is referenced in code as white)) should be blinking constantly. The LED (blue) is only blinking when my ultrasonic sensor has something in range. I would like to make it so it blinks no matter the circumstances. This is probably something obvious Im missing - excuse my noob-ness NOTE: if the picture of the circuit isnt correct (pins not being correct) know that it is correct in real life, this is what I get for doing a quick mock up and post at 1:00 AM

#include "DHT.h";


#define DHTTYPE DHT22   // DHT Type is DHT 22 (AM2302)
#define DHTPIN 7       // DHT-22 Output Pin connection
#define trigPin 5
#define echoPin 6
#define beepPin 12
#define scalingfactor 10
#define blueLed 11

#define ledPin2 10

float hum;    // Stores humidity value in percent
float temp;
float soundsp;
float soundum;
 DHT dht(DHTPIN, DHTTYPE);  // Initialize DHT sensor for normal 16mhz Arduino

int centi = 0;

const long erval = 500;

unsigned long previous = 0;
unsigned long lastduration = 0;
unsigned long duration = 0; //time it takes for sound to return
unsigned long maxtime = 2000;
unsigned long i = 1;  //variable to determine actual distance
unsigned long time2 = 0;
unsigned long time1 = 0;



void setup() {
  Serial.begin (9600);  //open monirot channel
  dht.begin();  //start temp and humidity sensor
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(beepPin, OUTPUT);
  pinMode(blueLed, OUTPUT);
}


void calculate(){
      // Calculate the Speed of Sound in M/S in accordance with temp
    soundsp = 331.4 + (0.606 * temp) + (0.0124 * hum);
    
    // Convert to cm/us
    soundum = soundsp / 10000;
}

void logdistance() {
  digitalWrite(trigPin, HIGH); // send sound signal
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);  // stop sound signal
  duration = pulseIn(echoPin, HIGH);  // listen for sound signal
  centi = (duration* soundum)/2;   // determine delay of sound signal in one direction
  maxtime = scalingfactor * centi;  // change max time in accordance to the distance of the previous object
}


void beep() {
  digitalWrite(beepPin, HIGH);   // turn on the beep pin (or LED pin)
  delay(60);  
  logdistance();   // run the log distance function again so we get accurate data
  if (time2 - time1 < 30) {  // delay based on how close an object is
    delay((60 - (time2 - time1)));
  }
  digitalWrite(beepPin, LOW);  // turn off beeper (or LED)
  i = 1;
    while (1) {
    if (i % 60 == 0) { 
      logdistance();  // run my log distance funciton
    } 
    delay(1);
    ++i;  // add one to i to report that we have recorded the distance
    if (i >= maxtime) { // dont report distanc if sound took longer than max time to return
      break;
      } 
    }
}


void BlinkBlue (int interval){
   static long prevMill = 0;
   if (((long)millis() - prevMill) >= interval){ 
    prevMill = millis(); 
    digitalWrite(blueLed, !digitalRead(blueLed));
   }
}

void temps(){
  hum = dht.readHumidity();  // Get Humidity value
  temp= dht.readTemperature();  // Get Temperature value
}



void loop() {
  BlinkBlue(500); 
  beep();

  temps();
  calculate();




  //print results
  Serial.print (duration);
  Serial.print("c. ");
  Serial.print(centi);
  Serial.print(" ");
  Serial.print(temp);
  Serial.println(" ");
}

circuit

6
  • 1
    Start with the BlinkWithoutDelay example and go from there. Commented Mar 17, 2021 at 17:09
  • are you saying I need to save a log of my led blinks? The LED's blink based off when an object is detected. would that interfere with the process of it measuring distance? Commented Mar 17, 2021 at 17:12
  • sorry again- definitely new to this: im probably not picking up what youre putting down Commented Mar 17, 2021 at 17:18
  • In the IDE look at the File -> Examples menu. In there somewhere is one called "BlinkWithoutDelay". That will teach you how to do your LED blinking in a "non-blocking" manner so they don't interfere with each other. Commented Mar 17, 2021 at 17:24
  • okay so Ive made a few changes to the project. Ive updated the problem itself. Commented Mar 19, 2021 at 4:41

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.