Hello, I am trying by means of a function that will send a high level for 5sec to turn on a led then it turns off, all this without stopping the program, that is why I am using millis, the problem I have is that I do not know much about the syntax of c++ and the program does not interact as I have thought, could you help me, I would appreciate it.
my code:
unsigned long time1, tx = 0;
int stadeL1 = LOW;
char comand;
int out = 4;
uint8_t executionTime(int timep){
time1 = millis();
if( (time1-tx) == timep ){
Serial.println("turn on");
digitalWrite(out, HIGH); //change status for 5 seconds
tx=time1;
}
//change status after 5 seconds
}
void setup() {
Serial.begin(9600);
pinMode(out, OUTPUT);
digitalWrite(out, stadeL1);
}
void loop() {
if ( Serial.available() > 0 ){
comand = Serial.read();
if( comand == '1' ){
Serial.println("Execute Function");
executionTime(1000);
} else {
Serial.println("Error");
}
comand="";
}
}
unsigned long startTime;
unsigned long currentTime;
unsigned long period = 5000;
const byte ledPin = 3;
void setup()
{
Serial.begin(115200);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, HIGH); //turn off the LED
}
void loop()
{
currentTime = millis();
if (Serial.available())
{
char inChar = Serial.read();
if (inChar == '1')
{
digitalWrite(ledPin, LOW); //turn on teh LED
startTime = currentTime;
}
}
if (startTime != 0) //timer is running
{
if (currentTime - startTime >= period)
{
digitalWrite(ledPin, HIGH); //turn off the LED
startTime = 0; //stop timing
}
}
}
Hello, I've been trying to put it in a function for hours, but Millis has a strange interaction, I don't know what's happening, could you help me again please.
unsigned long startTime;
unsigned long currentTime;
unsigned long period = 5000;
const byte ledPin = 4;
void setup()
{
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW); //turn off the LED
}
void loop(){
currentTime = millis();
if (Serial.available()){
char inChar = Serial.read();
if (inChar == '1'){
Funcion();
}
}
}
void Function(){
digitalWrite(ledPin, HIGH); //turn on teh LED
startTime = currentTime;
if (startTime != 0){ //timer is running
if (currentTime - startTime >= period){
digitalWrite(ledPin, LOW); //turn off the LED
startTime = 0; //stop timing
}
}
}