I am trying to make a code to control the intensity of incandescent lamp (AC voltage). I use a signal zero cross circuit to synchronize the signal control of the ac load. The value of the intensity is given by a bluetooth device (android). The initial intensity is 50%. My code is the following:
Code: Select all
#include "BluetoothSerial.h"
BluetoothSerial SerialBT;
short eValvula=4;
short OPTO = 34;
float intensity;
unsigned long shooting_time = 8333;
double t1, t1_1;
long tiempoBLE_1=0;
void setup()
{
//Serial.begin(115200);
SerialBT.begin("Chi"); //Bluetooth device name
pinMode(eValvula,OUTPUT);
pinMode(OPTO, INPUT);
}
void loop()
{
if (SerialBT.available())
{
intensity=SerialBT.parseFloat();
}
shooting_time = map(intensity, 0, 100, 8333, 0);
if (digitalRead(OPTO)==1)
{
digitalWrite(eValvula, LOW);
//SerialBT.println(t1-t1_1);
// SerialBT.print(" ");
// SerialBT.println(intensity);
t1_1 = micros();
}
t1 = micros();
if ((abs(t1 - t1_1) >= shooting_time) && (abs(t1 - t1_1) <= 8333))
{
digitalWrite(eValvula, HIGH);
}
else
{
digitalWrite(eValvula, LOW);
}
if(millis()-tiempoBLE_1>7)
{
tiempoBLE_1=millis();
SerialBT.println(tiempoBLE_1-millis());
}
}