Having problem with developing smart curtain system with bluetooth module

Hi, I am developing smart curtain system with voice control using Arduino Uno and bluetooth sensor
Almost of codes is already written. but I have to fix error with delay function.
I am using timer0_millis for time control.
But the one thing I want to fix is that because of delay in timer1 function,
I cannot change the value of a. but it need to be changed when voice is coming.
can anybody help me to fix this?

extern volatile unsigned long timer0_millis;
#include <SoftwareSerial.h>
#include <Stepper.h>
#include <dht.h>
#include <SimpleTimer.h>
SoftwareSerial BT(2,3);
const int stepsPerRevolution = 64;
Stepper myStepper(stepsPerRevolution, 11,9,10,8);
dht DHT;
#define DHT11_PIN A1
SimpleTimer timer;
char a = BT.read();
void setup()
{
Serial.begin(9600); //Sets the data rate in bits per second (baud) for serial data transmission
pinMode(7, OUTPUT);
myStepper.setSpeed(300);
BT.begin(9600);//Sets digital pin 13 as output pin

}
void loop(){

if(BT.available()){
char a = BT.read();
if (a=='1'){

digitalWrite(7,HIGH);
myStepper.step(2048);
delay(500);
Serial.print('1');

}
if (a=='2'){
automode();

}
if (a=='3'){

digitalWrite (7,LOW);
myStepper.step(-2048);
Serial.print('2');
delay(500);

} if (a=='4'){

digitalWrite (7,LOW);
myStepper.step(-2048);
Serial.print('2');
delay(500);

}
}

}

void automode(){

int chk = DHT.read11(DHT11_PIN);
int AnalogValue;
AnalogValue = analogRead(A0);

if(DHT.temperature>30&&AnalogValue>600){
  myStepper.step(-2048);
  timer1();
  
  
}else if(DHT.temperature>30&&AnalogValue<600){
 myStepper.step(2048); 
timer1();
}else if (DHT.temperature<30&&AnalogValue>600){
myStepper.step(2048);
timer1();
}else if (DHT.temperature<30&&AnalogValue<600){

myStepper.step(-2048);
timer1();
}
return;}
void timer1(){

  timer0_millis;
  
  if (timer0_millis>=100000){
    timer0_millis=0;}
   
    do{delay(5);}
    while(timer0_millis<100000);
    return;}

DON'T use 'timer0_millis' directly unless you are going to read it safely like the millis() function does:

unsigned long millis()
{
	unsigned long m;
	uint8_t oldSREG = SREG;

	// disable interrupts while we read timer0_millis or we might get an
	// inconsistent value (e.g. in the middle of a write to timer0_millis)
	cli();
	m = timer0_millis;
	SREG = oldSREG;

	return m;
}

Just change 'timer0_millis' in your sketch to 'millis()'.

Better yet, change your entire "timer1()" function to:
delay(100000ul);. It does the same thing, doesn't mess with the Arduino internals, and is easier to read.

You can use some tricks to simplify automode():

void automode()
{
  int chk = DHT.read11(DHT11_PIN);
  int AnalogValue = analogRead(A0);

  if (DHT.temperature > 30)
  {
    // HOT
    myStepper.step((AnalogValue > 600) ? -2048 : 2048);
  }
  else
  {
    // COLD
    myStepper.step((AnalogValue > 600) ? 2048 : -2048);
  }
  
  delay(100000ul); // Wait 100 seconds
}

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.