1

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;
}
2
  • 1
    Sorry, your code doesn't make much sense to me. You include SimpleTimer, but you are not using it. You declare timer0_millis from extern as volatile, but you are using it just as a simple variable and not inside an ISR. And you use delay() and timer0_millis together. – chrisl Jun 5 at 14:56
  • 3
    Do not use timer0_millis directly: it's an undocumented implementation detail, and you have a data race every time you access it. Use millis() instead. See Blink without delay. – Edgar Bonet Jun 5 at 15:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.