Vehicle Security Project

Greetings,

I was hoping you folks might have some idea how to code something I’m working on with the photon. I’m thinking of using a photoresistor to read the status of the (aftermarket) Bluetooth module in my truck to lock the doors if motion is sensed and its not connected to my phone, also to unlock the doors if motion is sensed and my phone is connected. I will install a motion sensor that pulls a digital pin low with motion.

Part one:
If the led is flashing then write Bluetooth status is low (disconnected) , if the led is solid then write Bluetooth status high (connected).
Part two:
If Bluetooth value is low (phone not connected) and a digital input goes low (motion detected), trigger D7 high (lock doors).

And

If Bluetooth value is high (phone connected) and digital input goes low (motion detected), trigger D7 low (unlock doors).

The connection status is a blue led that blinks when searching and solid when paired. I have installed a photoresistor into the unit and it reads 400bits when off and 3500bits when on. it flashes about 2 times a second when pairing but goes right to solid when turned on and the phone is in range.

So how do i use an analog frequency reading to trigger an if statement?

The led flashes around 2hz, I have tried to average the readings so if its blinking the average is lower but it seems to get into some type of phase problem where it reads a bunch of high numbers then a bunch of low numbers. It really needs to be an instantaneous thing such as: if theres motion, turn on the bluetooth, read for 2 seconds and if there are any low readings lock the doors. If there are only high readings, unlock the doors.

below is my current code, its a bit messy as i keep trying new things. Any help is appreciated!!

// use ``` instead of ''' 

//#pragma SPARK_NO_PREPROCESSOR
#include "blynk/blynk.h"
#include "blynk/BlynkSimpleParticle.h"
#include "SparkCorePolledTimer/SparkCorePolledTimer.h"
#include "spark-dallas-temperature/spark-dallas-temperature.h"
#include "OneWire/OneWire.h"
#include "MCP23008-I2C/MCP23008-I2C.h"  //I2C STUFF
#define ONE_WIRE_BUS D6                 // Data wire is plugged into pin D5 on the particle
#define TEMPERATURE_PRECISION 12        // DS18B20 Thermometer Stuff
//#define lock BLYNK_WRITE(7)             // lock doors
//#define unlock BLYNK_WRITE(6)           // unlock doors

//char auth";  //BLYNK CE4
//char auth";  //BLYNK CE3
char auth[] = "AUTH_CODE_HERE";  //BLYNK CE2

OneWire oneWire(ONE_WIRE_BUS);          // DS18B20 Thermometer Stuff
DallasTemperature sensors(&oneWire);    // DS18B20 Thermometer Stuff
Adafruit_MCP23008 mcp;                  // I2C STUFF
SparkCorePolledTimer updateTimer(5000); //Create a timer object and set it's timeout in milliseconds
void OnTimer(void);                     //Prototype for timer callback method

float main, mainRaw, aux, auxRaw, acc, accRaw, light, lightRaw;
float bt, btRaw;


//averaging stuff
//const int aIn = A4;
//int positive;
//unsigned long period;
//unsigned long mark;
//end averaging stuff


//int lock(String command);
//int unlock(String command);


//defince temp address
DeviceAddress Thermometer1 = { 0x28, 0xFF, 0x44, 0x50, 0x16, 0x15, 0x3, 0xC };
DeviceAddress Thermometer2 = { 0x28, 0xFF, 0xD5, 0x4D, 0x16, 0x15, 0x3, 0xD3 };
DeviceAddress Thermometer3 = { 0x28, 0xFF, 0x2D, 0x37, 0x16, 0x15, 0x3, 0xF8 };
DeviceAddress Thermometer4 = { 0x28, 0xFF, 0x6A, 0x24, 0x16, 0x15, 0x3, 0xF0 };
//DeviceAddress Thermometer5 = { 0x28, 0xFF, 0xF5, 0x4F, 0x16, 0x15, 0x3, 0x2C };
// define temp bit resolution ie: int, float, double
double InTempC = -1;
double Temp1 = -1;
double Temp2 = -1; 
double Temp3 = -1;
double Temp4 = -1;
//double Temp5 = -1;
void update18B20Temp(DeviceAddress deviceAddress, double &tempC);
//end temp stuff

 //setupsetupsetupsetupsetupsetupsetupsetupsetupsetupsetupsetupsetupsetupsetupsetupsetupsetupsetupsetupsetupsetupsetupsetupsetup 
