| 
  • If you are citizen of an European Union member nation, you may not use this service unless you are at least 16 years old.

  • Stop wasting time looking for files and revisions. Connect your Gmail, DriveDropbox, and Slack accounts and in less than 2 minutes, Dokkio will automatically organize all your file attachments. Learn more and claim your free account.

View
 

BlinkyPot

Page history last edited by WikiAdmin 5 years, 2 months ago

If you plug a potentiometer into one of the two right hand input (upper pair) daughter board sockets, you can begin to learn about analog signals, reading an analog input.

 

We will use a simple pot for this challenge, but be aware: There are many, many sensors which act a bit like our pot. Except the voltage they produce changes not because the pot's knob has been turned but because something....

 

  • light
  • temperature
  • a voltage
  • the distance between a sensor and some object
  • .... etc

 

 ... has changed.

 

A Binky program which lets you control blink rate with a pot

 

The following assumes that you have the pot plugged into the lower right hand input socket. (The second one down from the top of the board. The two sockets below that are for output daughter boards.)

 

You could use the upper one, if you changed the one reference in this to inPLR to inPUR. The fact that we can make a major change... a re-wireing... to the hardware, but only need one small change in the software is a hallmark of a well done project.

 

You cannot use either of the right hand input sockets. More on that later.

 


#include <NovGrdCore.h>

NovGrdCore NGC;

void setup()
{
//yes, you have to have this... even though it is empty
}

void loop()
{
int iAnVal;

iAnVal=analogRead(inPLR);

if (iAnVal<60)
      {iAnVal=60;};
if (iAnVal>1000)
      {iAnVal=1000;};

NGC.makeOuUR(1);
delay(iAnVal);
NGC.makeOuUR(0);
delay(iAnVal);
}

There are two things in that which may be new to you....

 

1) analogRead, and...

 

2) The variable, which gives rise to....  

 

      int iAnVal;
      iAnVal=analogRead(inPLR);
 

 The first (int iAnVal;) creates the variable. Once you have done it, there is a "place" where you can"put things"... numbers from 0 to 1023, in this case... because of the int part.

 

The next uses the variable we created. When we tell the computer to do analogRead(inPLR), it goes off and reads the voltage coming in on inPLR, and it turns that voltage into a number from 0 to 1023. And puts the number in iAnVal, our variable.

 

The rest of BlinkyPot should be pretty easy to follow... it isn't much different from our first Blinky.

 

But!

 

Inside both delay s, instead of a number, which is what we had last time, we have iAnVal again. Now we are taking something out of the "place we can put things". If during analogRead(inPLR), the voltage present made, say, 345 go into iAnVal, then, until we do something to change what is in iAnVal, and delay(iAnVal)"boils down" to delay(345)... at that time.

 

There is one other little thing in the program, isn't there? (The two "if" statements.)

 

They "fix things" so that the number in iAnVal will be about 59 and below 1001 by the time it is used by the "delay" commands.

 


There's always more you can do....

 

Part of the fun of programming, and working with electronics, is that you are never done. You might, for instance, fit two pots to your NoviceGuard and use one to control the "on" time, and the other to control the "off" time.

 

You might, with just one pot, turn the first LED on when it is turned way "down", and then two when it is turned up a bit, three when it is turned up more, and all four on when the pot is turned into the last quarter of its travel.

 


Important Thing To Learn- Analog and Digital

 

Despite what you will hear people "in the street" saying, "digital" doesn't just mean "with digits".

 

When you were just getting started with the Arduino, we didn't make a fuss about it, but it actually has two sorts of inputs... digital inputs (they're on the left) and analog inputs (on the right).

 

(Just before we go on... you will be pleased to know that in the NoviceGuard, we only have digital outputs.)

 

The digital inputs can sense only two possiblities. We usually call them "high" and "low". Sometimes, as in NovGrdCore, "1" and "0" are used for the two possibilities. We could call them "red" and "green", if we liked. But, for anything digital, there are only two states. It is like a simple light bulb: It is either on or off. As the Arduino is an electical device, whether the input counts as "high" or "low" comes from what the voltage is there when the Arduino "looks". If the voltage is above a certain value, the input is considered "high". Below a certain voltage, the input is called "low". This is great! It means that the electronics don't need to work perfectly! One day, 0.1v might be on the input, another day, it might be 0.3v... but either will "do"!

 

The analog inputs, on the other hand, can sense "off", "low", "medium", "quite high", "very high"... and lots of things in between! It is like the volume control on a sound system.

 

When a novice first "plays" with NoviceGuard, a little "cheat" is in place. He or she can read the right hand inputs as if they were digital inputs.

 

The NovGrdCore function boInHigh(x) returns "true or "false"... whatever the answer would be to "Is that input high?) (Hence the name! The "bo" part is short for "Boolean", which is an adjective for things that can only be true or false.) If we have 1 or 3 in for the "x", we are reading the right hand inputs, which are actually analog inputs, returning 0 to 1023. But, inside NovGrdCore, we have told the software to "convert" that number to a "true" or "false", depending upon whether the reading gives a high number, or a low one.

 

(When we have a 0 or a 2 in place of the "x", then we are looking at the left hand inputs, and the function is actually reading a digital input, as the way it works would lead us to believe, and nothing "clever" is going on.)

Comments (0)

You don't have permission to comment on this page.