Need Help With Code

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
zivsi12
 
Posts: 2
Joined: Tue Nov 08, 2022 5:21 am

Need Help With Code

Post by zivsi12 »

Hi im working on a code that toggle a light on and off with a light senser. my problem is whenever i press the button it works but i can turn off the func if you can help me it would be great thanks.
Thats the func
Thats the func
Screenshot_1.png (16.67 KiB) Viewed 96 times

User avatar
blnkjns
 
Posts: 963
Joined: Fri Oct 02, 2020 3:33 am

Re: Need Help With Code

Post by blnkjns »

Please post code as code, not as image, that way we can put in suggestions.
Where does this code run on? Is it a part of more? Is it Visual Studio Platform IO?
Looks Arduinoish, but not completely.
I miss the main loop of the code. Also keep delays out of code whenever possible. It will give weird interaction.
Basically it has to be something like this (in pseudocode)

Code: Select all

boolean lampstate
int/float lightamount
upperlimit = somevalue
lowerlimit = somevalue

loop forever{
  put sensorvalue in lightamount
  if lightamount<lowerlimit set lampstate to on
  if lightamount>upperlimit set lampstate to off
  set light to lampstate
  small delay (1) or do other things.
}
The idea behind 2 values is that it prevents stuttering on the desired turn-on light level. Also make sure the lamp does not cause a feedback loop: the light may not light the sensor.

User avatar
zivsi12
 
Posts: 2
Joined: Tue Nov 08, 2022 5:21 am

Re: Need Help With Code

Post by zivsi12 »

Code: Select all

/*
By: Ziv Simoni
2022
*/
 #include "config.h"
 #include <WiFi.h>
 #include "WiFiClientSecure.h"
 #include <Adafruit_NeoPixel.h>
 #include "AdafruitIO_WiFi.h"
 #include <Wire.h>
 #include <BH1750.h>
 
#define LED_BUILTIN 2
 
 #define ssid "******"    //******
 #define pass "*******"   //********
 
 #define AIO_USERNAME  "zivsi12"            //personal info from adafruit.io
 #define AIO_KEY       "*********"
 #define AIO_SERVER      "io.adafruit.com"
 #define AIO_SERVERPORT  8883              // Using port 8883 for MQTTS
 
//sketch starts assuming the the light is off
boolean confirmRequestPending = true;
 
WiFiClientSecure client;  // WiFi ClientSecure for SSL/TLS support
 
AdafruitIO_WiFi io(AIO_USERNAME, AIO_KEY, ssid, pass);
 
BH1750 lightMeter; //LightSensor
 
// io.adafruit.com root CA
 const char* adafruitio_root_ca = \  
    "-----BEGIN CERTIFICATE-----\n" \
    "MIIDrzCCApegAwIBAgIQCDvgVpBCRrGhdWrJWZHHSjANBgkqhkiG9w0BAQUFADBh\n" \
    "MQswCQYDVQQGEwJVUzEVMBMGA1UEChMMRGlnaUNlcnQgSW5jMRkwFwYDVQQLExB3\n" \
    "d3cuZGlnaWNlcnQuY29tMSAwHgYDVQQDExdEaWdpQ2VydCBHbG9iYWwgUm9vdCBD\n" \
    "QTAeFw0wNjExMTAwMDAwMDBaFw0zMTExMTAwMDAwMDBaMGExCzAJBgNVBAYTAlVT\n" \
    "MRUwEwYDVQQKEwxEaWdpQ2VydCBJbmMxGTAXBgNVBAsTEHd3dy5kaWdpY2VydC5j\n" \
    "b20xIDAeBgNVBAMTF0RpZ2lDZXJ0IEdsb2JhbCBSb290IENBMIIBIjANBgkqhkiG\n" \
    "9w0BAQEFAAOCAQ8AMIIBCgKCAQEA4jvhEXLeqKTTo1eqUKKPC3eQyaKl7hLOllsB\n" \
    "CSDMAZOnTjC3U/dDxGkAV53ijSLdhwZAAIEJzs4bg7/fzTtxRuLWZscFs3YnFo97\n" \
    "nh6Vfe63SKMI2tavegw5BmV/Sl0fvBf4q77uKNd0f3p4mVmFaG5cIzJLv07A6Fpt\n" \
    "43C/dxC//AH2hdmoRBBYMql1GNXRor5H4idq9Joz+EkIYIvUX7Q6hL+hqkpMfT7P\n" \
    "T19sdl6gSzeRntwi5m3OFBqOasv+zbMUZBfHWymeMr/y7vrTC0LUq7dBMtoM1O/4\n" \
    "gdW7jVg/tRvoSSiicNoxBN33shbyTApOB6jtSj1etX+jkMOvJwIDAQABo2MwYTAO\n" \
    "BgNVHQ8BAf8EBAMCAYYwDwYDVR0TAQH/BAUwAwEB/zAdBgNVHQ4EFgQUA95QNVbR\n" \
    "TLtm8KPiGxvDl7I90VUwHwYDVR0jBBgwFoAUA95QNVbRTLtm8KPiGxvDl7I90VUw\n" \
    "DQYJKoZIhvcNAQEFBQADggEBAMucN6pIExIK+t1EnE9SsPTfrgT1eXkIoyQY/Esr\n" \
    "hMAtudXH/vTBH1jLuG2cenTnmCmrEbXjcKChzUyImZOMkXDiqw8cvpOp/2PV5Adg\n" \
    "06O/nVsJ8dWO41P0jmP6P6fbtGbfYmbW0W5BjfIttep3Sp+dWOIrWcBAI+0tKIJF\n" \
    "PnlUkiaY4IBIqDfv8NZ5YBberOgOzW6sRBc4L0na4UU+Krk2U886UAb3LujEV0ls\n" \
    "YSEY1QSteDwsOoBrp+uvFRTp2InBuThs4pFsiv9kuXclVzDAGySj4dzp30d8tbQk\n" \
    "CAUw7C29C79Fv1C5qfPrmAESrciIxpg0X40KPMbp1ZWVbd4=\n" \
    "-----END CERTIFICATE-----\n";
 
 
 AdafruitIO_Feed *led = io.feed("Led BuiltIn");
 AdafruitIO_Feed *light = io.feed("Light Sensor");
 AdafruitIO_Feed *toggleautol = io.feed("Toggle Auto Light");
 
