Manual / automatic lighting control system

I’m trying to develop a lighting control system.
by the app should open a menu for modes:
Automatic: controls the lamp intensity by the BH1750 sensor, as darkness increases the intensity increases, and shows the lux variation data on the screen
Manual: I can control the intensity of the lamp through the Slider.

I’m using arduino uno, sensor BH1750, and ESP8266

#define BLYNK_PRINT Serial
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
#include <BlynkSimpleShieldEsp8266_HardSer.h>
#include <TimerOne.h> // Avaiable from http://www.arduino.cc/playground/Code/Timer1

//— BIBLIOTECAS NECESSÁRIAS Sensor BH1750 —
#include <Dimmer.h>
#include <Wire.h>
#include <BH1750.h>

//— DECLARAR VARIAVEIS GLOBAIS — sensor BH1750
Dimmer lamp(3);
BH1750 lightMeter(0X23);

void sensor();
void slider();

void sendUptime ()
{
uint16_t lux=lightMeter.readLightLevel();
Blynk.virtualWrite(4, lux);
delay(500);
}

// ---- Rotina do Slider e Dimmer —
volatile int i=0; // Variable to use as a counter volatile as it is in an interrupt
volatile boolean zero_cross=0; // Boolean to store a “switch” to tell us if we have crossed zero
int AC_pin = 3; // Output to Opto Triac
int brightness = 128; // Dimming level (0-128) 0 = on, 128 = 0ff
int freqStep = 60; // This is the delay-per-brightness step in microseconds.

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = “c4b2d5c565214b869a7478f13a929451”;

// Your WiFi credentials.
// Set password to “” for open networks.
char ssid[] = “Blynk”;
char pass[] = “franca9189”;

// Hardware Serial on Mega, Leonardo, Micro…
//#define EspSerial Serial1

// or Software Serial on Uno, Nano…
#include <SoftwareSerial.h>
SoftwareSerial EspSerial(6, 7); // RX, TX

// Your ESP8266 baud rate:
#define ESP8266_BAUD 9600

ESP8266 wifi(&EspSerial);

BLYNK_WRITE(V2) {
switch (param.asInt())
{
case 1:{ // Item 1
Serial.println(“Manual”); // Dimerização Slider
slider();
break;
}
case 2:{ // Item 2
Serial.println(“Automatico”); // Dimerização sensor BH1750
sensor();
break;
}
}
}

// — Sub-Rotina do Slider —
void slider(){

BLYNK_WRITE(V1)// slider brillo
{
int brillo = param.asInt();
brightness=brillo;
}
{
void zero_cross_detect() {
zero_cross = true;
i=0;
digitalWrite(AC_pin, LOW); // turn off TRIAC (and AC)
}

//Turn on the TRIAC at the appropriate time
}

void dim_check() {
if(zero_cross == true) {
if(i>=brightness) {
digitalWrite(AC_pin, HIGH); // turn on light
i=0; // reset time step counter
zero_cross = false; //reset zero cross detection
}
else {
i++; // increment time step counter
}
}
}

}
// — Sub-Rotina do Sensor BH1750 —

void sensor(){

uint16_t lux = lightMeter.readLightLevel();
Serial.print(“Light: “);
Serial.print(lux);
Serial.println(” lx”); // printa e pula a linha para o proximo comando

if (lux < 1200 ){

  uint16_t lux = lightMeter.readLightLevel();
  int valor = map(lux, 0, 1200, 200, 0); // Lê o valor em lux do sensor (de 0 a 1200 lux (estipulado como limite)) e converte para potência de acionamento do triac (de 100% a 0% (sendo uma proporção inversa))
  int ilumina = map (valor, 0, 200, 0, 50); // Lê a variável "valor" em porcentagem  e converte para um valor que será enviado para a lampada dimmerizar (de 0 a 200)
                                              
  Serial.print("valor: ");
  Serial.println(valor);

  Serial.print("ilumina: ");
  Serial.println(ilumina);

  lamp.set(ilumina); // Atualiza o valor da lâmpada

  delay(1500);

}

else {

  lamp.set(LOW); // Atualiza o valor da lâmpada

}

delay(1500);
}