void setup(){
Serial.begin(9600);
Blynk.begin(auth);      // BLYNK initialization
updateTimer.SetCallback(OnTimer);
//Particle.function("lock", lock);
//Particle.function("unlock", unlock);

//averging
// while(analogRead(aIn)>512);//wait for positive half period to expire
//  while(analogRead(aIn)<512);//wait for negative half period to expire
//  mark = micros();//zero crossing from negative to positive found
//  while(analogRead(aIn)>512);//wait for positive half period to expire
//  while(analogRead(aIn)<512);//wait for negative half period to expire
//  period = micros()-mark;
//averaging


    sensors.begin();    // DS18B20 initialization
    sensors.setResolution(Thermometer1, TEMPERATURE_PRECISION);
    sensors.setResolution(Thermometer2, TEMPERATURE_PRECISION); 
    sensors.setResolution(Thermometer3, TEMPERATURE_PRECISION);
    sensors.setResolution(Thermometer4, TEMPERATURE_PRECISION);
//    sensors.setResolution(Thermometer5, TEMPERATURE_PRECISION);
pinMode(A0, INPUT); //
pinMode(A1, INPUT);
pinMode(A2, INPUT);
pinMode(A4, INPUT); //BLUETOOTH PHOTORESISTOR
pinMode(A5, INPUT);

//pinmode(lock, OUTPUT);
//pinmode(unlock, OUTPUT);



// I2C STUFF
mcp.begin();      // use default address 0
  mcp.pinMode(0, OUTPUT); 
  mcp.pinMode(1, OUTPUT);
  mcp.pinMode(2, OUTPUT);
  mcp.pinMode(3, OUTPUT);
  mcp.pinMode(4, OUTPUT);
  mcp.pinMode(5, OUTPUT);
  mcp.pinMode(6, OUTPUT);
  mcp.pinMode(7, OUTPUT);
// END I2C STUFF
}// END VOID SETUP BUT WHAT ABOUT I2C CODE BELOW BEFORE LOOP

// BLYNY WRITE NEEDS TO BE ABOVE LOOP BUT NOT IN SETUP LIKE THIS
// I2C STUFF
BLYNK_WRITE(0) {
    if (param.asInt()) {
        mcp.digitalWrite(0, HIGH);
        delay(3000);        
    } else {
       mcp.digitalWrite(0, LOW);}}
       
BLYNK_WRITE(1) {
    if (param.asInt()) {
        mcp.digitalWrite(1, HIGH);
    } else {
       mcp.digitalWrite(1, LOW);}}
       
BLYNK_WRITE(2) {
    if (param.asInt()) {
        mcp.digitalWrite(2, HIGH);
    } else {
       mcp.digitalWrite(2, LOW);}}
       
BLYNK_WRITE(3) {
    if (param.asInt()) {
        mcp.digitalWrite(3, HIGH);
    } else {
       mcp.digitalWrite(3, LOW);}}
       
BLYNK_WRITE(4) {
    if (param.asInt()) {
        mcp.digitalWrite(4, HIGH);
    } else {
       mcp.digitalWrite(4, LOW);}}
       
BLYNK_WRITE(5) {
    if (param.asInt()) {
        mcp.digitalWrite(5, HIGH);
    } else {
       mcp.digitalWrite(5, LOW);}}
       
BLYNK_WRITE(6) {
    if (param.asInt()) {
        mcp.digitalWrite(6, HIGH);
        delay(500);   
        mcp.digitalWrite(6, LOW);
        delay(500);  
        mcp.digitalWrite(6, HIGH);
        delay(500); 
    } else {
       mcp.digitalWrite(6, LOW);}}
        
