-1

so I will explain what I want to get. I am reading temperature from DS18B20 and showing it on the LCD. I have one LED o pin 3 which is blinking when there is a temp reading and screen update. I have another LED on pin 4 which should lit when temp is more than 25 and less than 30, and I want it to blink when temp is more than 30. What I am getting is a blinking but not like one blink every 10ms but it waits for whole loop to start again to blink. Can I do it separately that this LED will blink indepentantly when condition is true? With set interval for example 10ms which wont be waiting for whole loop.

here is my example code:

    #include <OneWire.h>

#include <Wire.h> 
#include <LiquidCrystal_I2C.h>



LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);  // Set the LCD I2C address

//LiquidCrystal_I2C lcd(0x27, POSITIVE);  // Set the LCD I2C address
OneWire  ds(2);

unsigned long previousMillis = 0;
const long interval = 1000;
int ledState = LOW;
void setup()
{
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);

  lcd.begin(16,2);               // initialize the lcd 

}

void loop()

{
  unsigned long currentMillis = millis();

  byte i;
  byte present = 0;
  byte type_s;
  byte data[12];
  byte addr[8];
  float celsius;

  if ( !ds.search(addr)) {
    ds.reset_search();
    delay(250);
    return;
  }

  Serial.println();

  // the first ROM byte indicates which chip
  switch (addr[0]) {
    case 0x10:
      Serial.println("  Chip = DS18S20");  // or old DS1820
      type_s = 1;
      break;
    case 0x28:
      Serial.println("  Chip = DS18B20");
      type_s = 0;
      break;
    case 0x22:
      Serial.println("  Chip = DS1822");
      type_s = 0;
      break;
    default:
      Serial.println("Device is not a DS18x20 family device.");
      return;
  } 

  ds.reset();
  ds.select(addr);
  ds.write(0x44, 1);        // start conversion, with parasite power on at the end

  delay(850);     // maybe 750ms is enough, maybe not
  // we might do a ds.depower() here, but the reset will take care of it.

  present = ds.reset();
  ds.select(addr);    
  ds.write(0xBE);         // Read Scratchpad

  Serial.print("  Data = ");
  Serial.print(present, HEX);
  Serial.print(" ");
  for ( i = 0; i < 9; i++) {           // we need 9 bytes
    data[i] = ds.read();
    Serial.print(data[i], HEX);
    Serial.print(" ");
  }
  Serial.print(" CRC=");
  Serial.print(OneWire::crc8(data, 8), HEX);
  Serial.println();

  // Convert the data to actual temperature
  // because the result is a 16 bit signed integer, it should
  // be stored to an "int16_t" type, which is always 16 bits
  // even when compiled on a 32 bit processor.
  int16_t raw = (data[1] << 8) | data[0];
  if (type_s) {
    raw = raw << 3; // 9 bit resolution default
    if (data[7] == 0x10) {
      // "count remain" gives full 12 bit resolution
      raw = (raw & 0xFFF0) + 12 - data[6];
    }
  } else {
    byte cfg = (data[4] & 0x60);
    // at lower res, the low bits are undefined, so let's zero them
    if (cfg == 0x00) raw = raw & ~7;  // 9 bit resolution, 93.75 ms
    else if (cfg == 0x20) raw = raw & ~3; // 10 bit res, 187.5 ms
    else if (cfg == 0x40) raw = raw & ~1; // 11 bit res, 375 ms
    //// default is 12 bit resolution, 750 ms conversion time
  }
  celsius = (float)raw / 16.0;


  digitalWrite(3, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(10);              // wait for a second
  digitalWrite(3, LOW);    // turn the LED off by making the voltage LOW


   lcd.home ();
   lcd.print("Reading 1");
   lcd.setCursor( 0, 1);
   lcd.print("temp:");
   lcd.print(celsius);
   lcd.print("*C");

   if (celsius >= 25 && celsius < 30) {
    digitalWrite(4,HIGH);
   } else if (celsius >= 30) {
blink1();
   } else {
    digitalWrite(4,LOW);
   }

}

void blink1() {
digitalWrite(4,HIGH);
delay(50);
digitalWrite(4,LOW);
}
2

Here try this:

#include <OneWire.h>

#include <Wire.h> 
#include <LiquidCrystal_I2C.h>



LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);  // Set the LCD I2C address