void setup()
{
// Debug console
Serial.begin(9600);

// Set ESP8266 baud rate
EspSerial.begin(ESP8266_BAUD);
delay(10);

Blynk.begin(auth, wifi, ssid, pass, “192.168.1.39”,8080);
delay(10);
// You can also specify server:
//Blynk.begin(auth, wifi, ssid, pass, “blynk-cloud.com”, 80);
//Blynk.begin(auth, wifi, ssid, pass, IPAddress(192,168,1,100), 8080);

// — Configuração Inicial Slider Dimmer —
pinMode(AC_pin, OUTPUT); // Set the Triac pin as output
attachInterrupt(digitalPinToInterrupt(2), zero_cross_detect, RISING);

//attachInterrupt(0, zero_cross_detect, RISING); // Attach an Interupt to Pin 3 (interupt 0)
Timer1.initialize(freqStep); // Initialize TimerOne library
Timer1.attachInterrupt(dim_check, freqStep);

// — Configurações Iniciais Sensor BH1750 —
// INICIALIZAR A INTERFACE I2C
Wire.begin();

// CONFIGURANDO O BH1750
if (lightMeter.begin(BH1750::CONTINUOUS_HIGH_RES_MODE)) {
Serial.println(F(“BH1750 Advanced begin”));
}
else {
Serial.println(F(“Error initialising BH1750”));
}

// INICIALIZA LÂMPADA DIMERIZADA
lamp.begin();

}

void loop()
{
Blynk.run();
}

my program:

erro:

Usando biblioteca BlynkESP8266_Lib na pasta: C:\Users\Ewerton França\Documents\Arduino\libraries\BlynkESP8266_Lib (legacy)
Usando a biblioteca blynk-library-master na versão 0.5.4 na pasta: C:\Users\Ewerton França\Documents\Arduino\libraries\blynk-library-master
Usando biblioteca BlynkSimpleShieldEsp8266_HardSer na pasta: C:\Users\Ewerton França\Documents\Arduino\libraries\BlynkSimpleShieldEsp8266_HardSer (legacy)
Usando a biblioteca TimerOne na versão 1.1 na pasta: C:\Users\Ewerton França\Documents\Arduino\libraries\TimerOne
Usando a biblioteca Dimmer-master na versão 1.0 na pasta: C:\Users\Ewerton França\Documents\Arduino\libraries\Dimmer-master
Usando a biblioteca Wire na versão 1.0 na pasta: C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.15.0_x86__mdqgnx93n4wtt\hardware\arduino\avr\libraries\Wire
Usando a biblioteca BH1750-master na versão 1.1.3 na pasta: C:\Users\Ewerton França\Documents\Arduino\libraries\BH1750-master
Usando a biblioteca SoftwareSerial na versão 1.0 na pasta: C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.15.0_x86__mdqgnx93n4wtt\hardware\arduino\avr\libraries\SoftwareSerial
exit status 1
a function-definition is not allowed here before ‘{’ tokenPreformatted text

You need to edit your post to format the code so that it displays correctly…
Blynk - FTFC

Pete.

i have write a demo for u with my blockly software.

#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
char auth[] = "d9efdd0413ec4b74ab0057a0b8675654";
char ssid[] = "wifi-ssid";
char pass[] = "wifi-pass";
int mode;
 BLYNK_WRITE(V0) {
mode= param.asInt();
  Serial.println(mode);
}

int vpin_value;
 BLYNK_WRITE(V1) {
vpin_value= param.asInt();
  if (mode == 2) {
    analogWrite(12,vpin_value);
  }
}

BlynkTimer timer;
void myTimerEvent() {
  if (mode == 1) {
    analogWrite(12,analogRead(A0));
  }
}

void setup(){
   Serial.begin(9600);
 Blynk.begin(auth, ssid, pass,"blynk-cloud.com",8080);
  Serial.begin(9600);
  timer.setInterval(1000L, myTimerEvent);
}

void loop(){
  Blynk.run();
  timer.run();
}

If you want people to help identify the coding error, go back to post no 1, hit the pencil icon to edit it, and insert the backticks as shown in the graphic.

Pete.

1 Like