BLYNK_WRITE(7) {
    if (param.asInt()) {
        mcp.digitalWrite(7, HIGH);
        delay(500);   
        mcp.digitalWrite(7, LOW);
        delay(500);  
        mcp.digitalWrite(7, HIGH);
        delay(500); 
    } else {
       mcp.digitalWrite(7, LOW);}}
//I2C STUFF


//looplooplooplooplooplooplooplooplooplooplooplooplooplooplooplooplooplooplooplooplooplooplooploop//looplooplooplooplooplooploopl
void loop()
{
Blynk.run();
updateTimer.Update();

}

void OnTimer(void) { //Handler for the timer, will be called automatically
        // DS18B20
        sensors.requestTemperatures();
        update18B20Temp(Thermometer1, InTempC);
        Temp1 = (InTempC * 9)/5 + 32;
        update18B20Temp(Thermometer2, InTempC);  
        Temp2 = (InTempC * 9)/5 + 31.33;  
        update18B20Temp(Thermometer3, InTempC);  
        Temp3 = (InTempC * 9)/5 + 31.67;  
        update18B20Temp(Thermometer4, InTempC);  
        Temp4 = (InTempC * 9)/5 + 32; 
//        update18B20Temp(Thermometer5, InTempC);
//        Temp5 = (InTempC * 9)/5 + 32.12;
//delay(5000); // READ DELAY - TOOK THIS OUT AND IT GOT JUMPY WITH -127'S C AND 196'S F        
updateTimer.Update();    // new stuff
}

void update18B20Temp(DeviceAddress deviceAddress, double &tempC)
{
  tempC = sensors.getTempC(deviceAddress);

// read temperature and discard reading between 150 and -30
if ( Temp1 < 150 && Temp1 > -30) {
    Blynk.virtualWrite(21, Temp1); 
}
if ( Temp2 < 150 && Temp2 > -30) {
    Blynk.virtualWrite(22, Temp2); 
}
if ( Temp3 < 150 && Temp3 > -30) {
    Blynk.virtualWrite(23, Temp3);
}
if ( Temp4 < 150 && Temp4 > -30) {
    Blynk.virtualWrite(24, Temp4);
}
//if ( Temp5 < 150 && Temp5 > -30) {
//    Blynk.virtualWrite(25, Temp5);
//}

// read analog ports
mainRaw = analogRead(A0);
auxRaw = analogRead(A1);
accRaw = analogRead(A2);
lightRaw = analogRead(A5);
btRaw = analogRead(A4);

// map analog input scale output
main = map(mainRaw, 0, 4096, 0, 1865);
aux = map(auxRaw, 0, 4096, 0, 1865);
acc = map(accRaw, 0, 4096, 0, 1865);
light = map(lightRaw, 0, 4050, 1100, 1500);
bt = map(btRaw, 300, 3500, 0, 100);
// correct display decimal place
main = (main / 100);
aux = (aux / 100);
acc = (acc / 100);
light = (light / 100);

Blynk.virtualWrite(10, main);
Blynk.virtualWrite(11, aux);
Blynk.virtualWrite(12, acc);
Blynk.virtualWrite(15, light);

Blynk.virtualWrite(14, bt);


}

I can give you some high level feedback.

You should not use your bluetooth kit to detect “safe” vs “stolen” status of your vehicle. What are you going to do when your phone battery is dead, or you forgot your phone, or you have bluetooth issues, etc etc. It’s just a strange way to go about it.

instead I recommend you create a simple app that you can use to “arm” and “disarm” the vehicle using the particle cloud over wifi. This will work anytime your vehicle is at home. When not at home, you could connect ad-hoc and control the alarm that way. This could all be done quite easily with the “blynk” app.

You could use BT to detect when your phone is near and automatically disarm, but I would see this as a second stage of the project, and get the basic alarm going first.

