Go Down

Topic: Blink LED without using delay in the main loop (Read 5871 times) previous topic - next topic

yee0722

ok. thank you so much! I did it. I will upload my sketch after finish edit. Thanks again for helping.

Paul__B

#16
May 10, 2014, 02:30 pm Last Edit: May 10, 2014, 02:43 pm by Paul__B Reason: 1
Code: [Select]

//declare variables
const int buzzer  = 5;
const int yellow1 = 3;      // warning temp
const int ovr_tmp = 4;      // overtemp
const int flm_ind = 2;      // flame led indicate flame

float tempC;
float voltage;
int tempPin =0;             // Temp sensor plugged analog pin 0
int flamePin= 1;

int warnState    = LOW;     // initialise the LED
int ovr_State    = LOW;
int flm_State    = LOW;
boolean _warning = false;   // condition reports
boolean _ovr_tmp = false;
boolean _flame   = false;

unsigned long warn_cnt = 0; // will store last time LED was updated
unsigned long over_cnt = 0;
unsigned long flame_cnt = 0;
unsigned long poll_cnt = 0; // Last time sensors polled


// Have we completed the specified interval since last confirmed event?
// "marker" chooses which counter to check
boolean timeout(unsigned long *marker, unsigned long interval) {
  if (millis() - *marker >= interval) {
    *marker += interval;    // move on ready for next interval
    return true;       
  }
  else return false;
}


// write setup function
void setup()
{
  Serial.begin(9600);// open serial port to communicate with the temp sensor
  pinMode (ovr_tmp, OUTPUT);
  pinMode (yellow1, OUTPUT); 
  pinMode (flm_ind, OUTPUT);
  pinMode (buzzer,  OUTPUT);

}

//write loop that will control what we want the arduino to do with the sensor readout

void loop()
{
  if (timeout(&poll_cnt, 3000UL )) {  // Execute each 3 seconds
    tempC = analogRead(tempPin);      // taking the temp pin reading and setting it equal to tempC variable
    tempC = (5.0*tempC*100.0)/1024.0; // will convert the analog input to a temperature in celcius
    Serial.print((byte)tempC);        // will output the converted temperature to pc
    Serial.println("02");             // xbee 2

    voltage = analogRead(flamePin);
    voltage = (5.0*voltage)/1023.0;
    Serial.print("0");
    Serial.print((byte)voltage);
    Serial.println("04");             // flame sensor from xbee2

    ////////////////////////////////////////////////////////////   
    if (tempC>34)  // over temp
    {
      if (voltage <= 3)              // there is flame and temp >40, give fire alert!
      {
        _flame = true;               // flame detected
        digitalWrite (buzzer, HIGH); // give alert
      }
      else                           // temp>40, but no flame, possibility of false alarm
      {
        _flame = false;              // no flame detected
        digitalWrite (buzzer, LOW);  // no alert
      }
      _ovr_tmp = true;               // over temp
      _warning = false;              // not required     
    }   

    //////////////////////////////////////////////////////////////
    else if ((tempC > 30) && (tempC <= 34))  //warning temp range
    {
      if (voltage <= 3)              //there is flame and temp in warning range
      {
        _flame = true;               // flame detected
        digitalWrite (buzzer, HIGH); // give alert
      }
      else  //warning temp, but no flame, possibility of false alarm
      {
        _flame = false;              // no flame detected
        digitalWrite (buzzer, LOW);  // no alert   
      }
      _ovr_tmp = false;              // not strictly over temp
      _warning = true;               // give warning
    }   
    else  //not in warning range and no flame
    {
      _flame = false;                // no flame detected
      _warning = false;              // no panic
      _ovr_tmp = false;              // not over temp
      digitalWrite (buzzer, LOW);    // no alert
    }

  } // End 3 second procedures

  // Perform the indications
  if (timeout(&warn_cnt, 2000UL )) {
    if (_warning) {
      if (warnState == LOW) warnState = HIGH;
      else warnState = LOW;
    }
    else warnState = LOW;
    digitalWrite(yellow1, warnState);
  }

  if (timeout(&over_cnt, 1000UL )) {
    if (_ovr_tmp) { 
      if (ovr_State == LOW) ovr_State = HIGH;
      else ovr_State = LOW;
    }
    else ovr_State = LOW;
    digitalWrite(ovr_tmp, ovr_State);
  }

  if (timeout(&flame_cnt, 400UL )) {
    if (_flame ) {   
      if (flm_State == LOW) flm_State = HIGH;
      else flm_State = LOW;
    }
    else flm_State = LOW;
    digitalWrite(flm_ind, flm_State);
  }
}


You may wish to alter the blink times I have picked.   :D

yee0722

FIrst, this is sooooo awesome! Thank you so much help me make my project look much better!

I still very much to learn in Arduino. Thank you again!

Paul__B

So I would really appreciate it if you could tell me how you fare with that code.

I presume you have your rig already set up to test it; I am reasonably sure about the parts I have put in, but don't know how the parts to do with your Xbee and such work.   :D

yee0722

Yess I tested it and it works perfect. haha. The XBee that I set is just an offset to be differentiate between two Xbee in Labview..

The code you written is much advanced for me.  :D Seems I need dig some coding book to understand more.  :)

Paul__B


The code you written is much advanced for me.  :D Seems I need dig some coding book to understand more.  :)


Well, part of the concept is that it is somewhat "modular".  So you can easily use the "timeout" function for whatever else you need to perform at given intervals simply by copying the function call where you need it, providing its own separate "counter" in the variable declarations and specifying the number of milliseconds it needs to delay.  (I trust you have tried different values as an exercise).

In fact, you can if necessary, choose how many milliseconds to wait for each time you call the function (noting that the delay time is an "unsigned long" or "UL").

I have left it to you if you wish to do this to make the buzzer intermittent.  You can even give it a particular cadence.

Go Up