Optoisolator code

Hello,

I am a teacher, and I'm working with a class on a project. We are relatively new at coding in arduino so please forgive us.

We are designing a device to mitigate hot car deaths of children and pets. The particular part that we are having trouble with is designed to sense two inputs:

1) Is the ignition in the car on? 2) Is the weight sensor in the front seat of the car activated?

We have our optoisolator circuits correct, as we are able to test them independently with the LED and the LED lights up at the appropriate time when each of the (1) and (2) above are turned "off."

Our code should use the following logic: If the ignition is off and the weight sensor is triggered (on), the we should return a positive signal.

Here is the code we have:

We initially started with LEDs so that we could see them as independent indicators. They work independently (if the weight batteries are switched on, the LED lights up) and the same is true for the ignition battery. We cannot get the LEDs to light up at the same time, so we decided to try serial monitor instead.

Here is our code: int ignition_LED = 12 ; // LED connected to digital pin int weight_LED = 11;

int ignition_input = 7; // pushbutton connected to digital pin 7 int weight_input = 2;

int ignition_val = 0; // variable to store the read value int weight_val = 0;

void setup() { pinMode(ignition_LED, OUTPUT); // sets the digital pin 13 as output pinMode(ignition_input, INPUT); // sets the digital pin 7 as input pinMode(weight_input, INPUT);

}

void loop() { ignition_val = digitalRead(ignition_input); // read the input pin digitalWrite(ignition_LED, ignition_val); delay(1000);// sets the LED to the button's value

weight_val = digitalRead(weight_input); // read the input pin digitalWrite(weight_LED, weight_val); // sets the LED to the button's value delay(1000);

Serial.begin(9600); Serial.println("ignition_val" + ignition_val); Serial.println("weight_val" + weight_val);

}

This prints out " weight_val gnition_val weight_val gnition_val weight_val"

First, how can I get it to read the values from PIN 11 and 12 as high or low and translate them to yes or no. Then I can use help with the If Then statement as well.

Thanks so much in advance!

Beth

it's not doing any particular harm, but this:

Serial.begin(9600);

Only needs to run once - it belongs in setup.

As you observe, this doesn't do what you want:

  Serial.println("ignition_val" + ignition_val);

Rather:

  Serial.print("ignition_val ");
  Serial.println(ignition_val);

Do you really want to read from pins 11 and 12 ? After all, your program set them either HIGH or LOW so you should know their state. It seems more likely that you want to read from pins 7 and 2. If you really want to read from them you can just use digitalRead() despite the fact that they are defined as outputs.

if (digitalRead(ignition_LED) == HIGH)
{
  Serial.println("something");
}
else
{
  Serial.println("something else");
}

Some other observations Put the Serial.begin() in setup() so that it only occurs once. "ignition_val" + ignition_valIs not how you concatenate text with a variable. You would be better off printing them with separate print statements.

It is not clear from your description how the switches are wired. Are they held HIGH or LOW with resistors to ensure that the state is not floating between the two ? If neither then I suggest that you use

pinMode(ignition_input, INPUT_PULLUP);

to activate the built in pullup resistor and wire the switches to take the pin LOW when activated.

1) Is the ignition in the car on? 2) Is the weight sensor in the front seat of the car activated?

If you use INPUT_PULLUP then your code boils down to

if (digitalRead(ignition_input) == LOW && digitalRead(weight_input == LOW))  //ignition on and seat occupied
{
  //code to read the temperature
  if (temp >= threshold)  //temperature too high
    {
       //make a noise, flash a light, whatever
    }
}

Thank you so much! From your helpful and patient posts, my students and I have been able to get this output:

Adult Present Ignition Off

Adult Present Ignition On

Adult Not Present Ignition Off

and

Adult Not Present Ignition On

We used the following code based on your suggestions!

int ignition_LED = 12 ; // LED connected to digital pin int weight_LED = 11;

int ignition_input = 7; // pushbutton connected to digital pin 7 int weight_input = 2;

int ignition_val = 0; // variable to store the read value int weight_val = 0;

