Ultrasonic Toggle Switch Source Code (Arduino)


// ultrasonic toggle switch
// Joe Schembrie aka 'Engineer Zero'
// comments 6-23-2016

// the circuit for this sketch consists of a yellow and green LED and an ultrasonic distance sensor (pinger).
// when a hand approaches the pinger, the yellow light comes on with the approach.
// when the hand gets close enough, the value of green is toggled.  
// IE, if the green LED was off, it turns on.  If it was on, it turns off.

// to see this in action, visit https://engineerzero.wordpress.com/2014/06/01/ultrasonic-toggle-switch/

// The green LED toggles only once while the hand is near. 
// IMPORTANT
// The hand must be retracted far enough so that the yellow light goes off
// for the green LED to be toggled again.  This prevents 'bouncing.'  

// NOTE:  In some respects, this device seems to work best when the yellow light is disconnected.
// NOTE ON NOTE:  I wrote the above note a couple years ago AND DON'T REMEMBER WHY.

// Now for inclusion of the library.  
// the NewPing.h library is available on the Internet.
// I did not write it.  I cannot answer questions about it.
// It comes with documentation on the Internet.  
// Google away!

#include <NewPing.h> 

#define TRIGGER_PIN  11  // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN     10  // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);

// now to stuff that I wrote.
// here are some very important variables

// dis is distance of hand to sensor.  greenstate is state of green light.
// toggle is a variable that gives approval to toggle.
// Note that 'toggle' does not by itself trigger a toggle.  
// It only gives 'approval to toggle.'  

unsigned int dis;
int greenstate = 0;
int toggle = 10;


void setup() {
  // put your setup code here, to run once:

pinMode(2,OUTPUT);  // yellow is 2
pinMode(3,OUTPUT);  // green is 3
pinMode(11,OUTPUT); // pins 10 and 11 are used by NewPing
pinMode(10,INPUT);    

digitalWrite(2,LOW);
digitalWrite(3,LOW);

}

void loop() {
  // put your main code here, to run repeatedly:
  
// NewPing stuff . . . 

delay(50);

unsigned int uS;
uS = sonar.ping();

dis = (uS/US_ROUNDTRIP_CM);

// . . . end of NewPing stuff.  Now onto my stuff:

// the following code sets the zone value according to distance.
//
// after toggling, toggle approval is de-activated but 
// is re-activated if the hand is retracted to zone 0.
// this prevents nuisance toggling (bouncing).

int zone;

// zone = 0 means the hand is far enough away that the sensor doesn't react to it.
// toggle approval is granted.  
// I set the distance at 40 cm.  You can experiment with distances as you please.

if(dis>=40){
  zone = 0;
  toggle=10;
}

// zone = 1 means the hand is close enough to turn on yellow light.

if(dis<40){
  zone = 1;
}

// zone = 2 means the hand is close enough to toggle the green light.
// the 10 cm distance is open to your choice.  

if (dis<10){
  zone = 2;
}

// Now, I vaguely remember that the reason for the following test had something
// to do with the quirkiness of the sensor.  Sometimes it fails to read and
// reports a zero distance.  The following test prevents that from causing problems.

if(dis<2){
  zone = 0;
  toggle = 10;
}


// now we take care of yellow light.  
// It's pretty simple.  If you're within a certain distance, it's on.  Otherwise, off.
// 

if(zone==0){
  digitalWrite(2,LOW);
}

if(zone>0){
  digitalWrite(2,HIGH);
}


// we toggle the variable greenstate (and, most importantly, the green light)
// if we're in zone 2 and toggle is 10.
// 
// why these values?  It will become apparent in later context.  
//
// zone 2 means we're close enough to toggle.
//
// toggle = 10 means go ahead and toggle.
// toggle = 0 means don't toggle.
// 
// greenstate = 0 means the green light is OFF.
// greenstate = 100 means the green light is ON.
//
// 'toggle' is a variable that 'approves' toggling the green light
// only ONCE when you're close to the sensor.  It's then set to 0
// and your hand has to move away in order for it to reset to 10.
// this prevents bouncing.


int k = 0;

// now, you're probably also wondering about this 'k' business.
// the k variable is a handy way to avoid making a complex IF statement.
// Maybe it's me, but sometimes I just can't get multi-condition IF statements
// to work on the Arduino.
//
// So instead, I use individual IF statements to create flag variables
// and then add the flag variables together to create a 'master flag variable'
// which I can then test in a simple one-variable conditional statement.
//
// So k is for 'kludge.'  But it works . . . .

k = toggle + zone + greenstate;

// k = 112 means that the toggle (=10) is approved, we're in toggle zone (=2) , 
// and the greenstate (=100) indicates the green light is ON.

if(k==112){
  greenstate = 0;
  digitalWrite(3,LOW);
  toggle = 0;
}

// k = 12 means that the toggle (=10) is approved, we're in toggle zone (=2) , 
// and the greenstate (=0) indicates the green light is OFF.

if(k==12){
  greenstate = 100;
  digitalWrite(3,HIGH);
  toggle = 0;
}

// these two statements then toggle the green light 
// (and circuits activated by the same pin).
//
// note that for all other values of k, no action is taken.  

// And that's all there is to it!!!

}






 
  