For a motion sensor, just buy a pre-made shock/motion sensor from an existing car alarm. Most of them are available separately, are adjustable, reliable, and easy to interface with. By the time the car is moving, it’s a little late to be intervening. Why does the thief care if you lock the doors on him… he’s already inside, he can easily over-ride the locks manually or just smash the window. You’d need to install aftermarket deadbolts if you really want to give him a rough time. The shock sensor, on the other hand, will set the alarm off when the thief smashes the window, or lock, or ignition cylinder. This will send a notification to your phone over wifi, so you can get out there with a baseball bat and potentially stop the thief (or you know, call the po-po).

You also want to make sure not to post authorisation credentials on the Internet :wink:

Thank you moors7, I forgot about that. Your insight in previous posts has been very helpful as well.

1 Like

Vinistois, I am going to be using this in a low risk area and I don’t have the same security concerns about this project yet. At the moment I’m focusing on the code, but I did name this post “vehicle security” and I appreciate your feedback. I am using a directed 598d for the motion http://www.amazon.com/gp/product/B0009VFOME?psc=1&redirect=true&ref_=oh_aui_detailpage_o01_s00

I have installed a hardware A/D converter so now i have a digital signal. I have tried so many things now and haven’t made any headway. What is the easiest way to read this into a if statement?

If D5 is changing low and high then false, If D5 is not changing then True.

It sounds so easy. PulseIn looked promising but i can t get it to function in this manner.

I thought pulse count over time should work but can’t figure that out either, so frustrating…

Take a look at interrupts in the docs. It has a ‘change’ variant which might be what you’re looking for.

Why the change D5 and D6 in this line? "
#define ONE_WIRE_BUS D6 // Data wire is plugged into pin D5 on the particle"

I was just moving the input around and didn’t change the note, its actually on D2 now.

I still need to figure this out, i looked for change in the interrupts but nothing stood out. this has actually been the most frustrating part of my build, I’m kinda stuck on it. I see people use frequency counting libraries but id rather not load a library for this, maybe i have to though. it seems like an east task, I’m such a noob i just grab code from here and there and make it work. the closest thing I’ve found is someone reading the led off their energy meter. there is so much going on with the code i can’t figure out what i need. its also written for a jee node but looks similar.

http://jeelabs.net/projects/cafe/wiki/Electricity_consumption_meter

Just skimming over your thread I’ve got some suggestions you could try.

Despite your photoresistor not giving you clean 0V vs. 3.3V the values are still well either side of the threshold to distinguish between HIGH and LOW, so you won’t need to analogRead() or use an external ADC to generate an interrupt trigger.

This would make your code a lot simpler like this

const int pinTrig = D2;
const     uint32_t msBlinking = 1000; // LED needs to be lit more than
volatile  uint32_t msLastTrig;
bool connected;

void trig()
{
  msLastTrig = millis();
}

void setup()
{
  pinMode(D7, OUTPUT);
  pinMode(pinTrig, INPUT);
  attachInterrupt(pinTrig, trig, RISING);
}

void loop()
{
  connected = (digitalRead(pinTrig) && (millis() - msLastTrig > msBlinking));

  digitalWrite(D7, connected);
}

This is the easiest version but lacks any logic to overcome short connection drops. It will immediately signal connected == false once there is only one short LOW on the pin.
But this should get you unstuck :wink:

Wow, thats pretty sweet! Thank you @ScruffR, I have tried the code and its very close. When the Bluetooth LED is off Connected is 1, when its blinking or connected it is 0. I have tried changing FALLING to RISING and have increased the msBlinking time to 2000 but has the same outcome. D6 is pulled LOW when the bluetooth led is on, so i thought perhaps the trigger should be rising. odd that changing this has no affect on the outcome. I’ll keep working on it and appreciate your help, I have found new hope and I’m sure i can get this working now, thanks again:)

