HOME      

Simple Arduino code for watching the state of some inputs

(filename: PCB274-test-quad-opto.htm)

The code here was written as a testing tool for something I was working on. It has very broad general application... it displays the state of some inputs to the Arduino it is running on. (I wanted to watch the state of four outputs from a quad opto-isolator breakout board I was making at the time.)

The code sends a string of four characters to the Arduino IDE's serial monitor, several times a second. The characters tell you the state of the inputs.

The following has been snipped by hand, and not re-tested. It shouild be okay... but please let me know if you discover flaws...

/*ar7923
Much of this program's documentation is in the Serial.println stmts in setup()

===============================
Constants, etc

chTripped and chTrippedNot are just to make it easy to do a "pretty"
display on the serial monitor of the history of the states of the inputs
to the Arduino.

chTripped is the character to display if the channel is tripped, if the
    PIR on that channel is currently "seeing" moving heat.
chTrippedNot  is the character to display if the channel is NOT tripped.
*/

const char chTripped='|';
const char chTrippedNot='-';

//Note: Contray to my usual practice, a widely adopted practice, the
//first channel is called "Ch1", not "Ch0".. in this program.
byte bInCh1=2;
byte bInCh2=3;
byte bInCh3=4;
byte bInCh4=5;

boolean boInCh1Tripped;
boolean boInCh2Tripped;
boolean boInCh3Tripped;
boolean boInCh4Tripped;


//===========================================================
void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
Serial.println("ar793- ver 7 Jan 20");

... snip, snip, snip...

Serial.println("This program ");
Serial.println("checks the state of four inputs to the Arduino. It");
Serial.println("then sends a stream of characters to the serial monitor to");
Serial.println("indicate what it sees. Note that a small bit of cleverness");
Serial.println("is already present, though... the display can be inverted");
Serial.println("if that's what is wanted. An input that is high may result");
Serial.println("in a ((Tripped)) character or a ((TrippedNot)) character...");
Serial.println("OR the display may work the other way around!");
Serial.println("Serial monitor unknown to you? See...");
Serial.println("http://sheepdogguides.com/arduino/aht1serimoni.htm");
Serial.println("(The serial monitor is a very useful built in tool of the IDE.)");
Serial.println("-");
Serial.println("The names arise out of the eventual purpose of this program-");
Serial.println("It will be 'watching' some PIRs, and then sending signals to");
Serial.println("CCTV recorder reflecting what the PIRs are 'saying'. Depending");
Serial.println("on external factors, a given PIR may say it is seeing moving");
Serial.println("heat (an intruder?) by making the signal arriving at ar793 (this program!),");
Serial.println("** OR IT MAY ** make the input to ar793 when it 'sees' an intruder.");
Serial.println("-");
Serial.println("The bit of 'cleverness' in this means that you can accomodate either... or even");
Serial.println("a mix of the two, and the display on the serial monitor will 'hide' the details");
Serial.println("of how the PIRs are wired.");
Serial.println("");

Serial.println("-");
Serial.println("---------------------------------");
Serial.println("Connections to the Arduino...");
Serial.println("D2-5: Inputs (from the PIRs)");
Serial.println("");
Serial.println("");
Serial.println("");

pinMode(bInCh1,INPUT_PULLUP);
pinMode(bInCh2,INPUT_PULLUP);
pinMode(bInCh3,INPUT_PULLUP);
pinMode(bInCh4,INPUT_PULLUP);
}

void loop() {

//Yes... I know this has been done VERY crudely!

//Fetch and note the states of the inputs...

if (digitalRead(bInCh1)==HIGH) {boInCh1Tripped=true;} else {boInCh1Tripped=false;};
//    Alternative version, for when PIR wired the other way...
//      if (digitalRead(bInCh1)==LOW) {boInCh1Tripped=true;} else {boInCh1Tripped=false;};
if (digitalRead(bInCh2)==HIGH) {boInCh2Tripped=true;} else {boInCh2Tripped=false;};
if (digitalRead(bInCh3)==HIGH) {boInCh3Tripped=true;} else {boInCh3Tripped=false;};
if (digitalRead(bInCh4)==HIGH) {boInCh4Tripped=true;} else {boInCh4Tripped=false;};

//Report for each channel whether the PIR is currently tripped...
Serial.print(' ');//begin a line (of chTripped and chTrippedNot characters) with
//a space, just to "push" the information being presented off of the left margin
//of the serial monitor.


if (boInCh1Tripped) {Serial.print(chTripped);} else {Serial.print(chTrippedNot);};
Serial.print(' ');

if (boInCh2Tripped)  {Serial.print(chTripped);} else {Serial.print(chTrippedNot);};
Serial.print(' ');

if (boInCh3Tripped)  {Serial.print(chTripped);} else {Serial.print(chTrippedNot);};
Serial.print(' ');

if (boInCh4Tripped)  {Serial.println(chTripped);} else {Serial.println(chTrippedNot);};

//... and wait a moment before doing it all again...
delay(150);
}

I hope you will find that useful.

If you found this of interest, please mention in fora, give it a Facebook "like", or whatever. I've almost given up writing these pages, because it seems they are seldom read, and of course not every reader will use them... so... is there any point? If you want more of this stuff, help!?



To search THIS site.... (Go to my other sites, below, and use their search buttons if you want to search them.)

index sitemap advanced
search engine by freefind

Click here to visit my main homepage where you can explore other areas, such as education, programming, investing.




Ad from page's editor: Yes.. I do enjoy compiling these things for you. I hope they are helpful. However... this doesn't pay my bills!!! Sheepdog Software (tm) is supposed to help do that, so if you found this stuff useful, (and you run a Windows or MS-DOS PC) please visit my freeware and shareware page, download something, and circulate it for me? Links on your page to this page would also be appreciated!
Click here to visit editor's freeware, shareware page.

To email this page's editor, Tom Boyd.... Editor's email address. Suggestions welcomed! Please cite "PCB274-test-quad-opto.htm".



Valid HTML 4.01 Transitional Page has been tested for compliance with INDUSTRY (not MS-only) standards, using the free, publicly accessible validator at validator.w3.org. Mostly passes, unless there are some instances of pre-HTML5 element alignment lingering.

AND passes... Valid CSS!


Why does this page cause a script to run? Because of the Google panels, and the code for the search button. Also, I have my web-traffic monitored for me by eXTReMe tracker. They offer a free tracker. If you want to try one, check out their site. Why do I mention the script? Be sure you know all you need to about spyware.

....... P a g e . . . E n d s .....