Posted in Uncategorized | 1 Comment

Orbital Hydrogen Collector

Orbital Hydrogen Collector (OHC) with docking spacecraft

Orbital Hydrogen Collector (OHC) with docking spacecraft

Deep space missions will require the placement of large quantities of hydrogen in low earth orbit for use as spacecraft propellant. Procuring hydrogen in low earth orbit from the surface of the earth via chemical rocket booster can be relatively expensive.

The Orbital Hydrogen Collector (OHC) offers an alternative method of obtaining hydrogen propellant in low earth orbit. The OHC ‘scoops’ the hydrogen that exists in minute amounts in the near vacuum of the upper atmosphere at altitudes of low earth orbit. This hydrogen propellant can then be accessed by docking spacecraft to the OHC.

FIGURE 1 (not to scale) shows the basic design of an Orbital Hydrogen Collector. The direction of orbit is from right to left, as shown. The ‘flow’ of atmospheric hydrogen is thus from left to right.

ohc 2

Hydrogen molecules enter the scoop (a) and are compressed into higher-pressure gaseous and/or liquid form by the compressor unit (b). The hydrogen is then pumped into storage tanks (c). Spacecraft can access the stored hydrogen by docking with the propellant transfer boom (d).

The collection of atmospheric hydrogen causes the OHC to lose momentum and experience orbital decay. To compensate, the OHC can utilize electric propulsion to provide orbital boost. In the figure, photovoltaic panels (e) provide electric power to engine (f) while propellant is pumped from storage tanks (c) to engine (f).

The operational exhaust velocity of the engine must be higher than orbital velocity, or the engine will not be able to compensate for momentum lost to atmospheric drag. Plasma engines are the logical candidates for OHC propulsion.

At $5000/kg to ship payloads into orbit using expendable rocket boosters, it could cost tens to hundreds of billions of dollars to place into orbit the hydrogen propellant needed for just one human expedition to Mars. Informal calculations indicate that the OHC could collect the same amount of hydrogen in orbit for as little as one percent of that cost.

line break

wizard cover 12

The Wizard from Earth, a science fiction novel by S.J. Ryan (aka, Me), available on Amazon as an ebook

Posted in Uncategorized | Tagged , , | Leave a comment

Eye Robot, the Continuing Saga

0422051730

My original plan for making a robot with visual pattern recognition was to connect a camera to the raspberry pi, and then the raspberry pi to my laptop, and then I would run the raspberry pi ‘headless.’ That means, I would open up a remote terminal program on my laptop to access the raspberry pi’s desktop. The advantages of this are that I wouldn’t need a separate monitor or mouse or keyboard or wi-fi for the pi.

But this turned out to be one hassle after another. It turns out that you can’t configure a pi to be headless unless you first have it ‘headed.’ So you need all that stuff, in order to not need all that stuff. And then I couldn’t get the remote terminal program to work, and I would have to modify several files on the pi that I knew nothing about, let alone how to modify them in the first place . . . .