void setup() { pinMode(ignition_LED, OUTPUT); // sets the digital pin 13 as output pinMode(weight_LED,OUTPUT); pinMode(ignition_input, INPUT); // sets the digital pin 7 as input pinMode(weight_input, INPUT);

Serial.begin(9600);

}

void loop() { if (digitalRead(ignition_input) == HIGH) { Serial.println("Ignition Off");

}

else { Serial.println("Ignition On"); }

{ if (digitalRead(weight_input) == HIGH) { Serial.println("Adult Present");

}

else { Serial.println("Adult Not Present");

delay(2000); }

}

}

With this code, I also understand that the LEDs are now extraneous.

A bigger logic question that I now have follows:

This portion (ignition and weight) are just a small part of the logic of the code needed for our device. Next for example is the output of an Infrared Sensor that will output into the serial monitor "Baby Present" or "Baby Not Present" based on some code that we created with it(remember we are mitigating hot car deaths).

Should I just read the serial monitor directly?

Our new if statement is:

If "Person Not Present" and "Ignition Off" initialize IR Sensor.

If IR Sensor says "Baby Present" send SMS text to owner (another set of code that we have working on its own).

How do you suggest that we move forward?

Next for example is the output of an Infrared Sensor

Read the sensor and use its value as the basis of another if test as you do with the other sensors. Output a message to Serial by all means but you can't read from the Serial monitor in the way that you suggest, nor do you need to.

If "Person Not Present" and "Ignition Off" initialize IR Sensor.

Personally I would consider unconditionally read all 3 sensors and base subsequent tests on the values returned although you could certainly nest the tests as you suggest. Having said that, if you do it that way then test whether a baby is present and only read the other sensors if it is true.

As you have seen, there is often more than one way to achieve what you want.

Suggested pseudo code for you to think about

start of loop()
  read baby, temp and adult sensors
  if baby present
      if temp too high
        if adult not present
          call function to send text
        end if
      end if
  end if
end of loop()

Personally, I can not see your code achieving the results you expect. People don't forget their kids in the car. Sending a text message telling them that they did is pointless.

PaulS: Personally, I can not see your code achieving the results you expect. People don't forget their kids in the car. Sending a text message telling them that they did is pointless.

Unfortunately they do... I like the project. Mother of toddler who died in car in Idaho Falls pleads guilty This was sad :confused: the mother of several children returns from the grocery store and grabs the groceries with her other older children helping and thinking the oldest child had brought the infant in. The judge was totally lenient with his ruling but the damage was already done! I can't imagine losing one of my children. This type of technology should be available in all cars! I hope your project sparks the way to prevent further needless deaths.

Thank you all for your help thus far, especially UKHeliBob!

PaulS: For your leasure reading: Pulitzer Prize Winning Article

We have now integrated our IR Sensor:

Here is our code:

#include <Wire.h>

#include “WireExt.h”

int ignition_LED = 12 ; // Ignition/weight LED connected to digital pin
int weight_LED = 11; // Ignition/weight
int ignition_input = 7; // Ignition/weight pushbutton connected to digital pin 7
int weight_input = 2; // Ignition/weight
int ignition_val = 0; // Ignition/weight variable to store the read value
int weight_val = 0; // Ignition/weight
int i; //IR Sensor

#define D6T_addr 0x0A // Address of OMRON D6T is 0x0A in hex //IR Sensor
#define D6T_cmd 0x4C // Standard command is 4C in hex //IR Sensor

int rbuf[35]; // Actual raw data is coming in as 35 bytes. Hence the needed for WireExt so that we can handle more than 32 bytes //IR Sensor
int tdata[16]; // The data comming from the sensor is 16 elements, in a 16x1 array //IR Sensor
float t_PTAT; //IR Sensor

void setup()
{
pinMode(ignition_LED, OUTPUT); // Ignition/weight sets the digital pin 13 as output
pinMode(weight_LED,OUTPUT); // Ignition/weight
pinMode(ignition_input, INPUT); // Ignition/weight sets the digital pin 7 as input
pinMode(weight_input, INPUT); // Ignition/weight
Wire.begin(); //IR Sensor
pinMode(13, OUTPUT); //IR Sensor
Serial.begin(9600);
}