int ledState = LOW;    // the current state of LED
int lastTouchState;    // the previous state of touch sensor
int currentTouchState; // the current state of touch sensor
 
void setup() {
  delay(100);
  pinMode(LED_BUILTIN, OUTPUT);
  Serial.begin(115200);
  WiFi.begin(ssid, pass);
   Serial.println("Connecting");
   while(WiFi.status() != WL_CONNECTED) {
    delay(250);
    Serial.print("NOT CONNECTED");
    delay(250);
   }  
   Serial.println("");
   Serial.print("Connected to WiFi network with IP Address: ");
   Serial.println(WiFi.localIP());
 
  client.setCACert(adafruitio_root_ca);  // Set Adafruit IO's root CA
  Serial.print("Connecting to Adafruit IO");
  io.connect();
  // set up a message handler for the 'color' feed.
  // the handleMessage function (defined below) will be called whenever a message is received from adafruit io.
  led->onMessage(handleMessage);
  led->get();
 
  Wire.begin();  //starts the lightsensor
  lightMeter.begin();
 
  toggleautol->onMessage(ToggleAutoLight);
  toggleautol->get();
 
  currentTouchState = touchRead(T0);
}
 

void loop() {
     //   Serial.println("loop starting");     //echo the message to terminal
  if(WiFi.status() != WL_CONNECTED) {
    delay(250);
    WiFi.begin(ssid, pass);
    io.connect();
    delay(250);
  }
    io.run(); //is required by adafruit. it should always be present at the top of your loop function. it keeps the client connected to io.adafruit.com, and processes any incoming data.
  delay(20);
  ToggleTouch();
  readlightSensor();
}
 
 
void readlightSensor() {
  float lux = lightMeter.readLightLevel();
  Serial.println("=========");
  // reads the status of the light and sends to adafruit io
  if(lux < 15) {
    Serial.println("Light Off");
    light->save(2);
  }
  else {
    Serial.println("Light On");
    light->save(1);
  }
  Serial.println("=========");
  delay(3000);
}
 
 
void handleMessage(AdafruitIO_Data *data) {
  Serial.print("Led Is -> ");
  if(data->toPinLevel() == HIGH)
    Serial.println("ON");
  else
    Serial.println("OFF");
 
  digitalWrite(LED_BUILTIN, data->toPinLevel());
}
 

void ToggleAutoLight(AdafruitIO_Data *data) {
  Serial.print("Auto Turn On is ->");
  while(data->toPinLevel() == 1) {
    Serial.println("ON");
    AutoFunc();
  }
  if(data->toPinLevel() == 0) {
    digitalWrite(LED_BUILTIN, LOW);
    Serial.println("OFF");
  }
}
 

void AutoFunc() {
  float lux = lightMeter.readLightLevel();
  if(lux < 15) {
    digitalWrite(LED_BUILTIN, HIGH);
    Serial.println("===");
  }
  else {
    Serial.println("***");
  }
}
 

void ToggleTouch() {
  lastTouchState    = currentTouchState;             // save the last state
  currentTouchState = touchRead(T0); // read new state
 
  if (lastTouchState > 35 && currentTouchState < 35) {
    Serial.println("The sensor is touched");
 
    // toggle state of LED
    ledState = !ledState;
 
    // control LED arccoding to the toggled state
    digitalWrite(2, ledState);
  }
}

Thats the code i use the arduino software to run the code

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”