My second plan was to just do the pattern recognition programming on my laptop. It’s in python, and python is portable, right? So whatever I do on my laptop can be ported over to the pi, even though one is Windows and the other is Linux. Easy as. Nope, the webcam I got for the laptop has a mini-CD disk that clatters in the drive, and I couldn’t get SimpleCV to download at first, and then it wouldn’t work . . . and this got too complicated too.

So then I ended up getting all the peripherals for the pi after all. Today I connected the monitor, only to discover that the power supply that I ordered from Amazon is wrong. I bought the CanaKit power supply, thinking that since it was on the Tontec monitor page, that it was for the Tontec monitor. No, it’s for the raspberry pi, and because people who buy the Tontec monitor also buy the raspberry pi (of course), they also buy the CanaKit power supply for the raspberry pi. And that’s why it ends up on the Tontec monitor page even though it doesn’t connect to it. My fault.

This is what you see in the picture. I still can’t get the thing up and running because I need a power supply for the monitor. That will come next week. I suppose I’m in no hurry.

Anyhow, here are the links for the Tontec monitor and the correct power supply, in case you want to check them out:

Tontec 7″ LCD display screen for raspberry pi

Tontec power supply for the above

Until I get the power supply, I can’t vouch how this stuff works, but you can see the ratings on the Amazon pages.

BTW, I saw a demonstration of the Pixy(cam) Pet robot on Saturday, and it’s a good example of what can be done with robot vision. Here is a Youtube video of the Pixy Pet robot:

Posted in Uncategorized | Tagged , | Leave a comment

Eye, Robot (Part One)

raspberry pi

pi camera

I’m toying with the idea of a robot that can interact with its environment by pattern recognition. I thought the way to do this was with a raspberry pi computer. So I bought the computer and the pi camera from Maker Shed, and expected that all I would have to do is connect a USB cable to my laptop to begin programming the pi.

Well, not exactly. It turns out that what I need on both the pi and my laptop is a program called VNC, and VNC doesn’t come on the preloaded OS card that I got for the pi. I can download VNC for my laptop easily enough, but it appears that I’ll need a monitor and keyboard connected to the pi in order to download VNC onto it. There may be a way around that, but it looks like I’m about to make a trip to Goodwill for a cheap monitor and keyboard.

Posted in Uncategorized | Tagged , , , , | Leave a comment

Rocket Ship Freedom free for today (Dec 16)

kdp title 01

Rocket Ship Freedom

This is a YA novel I wrote eight years ago about a single stage to orbit rocket that travels to the Moon. It doesn’t come back, so how the crew gets home is something you’ll have to read the book to find out. If you’re a space buff, you’re probably wondering how an SSTO gets to the Moon in the first place. Again, it’s in the book. Which you can get today for FREE.

Posted in Uncategorized | Leave a comment

The Wizard from Earth

My science fiction novel, The Wizard from Earth, is available on Amazon Kindle as an eBook. It’s selling for only 99 cents. If you don’t have a Kindle, you can still download the free Kindle Reader software, and read it on your computer and/or smartphone.

Matt was a typical twenty-second century teenager with a supercomputer implanted in his head. He was supposed to be launched toward the star Alpha Centauri, but ended up on another world, lights years and centuries removed from his family.

Carrot was a native of that new world, a mutant of superior strength, senses, and intelligence, alienated from her own family and locked in a deadly struggle with a global empire.

Together they would battle humans, mutants, and super-intelligent AIs in a war for the fate of a new world — and for the survival of humankind.

View: The Wizard from Earth

Posted in Uncategorized | Leave a comment

Kind of a resume

I’m looking into tutoring again and thought it might be useful to list a few links that illustrate what I’ve done. I may not have done the right stuff, but I have done stuff.

Arduino Park:

Grisbot 2-obstacle course:

Grisbot Follows the Light:

Triac/Reed Switch test:

Ultrasonic distance sensor as toggle switch:

Ultrasonic pinger game:

LED Figurine Illumination

Grisbot with Scratch Obstacle:

ATTiny45 light display:

Grisbot light follow test:

Also, here’s my video work on Modeling Molecules.

Posted in Uncategorized | Leave a comment