//#pragma SPARK_NO_PREPROCESSOR
#include "blynk/blynk.h"
#include "blynk/BlynkSimpleParticle.h"
#include "SparkCorePolledTimer/SparkCorePolledTimer.h"
#include "spark-dallas-temperature/spark-dallas-temperature.h"
#include "OneWire/OneWire.h"
#include "MCP23008-I2C/MCP23008-I2C.h"  //I2C STUFF
#define ONE_WIRE_BUS D2                 // Data wire is plugged into pin D6 on the particle
#define TEMPERATURE_PRECISION 12        // DS18B20 Thermometer Stuff

//char auth[] = ;  //BLYNK CE2

WidgetLED led1(V31);    //BLYNK LED WIDGET on
WidgetLED led2(V30);    //BLYNK LED WIDGET off

const int pinTrig = D6;         //bt signal in
const     uint32_t msBlinking = 2000; // LED needs to be lit more than
volatile  uint32_t msLastTrig;
bool connected;





OneWire oneWire(ONE_WIRE_BUS);          // DS18B20 Thermometer Stuff
DallasTemperature sensors(&oneWire);    // DS18B20 Thermometer Stuff
Adafruit_MCP23008 mcp;                  // I2C STUFF
SparkCorePolledTimer updateTimer(5000); //Create a timer object and set it's timeout in milliseconds
void OnTimer(void);                     //Prototype for timer callback method

float main, mainRaw, aux, auxRaw, acc, accRaw, light, lightRaw;
//float bt, btRaw;

//define temp address
DeviceAddress Thermometer1 = { 0x28, 0xFF, 0x44, 0x50, 0x16, 0x15, 0x3, 0xC };      //outside temp
DeviceAddress Thermometer2 = { 0x28, 0xFF, 0xD5, 0x4D, 0x16, 0x15, 0x3, 0xD3 };     //inside temp
DeviceAddress Thermometer3 = { 0x28, 0xFF, 0x2D, 0x37, 0x16, 0x15, 0x3, 0xF8 };     //fridge temp
DeviceAddress Thermometer4 = { 0x28, 0xFF, 0x6A, 0x24, 0x16, 0x15, 0x3, 0xF0 };     //spare temp

// define temp bit resolution ie: int, float, double
double InTempC = -1;    //this line could probably get deleted, also 0 instead of -1
double Temp1 = -1;
double Temp2 = -1; 
double Temp3 = -1;
double Temp4 = -1;
//double Temp5 = -1;
void update18B20Temp(DeviceAddress deviceAddress, double &tempC);
//end temp stuff




void trig()
{
  msLastTrig = millis();
}










 //setupsetupsetupsetupsetupsetupsetupsetupsetupsetupsetupsetupsetupsetupsetupsetupsetupsetupsetupsetupsetupsetupsetupsetupsetup 
void setup(){
Serial.begin(9600);
Blynk.begin(auth);      // BLYNK initialization
updateTimer.SetCallback(OnTimer);
//Particle.function("lock", lock);
//Particle.function("unlock", unlock);


  pinMode(D7, OUTPUT);
  pinMode(pinTrig, INPUT);
  attachInterrupt(pinTrig, trig, RISING);





sensors.begin();    // DS18B20 initialization
sensors.setResolution(Thermometer1, TEMPERATURE_PRECISION);
sensors.setResolution(Thermometer2, TEMPERATURE_PRECISION); 
sensors.setResolution(Thermometer3, TEMPERATURE_PRECISION);
sensors.setResolution(Thermometer4, TEMPERATURE_PRECISION);

pinMode(A0, INPUT);     //main
pinMode(A1, INPUT);     //aux
pinMode(A2, INPUT);     //acc
//pinMode(A3, INPUT);     //spare
//pinMode(A4, INPUT);   //BLUETOOTH PHOTORESISTOR
pinMode(A5, INPUT);     //light
//pinMode(A6, INPUT);     //spare
//pinMode(A7, INPUT);     //spare


//pinMode(D0, INPUT);   //used by I2C Relays DO NOT ENABLE
//pinMode(D1, INPUT);   //used by I2C Relays DO NOT ENABLE
//pinMode(D2, INPUT);   //used by TEMP Sensors DO NOT ENABLE
//pinMode(D3, INPUT_PULLUP);    //BLUETOOTH PHOTORESISTOR A/D converter for pulseIn
//pinMode(D4, INPUT_PULLUP);    //outer motion trigger
//pinMode(D5, INPUT_PULLUP);    //inner motion trigger
//pinMode(D6, INPUT);  //BT
pinMode(D7, OUTPUT);

//pinmode(lock, OUTPUT);
//pinmode(unlock, OUTPUT);



// I2C STUFF
mcp.begin();      // use default address 0
  mcp.pinMode(0, OUTPUT); 
  mcp.pinMode(1, OUTPUT);
  mcp.pinMode(2, OUTPUT);
  mcp.pinMode(3, OUTPUT);
  mcp.pinMode(4, OUTPUT);
  mcp.pinMode(5, OUTPUT);
  mcp.pinMode(6, OUTPUT);
  mcp.pinMode(7, OUTPUT);
// END I2C STUFF
}// END VOID SETUP