//LiquidCrystal_I2C lcd(0x27, POSITIVE);  // Set the LCD I2C address
OneWire  ds(2);

const long interval = 1000; //İnterval at which to blink(milliseconds)
unsigned long previousMillis = 0;
int ledState = LOW;
void setup()
{
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);

  lcd.begin(16,2);               // initialize the lcd 

}

void loop()
{

  byte i;
  byte present = 0;
  byte type_s;
  byte data[12];
  byte addr[8];
  float celsius;

  if ( !ds.search(addr)) {
    ds.reset_search();
    delay(250);
    return;
  }

  Serial.println();

  // the first ROM byte indicates which chip
  switch (addr[0]) {
    case 0x10:
      Serial.println("  Chip = DS18S20");  // or old DS1820
      type_s = 1;
      break;
    case 0x28:
      Serial.println("  Chip = DS18B20");
      type_s = 0;
      break;
    case 0x22:
      Serial.println("  Chip = DS1822");
      type_s = 0;
      break;
    default:
      Serial.println("Device is not a DS18x20 family device.");
      return;
  } 

  ds.reset();
  ds.select(addr);
  ds.write(0x44, 1);        // start conversion, with parasite power on at the end

  delay(850);     // maybe 750ms is enough, maybe not
  // we might do a ds.depower() here, but the reset will take care of it.

  present = ds.reset();
  ds.select(addr);    
  ds.write(0xBE);         // Read Scratchpad

  Serial.print("  Data = ");
  Serial.print(present, HEX);
  Serial.print(" ");
  for ( i = 0; i < 9; i++) {           // we need 9 bytes
    data[i] = ds.read();
    Serial.print(data[i], HEX);
    Serial.print(" ");
  }
  Serial.print(" CRC=");
  Serial.print(OneWire::crc8(data, 8), HEX);
  Serial.println();

  // Convert the data to actual temperature
  // because the result is a 16 bit signed integer, it should
  // be stored to an "int16_t" type, which is always 16 bits
  // even when compiled on a 32 bit processor.
  int16_t raw = (data[1] << 8) | data[0];
  if (type_s) {
    raw = raw << 3; // 9 bit resolution default
    if (data[7] == 0x10) {
      // "count remain" gives full 12 bit resolution
      raw = (raw & 0xFFF0) + 12 - data[6];
    }
  } else {
    byte cfg = (data[4] & 0x60);
    // at lower res, the low bits are undefined, so let's zero them
    if (cfg == 0x00) raw = raw & ~7;  // 9 bit resolution, 93.75 ms
    else if (cfg == 0x20) raw = raw & ~3; // 10 bit res, 187.5 ms
    else if (cfg == 0x40) raw = raw & ~1; // 11 bit res, 375 ms
    //// default is 12 bit resolution, 750 ms conversion time
  }
  celsius = (float)raw / 16.0;

   lcd.home ();
   lcd.print("Reading 1");
   lcd.setCursor( 0, 1);
   lcd.print("temp:");
   lcd.print(celsius);
   lcd.print("*C");

   if (celsius >= 25 && celsius < 30) {
    digitalWrite(4,HIGH);
   } else if (celsius >= 30) {
blink1();
   } else {
    digitalWrite(4,LOW);
   }

   unsigned long currentMillis = millis();

   if (currentMillis - previousMillis >= interval) {

     previousMillis = currentMillis;

     if (ledState == LOW) {
       ledState = HIGH;
     } else {
       ledState = LOW;
     }

     digitalWrite(3, ledState);
   }
}

void blink1() {
digitalWrite(4,HIGH);
delay(50);
digitalWrite(4,LOW);
}

and also note that i choosed 1000 milliseconds for interval (equal to 1 second) so if you want 10ms as in your first post then change const long interval to 10

protected by VE7JRO Oct 29 at 15:48

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.