void loop()
{
if (digitalRead(ignition_input) == HIGH) // Ignition/weight
{
Serial.println(“Ignition Off”);// Ignition/weight
}
else // Ignition/weight
{
Serial.println(“Ignition On”);// Ignition/weight
}

if (digitalRead(weight_input) == HIGH) // Ignition/weight
{
Serial.println(“Adult Present”);// Ignition/weight
}
else // Ignition/weight
{
Serial.println(“Adult Not Present”); // Ignition/weight
delay(1000);// Ignition/weight
}

if ((digitalRead(ignition_input) == HIGH) && (digitalRead(weight_input) == LOW)) // Ignition/weight
{
Serial.println(“Initialize Sensing”); // Ignition/weight
}
else // Ignition/weight
{
Serial.println(“Do Not Sense”); // Ignition/weight
delay(4000); // Ignition/weight
}

Wire.beginTransmission(D6T_addr);//IR Sensor
Wire.write(D6T_cmd);//IR Sensor
Wire.endTransmission();//IR Sensor

if (WireExt.beginReception(D6T_addr) >= 0)//IR Sensor
{
i = 0;//IR Sensor
for (i = 0; i < 35; i++) //IR Sensor
{
rbuf = WireExt.get_byte();//IR Sensor

  • }*
  • WireExt.endReception();//IR Sensor*
    t_PTAT = (rbuf[0] + (rbuf[1] << 8)) * 0.1; // Unclear what this is used for. It’s not called elsewhere
  • for (i = 0; i < 16; i++)//IR Sensor*
  • {*
    tdata = (rbuf[(i * 2 + 2)] + (rbuf[(i * 2 + 3)] << 8)) * 0.1; //IR Sensor
    * }*
    * }*
    * // Data is captured into tdata. This should be a 16x1 data. For now we can output as a csv string which we can import to Matlab to sequence*
    * for (i = 0; i < 4; i++) //IR Sensor*
    * {*
    _ Serial.print(tdata*); //IR Sensor*
    * Serial.print(",");//IR Sensor*
    * }
    Serial.print("\n"); //IR Sensor*
    * // Data is captured into tdata. This should be a 4x1 data. For now we can output as a csv string which we can import to Matlab to sequence*
    * for (i = 4; i < 8; i++) //IR Sensor*
    * {
    Serial.print(tdata);//IR Sensor*

    * Serial.print(",");//IR Sensor*
    * }
    Serial.print("\n");//IR Sensor*

    * // Data is captured into tdata. This should be a 4x1 data. For now we can output as a csv string which we can import to Matlab to sequence*
    * for (i = 8; i < 12; i++) //IR Sensor*
    * {
    Serial.print(tdata);//IR Sensor*

    * Serial.print(",");//IR Sensor*
    * }
    Serial.print("\n");
    // Data is captured into tdata. This should be a 4x1 data. For now we can output as a csv string which we can import to Matlab to sequence*

    * for (i = 12; i < 16; i++) //IR Sensor*
    * {
    Serial.print(tdata);//IR Sensor*

    * Serial.print(",");//IR Sensor*
    * }
    Serial.print("\n");//IR Sensor*

    * Serial.print("\n");//IR Sensor*
    * int CurrentLowestTemp = 999;//IR Sensor*
    * int CurrentHighestTemp = 0;//IR Sensor*
    * for (int i = 0; i < 16; i++) //IR Sensor*
    * {
    if (tdata < CurrentLowestTemp) //IR Sensor
    {
    CurrentLowestTemp = tdata;//IR Sensor*

    * }
    }
    Serial.print("CurrentLowest: ");//IR Sensor*

    * Serial.println(CurrentLowestTemp);//IR Sensor*_

* for (int i = 0; i < 16; i++)//IR Sensor*
* {*
_ if (tdata > CurrentHighestTemp)//IR Sensor
* {
CurrentHighestTemp = tdata;//IR Sensor*

* }
}
Serial.print("CurrentHighest: ");//IR Sensor*

* Serial.println(CurrentHighestTemp);//IR Sensor*_

// (“TempDiff”)
* int TempDiff = CurrentHighestTemp - CurrentLowestTemp;*
* Serial.print("TempDiff: ");*
* Serial.print(TempDiff);*
* Serial.println();*
* if (TempDiff > 0 && TempDiff < 4) {*
* Serial.print(“Baby Not Present”);*
* } else if (TempDiff >= 4) {*
* Serial.print(“Baby Present”);*
* Serial.println();*
* if ((digitalRead(ignition_input) == HIGH) && (digitalRead(weight_input) == LOW) && (digitalread() // Ignition/weight*
* {*
* Serial.println(“Initialize Sensing”); // Ignition/weight*
* }*
* else // Ignition/weight*
* {*
* Serial.println(“Do Not Sense”); // Ignition/weight*
* delay(4000); // Ignition/weight*

* }*
* delay(4000);*
}
*Now we are attempting to add our SMS Text Portion: *
If: Ignition is off
and: Adult Not Present
and: Baby Present
then send SMS.
Here is our code for the SMS, that we had working independently on another computer, but now for some reason, we can’t get it working again. This code is for the SMS alone. We can’t get it to work now, and we are trying to get it to work before we add it to the other code.
#include <Ciao.h>
/*

This example show the interaction between the Ciao Library and the Thingspeak Cloud.
To run the example you need to register an account on thingspeak.com and create a
new channel by clicking “Channels” section in the website (Channels → My Channels → New Channel).
In the new channel you need to add two fields. The first one refers to the humidity data and the second one to the temperature value.
After that, replace the “XXXXXXXXX” value of APIKEY_THINGSPEAK with “Write API key” value reported in the API Keys section of the channel.

*/
#include <Wire.h>
#include <Ciao.h>// if you are using the Arduino IDE 1.8.x then: //
#include <UnoWiFiDevEd.h>

#define CONNECTOR “rest”
#define SERVER_ADDR “api.thingspeak.com

#define APIKEY_THINGSPEAK “H1Z9583M8FUCQBWO” //Insert your API Key
void setup() {
* Serial.begin(9600);*
* Ciao.begin(); // CIAO INIT*
* String uri = “/apps/thinghttp/send_request?api_key=”;
uri += APIKEY_THINGSPEAK;*

* delay(20000); // Thinkspeak policy*
* Serial.println( "about to send: " + uri + to SERVER_ADDR);
_ Ciao.println(“Send data on ThingSpeak Channel”);*_

* CiaoData data = Ciao.write(CONNECTOR, SERVER_ADDR, uri);*

* if (data.isEmpty()){*
* Serial.println(“Got no data”);*
* } else {*
* Serial.println(“Got data”);*
* }*
* Serial.println( “Sent” );*
}

void loop() {

}
* And here are the error codes we are getting: *
In file included from C:\Users\Manager\Desktop\SMS_Code_Test\SMS_Code_Test.ino:1:0:
C:\Users\Manager\Documents\Arduino\libraries\arduino-library-ciao-master\src/Ciao.h:42:2: error: #error CPU not yet supported
#error CPU not yet supported
* ^*
exit status 1
Error compiling for board Arduino Uno WiFi.

*We googled it, and added Caio Library and Caio Core but we are finding some information on how to the “rest” to work and can’t seem to understand what to do. *
Can you help?

Does the fact that you can't use that library on whatever device you are trying to compile for not seem obvious from the message?

Take a look at the last code you posted. Do you see how halfway through there is some code missing and the text is in italics. The reason is the forum software interpreted part of your code as forum markup. This is why we have the rule here that all code, warning, and error messages must be posted using code tags.

Please use code tags (</> button on the toolbar) when you post code or warning/error messages. The reason is that the forum software can interpret parts of your code as markup, leading to confusion, wasted time, and a reduced chance for you to get help with your problem. This will also make it easier to read your code and to copy it to the IDE or editor. Using code tags and other important information is explained in the How to use this forum post. Please read it.

As suggested, please add code tags to your code.

A quick glance would seem to indicate that you have not used the presence of ababy as the determining factor as to whether to do anything else. No baby == no problem.

I also spotted this

   if ((digitalRead(ignition_input) == HIGH) && (digitalRead(weight_input) == LOW) && (digitalread() // Ignition/weight

which certainly will not compile.