// BLYNY WRITE NEEDS TO BE ABOVE LOOP BUT NOT IN SETUP LIKE THIS
// I2C STUFF
BLYNK_WRITE(0) {                        //bluetooth power
    if (param.asInt()) {
        mcp.digitalWrite(0, HIGH);
        delay(3000);        
    } else {
       mcp.digitalWrite(0, LOW);}}
       
BLYNK_WRITE(1) {                        //start/stop
    if (param.asInt()) {
        mcp.digitalWrite(1, HIGH);
    } else {
       mcp.digitalWrite(1, LOW);}}
       
BLYNK_WRITE(2) {                        //spare
    if (param.asInt()) {
        mcp.digitalWrite(2, HIGH);
    } else {
       mcp.digitalWrite(2, LOW);}}
       
BLYNK_WRITE(3) {                        //windows vent  
    if (param.asInt()) {
        mcp.digitalWrite(3, HIGH);
    } else {
       mcp.digitalWrite(3, LOW);}}
       
BLYNK_WRITE(4) {                        //windows down
    if (param.asInt()) {
        mcp.digitalWrite(4, HIGH);
    } else {
       mcp.digitalWrite(4, LOW);}}
       
BLYNK_WRITE(5) {                        //windows up
    if (param.asInt()) {
        mcp.digitalWrite(5, HIGH);
    } else {
       mcp.digitalWrite(5, LOW);}}
       
BLYNK_WRITE(6) {                        //unlock    
    if (param.asInt()) {
        mcp.digitalWrite(6, HIGH);
        delay(500);   
        mcp.digitalWrite(6, LOW);
        delay(500);  
        mcp.digitalWrite(6, HIGH);
        delay(500); 
    } else {
       mcp.digitalWrite(6, LOW);}}
        
BLYNK_WRITE(7) {                        //lock
    if (param.asInt()) {
        mcp.digitalWrite(7, HIGH);
        delay(500);   
        mcp.digitalWrite(7, LOW);
        delay(500);  
        mcp.digitalWrite(7, HIGH);
        delay(500); 
    } else {
       mcp.digitalWrite(7, LOW);}}
//I2C STUFF


//looplooplooplooplooplooplooplooplooplooplooplooplooplooplooplooplooplooplooplooplooplooplooploop//looplooplooplooplooplooploopl
void loop()
{
Blynk.run();
updateTimer.Update();






}

void OnTimer(void) { //Handler for the timer, will be called automatically
        // DS18B20 change to farenheight and calibrate - change to *1.8 + 32
        sensors.requestTemperatures();
        update18B20Temp(Thermometer1, InTempC);
        Temp1 = (InTempC * 9)/5 + 32;
        update18B20Temp(Thermometer2, InTempC);  
        Temp2 = (InTempC * 9)/5 + 31.33;  
        update18B20Temp(Thermometer3, InTempC);  
        Temp3 = (InTempC * 9)/5 + 31.67;  
        update18B20Temp(Thermometer4, InTempC);  
        Temp4 = (InTempC * 9)/5 + 32; 


updateTimer.Update();    // new stuff i dint think we need this one
}

void update18B20Temp(DeviceAddress deviceAddress, double &tempC)
{
  tempC = sensors.getTempC(deviceAddress);

// read temperature and discard reading between 150 and -30, write to blynk - add averaging
if ( Temp1 < 150 && Temp1 > -30) {
    Blynk.virtualWrite(21, Temp1); 
}
if ( Temp2 < 150 && Temp2 > -30) {
    Blynk.virtualWrite(22, Temp2); 
}
if ( Temp3 < 150 && Temp3 > -30) {
    Blynk.virtualWrite(23, Temp3);
}
if ( Temp4 < 150 && Temp4 > -30) {
    Blynk.virtualWrite(24, Temp4);
}

// read analog ports and rename for mapping and virtual pin assignment - add averaging
mainRaw = analogRead(A0);   //main battery voltage
auxRaw = analogRead(A1);    //aux battery voltage
accRaw = analogRead(A2);    //acc battery voltage
lightRaw = analogRead(A5);  //ambient light sensor
//btRaw = analogRead(A4);

// map analog input scale output
main = map(mainRaw, 0, 4096, 0, 1865);
aux = map(auxRaw, 0, 4096, 0, 1865);
acc = map(accRaw, 0, 4096, 0, 1865);
light = map(lightRaw, 0, 4050, 1100, 1500);
//bt = map(btRaw, 300, 3500, 0, 100);

// correct decimal place for blynk display
main = (main / 100);
aux = (aux / 100);
acc = (acc / 100);
light = (light / 100);

Blynk.virtualWrite(10, main);
Blynk.virtualWrite(11, aux);
Blynk.virtualWrite(12, acc);
//Blynk.virtualWrite(13, pulsecount);
Blynk.virtualWrite(14, connected);
Blynk.virtualWrite(15, light);


  connected = (digitalRead(pinTrig) && (millis() - msLastTrig > msBlinking));

  digitalWrite(D7, connected);



if ( connected == 0) //Bluetooth LED is solid
{
WidgetLED led1(31); //means you need to have LED on app configured on virtual pin 31.
led1.on();
}
else {led1.off();}

if ( connected == 1) //Bluetooth LED is blinking or off
{
WidgetLED led2(30); //means you need to have LED on app configured on virtual pin 30.
led2.on();
}
else {led2.off();}


}


Sorry, I must have lost you there - what exactly does my code not do?

If you want to invert the logic, the easiest way to do this is

  connected = !((digitalRead(pinTrig) && (millis() - msLastTrig > msBlinking)));

It doesn’t differentiate between pulsed and steady low input on D6. It does change state when no low input is sensed, and the length of time before the state change can be set with msBlinking.

I think i may need to use a wait time before checking D6, ill try that next. Or maybe i can look for more than one change in state, some thing like if it happens more that 4 times then change boolean?

Could it be that you have taken my first shot of the code, which was missing a crucial instruction :blush: ?
Since in my tests the code did just as that.

photoresistor sees
  solid off ... not connected
  blinking  ... not connected
  solid on  ... connected

Can you just try my code alone with anything else?
If needed I can shoot a video and show what I think is the desired behaviour, maybe I just misunderstand.
I’ll also test with D6 instead of D2.

How have you wired your photoresistor?

1 Like

Im very grateful for your assistance, i have flashed only your code and did some testing. as soon as D6 is pulled to ground D7 LED turns off. I rewired to pull to high and its working!!! obviously my ignorance is showing through today. Many thanks, your are very patient and you just made my day.

2 Likes

Just to follow up on this.
I hope you have a voltage divider in place to ensure good digital signal seperation and to reduce current draw when the photoresistor is “open”
This was my setup that worked reliably

If you turn the resistors around you just need to use the inverted code line posted earlier.

I have a 4.7K to ground but yes, it is the same circuit. I can’t thank you enough, i spent a few hours a night for the last 2 weeks trying to figure this out. There are so many ways to get the same outcome it becomes difficult to select the “right one.”

1 Like