Click this for NoviceGuard main page > Click this for daughter board pages table of contents
This page will introduce you to a daughter board for NoviceGuard. You do not need a NoviceGuard to use what is presented here. It will be of little use if you do not have an Arduino.
The two inputs on the right hand side of the NoviceGuard can be "fed" with a voltage from 0 to Vcc.
There are many sensors which present the microprocessor with voltages in that range. But a properly wired potentiometer makes a good starting point when exploring analog inputs.
This is a daughther board for the NG_PwrDemand_1 power scenario... or maybe NG_PwrDemand_2, if you are connecting certain alternative sources of an analog input.
To "properly wire" a potentiometer to a set of pins to plug into a daughter board socket is easy enough: Wire one "end" of the big resistor in the pot to Vcc; wire the other to ground. Wire the pot's wiper to the Arduino's analog input, the middle pin of the daughter board socket. If you're not sure about that, be careful... three pins on the potentiometer, three pins on the daughter board connector: the two end pins, and the one in the middle... so far, so good... but wrong pin to wrong pin could be bad. See sidebar if in doubt, or contact me.
Sidebar, details of hardware hookup:
A 10k pot would be about right. Higher will work, but go too high, and the signal will be "noisy" with induced voltages. If you are using the NoviceGuard in NG_PwrDemand_1... which you can... I go below 1k. At that point, with Vcc=5v, the current you draw will be 5mA. Even under a higher NG_PwrDemand scenario, you are not only drawing unnecessary power from your supply, you are also getting on towards needing a pot capable of dissipating significant heat.)
In doing the power calculations, you can ignore any current flow in or out of the analog input of the Arduino. It will be negligible whenever you connect anything (between 0 and Vcc) to an analog input.
You might want to take care with which end of the pot you wire to Vcc. Get it the "right" way around, and turning the pot clockwise will increase the voltage presented to the Arduino, which will seem "natural" to users, I think. Certainly, if you are wiring up several, at least do them all the same way!
To achieve the above: Turn pot half way between fully clockwise and fully the other direction. Use ohm-meter to find the two pins which show the pot's full resistance. These are the "end" pins, the other is the wiper's. It might be an idea to solder the wiper's wire on now, so you don't get confused as to which pin is the wiper's.
Now turn the pot clockwise as far as it will go. And find the pair of pins with near zero resistance between them. One of them will be the wiper's pin. Connect the other to Vcc. (And, of course, the remaining pin to ground.)
You don't have to use a NoviceGuard to benefit from this page. The NoviceGuard just makes it easier for novices to hook things up. We will be using one analog input and LEDs on four outputs, although two, or even one, would suffice for an idea of what the program does. I've done a separate page on doing NoviceGuard challenges/ programs without a NoviceGuard. (You just have to provide.. not hard... your users with the things NoviceGuard would give them.)
A library is provided for NoviceGuard users: "NovGrdCore".
You don't have to use the library to benefit from this page. There's more on Do I need the NovGrdCore Library in the page I wrote about that. But using the library is just so convenient, NoviceGuard board or no NoviceGuard board!
(It's a pretty minimal "board" in this case! But the pot plugs in via a daughter board socket. And you might be using a more exotic source of a varying voltage. Keep it between 0 volts and Vcc, remember!)
Assuming you are using a NoviceGuard board and the NovGrdCore library, the following will respond to changes in the position of the wiper, i.e. respond to you changing the position of the knob on the pot, i.e. changes in the voltage presented to the Arduino.
It will "respond" by changing how many of the LEDs are on... you should be able to have none, 1, 2, 3 or all 4 on. Wow! But it is a first step to more exciting things, I assure you. (We won't go (far) into them here, but it is still true!)
/*PotentiometerSimpleA Using a potentiometer with a NoviceGuard... A very, VERY simple demonstration. Requires a potentiometer plugged into inPLR's daughter board socket. (Feeds the Arduino analog input A2, for those not using NoviceGuard) ... and... LEDs on all four of the usual NoviceGuard outputs. Note that a potentiometer daughter board can only be used in the input daughter board sockets on the right hand side of the board. (See... http://rugguino.com/NGBrdNotNeeded.htm ... if doing without NoviceGuard. And the NovGrdCore library. (See... ... to do without that.) http://rugguino.com/NGCoreDoINeed.htm */ #include <NovGrdCore.h> NovGrdCore NGC; void setup() { //Must have this, even if it is empty. Serial.begin(9600);//OPTIONAL... to enable serial monitor } void loop() { //Yes! This is done crudely. Perhaps it would be better to // read the input once, put the result in a variable, // do the "if..."s with the variable's contents. //I thought the following more clear for novices. if (analogRead(inPLR)>100) {NGC.makeOu(0,1);} else {NGC.makeOu(0,0);}; if (analogRead(inPLR)>300) {NGC.makeOu(1,1);} else {NGC.makeOu(1,0);}; if (analogRead(inPLR)>600) {NGC.makeOu(2,1);} else {NGC.makeOu(2,0);}; if (analogRead(inPLR)>900) {NGC.makeOu(3,1);} else {NGC.makeOu(3,0);}; Serial.println(analogRead(inPLR));//Optional... sends value to // serial monitor. }
Okay... that's a pretty boring little program. But it has actually been tested in a NoviceGuard. It should be correct, down to the last semi-colon.
There are things which could be "better" in that code... even without changing what the user sees at all.
Consider...
/*PotentiometerSimpleB vers 26 May 15 started 26 May 15 See notes in PotentiometerSimpleA */ #include <NovGrdCore.h> NovGrdCore NGC; void setup() { //Must have this, even if it is empty. Serial.begin(9600);//OPTIONAL... to enable serial monitor Serial.print("NovGrdCore library version: "); NGC.reportVers(); delay(1000); } void loop() { int iAnVal; iAnVal=analogRead(inPLR); if (iAnVal>100) {NGC.makeOu(0,1);} else {NGC.makeOu(0,0);}; if (iAnVal>300) {NGC.makeOu(1,1);} else {NGC.makeOu(1,0);}; if (iAnVal>600) {NGC.makeOu(2,1);} else {NGC.makeOu(2,0);}; if (iAnVal>900) {NGC.makeOu(3,1);} else {NGC.makeOu(3,0);}; Serial.println(iAnVal);//Optional... sends value to // serial monitor. }
Isn't that easier to read, follow, understand? (If you can't read your code, you can't find bugs.) Okay, you need to understand the idea of a variable, and using one. But the idea of NoviceGuard is to learn about the things you can do in the Arduino world.
Actually, as I'm sure you noticed, I lied. This program gives the user something which the first one should have done.
When the program is re-started when you invoked the serial monitor, the version of the NovGrdCore library is reported in the serial monitor window. You should probably put that code into all your NoviceGuard/ NovGrdCore work. It is one of those Good Habits successful programmers have. Notice that the code itself now has a version "ID" of sorts. (The "started" information is just something I find fun to have... you might too.)
At the moment, as you change the voltage on the analog input more and more LEDs come on or go off.
What if, just saying, you wanted always only just one on, but a different one, depending on the voltage?
(I promise: You will want to be able to do something like that in due course, even if, for this situation, the 1, 1 and 2, 1 and 2 and 3... is probably just fine, and, anyway: prettier!
Well, you can do something that would be like this in pseudo code....
.... if (voltage<100) then turn LED "0" on. if (voltage>99)AND(voltage<300) turn LED "1" on. if (voltage>299)AND(voltage<600 turn LED "2" on. if (voltage>599) turn LED "3" on.
Messy! And it gets worse, when you go to put it into Arduino-speak, that way...
/*PotentiometerSimpleC vers 26 May 15 started 26 May 15 See notes in PotentiometerSimpleA */ #include <NovGrdCore.h> NovGrdCore NGC; void setup() { //Must have this, even if it is empty. Serial.begin(9600);//OPTIONAL... to enable serial monitor Serial.print("NovGrdCore library version: "); NGC.reportVers(); delay(1000); } void loop() { int iAnVal; iAnVal=analogRead(inPLR); if (iAnVal<100) {NGC.makeOu(0,1);} else {NGC.makeOu(0,0);}; if ((iAnVal>99)&&(iAnVal<300)) {NGC.makeOu(1,1);} else {NGC.makeOu(1,0);}; if ((iAnVal>299)&&(iAnVal<600)) {NGC.makeOu(2,1);} else {NGC.makeOu(2,0);}; if (iAnVal>599) {NGC.makeOu(3,1);} else {NGC.makeOu(3,0);}; Serial.println(iAnVal);//Optional... sends value to // serial monitor. }
Buried in the above: We use "&&" for "and"... at least the sort of "and" needed in this context.
That program "works" (actually tested!)... but, trust me, you would probably tear your hair to get everything "just right". (Don't believe me? Write it yourself, from scratch.)
There's a nice control structure build into the Arduino language... "switch... case... case... end"... (Digression: "Nice sausage roll on the landing"? LOTS of "points" to anyone who can tell me where that comes from!)
You don't need to learn about the Switch structure today! But if you do, you will have another "tool" in your "toolbox", won't you?
The nice thing about the Switch structure is that if a case that applies is found, the program doesn't even try the other cases, if you remember the "break" at the end of each.
Yes... the program is bigger... but we have split things up. We only have to think about one simple thing at a time. My simple brain copes better in such an easy environment... and makes fewer mistakes, and I Get The Job Done sooner.
/*PotentiometerSimpleD vers 26 May 15 started 26 May 15 See notes in PotentiometerSimpleA */ #include <NovGrdCore.h> NovGrdCore NGC; void setup() { //Must have this, even if it is empty. Serial.begin(9600);//OPTIONAL... to enable serial monitor Serial.print("NovGrdCore library version: "); NGC.reportVers(); delay(1000); } void loop() { int iAnVal; byte bLevel; iAnVal=analogRead(inPLR); if (iAnVal<100) bLevel=0; if (iAnVal>99) bLevel=1; if (iAnVal>299) bLevel=2; if (iAnVal>599) bLevel=3; //Turn all the LEDs off... NGC.makeOu(0,0); NGC.makeOu(1,0); NGC.makeOu(2,0); NGC.makeOu(3,0); switch (bLevel){ case 0:{NGC.makeOu(0,1); break;}; case 1:{NGC.makeOu(1,1); break;}; case 2:{NGC.makeOu(2,1); break;}; case 3:{NGC.makeOu(3,1); break;}; default: {//Shouldn't arise!... but provide for it, // just to be safe! NGC.makeOu(0,1); NGC.makeOu(1,1); NGC.makeOu(2,1); NGC.makeOu(3,1);} }//end of "Switch" Serial.println(iAnVal); }
If you are wondering if the Break commands are actually needed, no, you're right, in this program, they aren't. But get in the habit of putting them in.
By a happy coincidence of how the NovGrdCore routines work, the above could be made even simpler... perhaps too simple... you can be too clever. But, in case it amuses, consider the following. It works! Really!...
/*PotentiometerSimpleD vers 26 May 15 started 26 May 15 See notes in PotentiometerSimpleA */ #include <NovGrdCore.h> NovGrdCore NGC; void setup() { //Must have this, even if it is empty. Serial.begin(9600);//OPTIONAL... to enable serial monitor Serial.print("NovGrdCore library version: "); NGC.reportVers(); delay(1000); } void loop() { int iAnVal; byte bLevel; iAnVal=analogRead(inPLR); if (iAnVal<100) bLevel=0; if (iAnVal>99) bLevel=1; if (iAnVal>299) bLevel=2; if (iAnVal>599) bLevel=3; //Turn all the LEDs off... NGC.makeOu(0,0); NGC.makeOu(1,0); NGC.makeOu(2,0); NGC.makeOu(3,0); NGC.makeOu(bLevel,1); Serial.println(iAnVal); }
Remember "Blinky"? The program that makes one of the LEDs go on/ off/ on/ off....
Change it. Write a program that makes the LED go off for 800ms, with bits of "on" in between the "offs". But make the length of the "on" change as you change the position of the potentiometer's knob.
And remember that there are many, many things which connect just like the potentiometer, but which give a voltage depending on a light level, a temperature, a sound volume, etc, etc.
With four LEDs, we could distinguish five ranges of voltage, if we were willing to use "no LED on" as one of them. (I don't like the "no LED on" state being used for one of them, because how do you tell it from "broken", or "off"?)
If you know binary numbers, you could write a program to distinguish seven ranges of voltage, or eight, if you allow "no LED on" to be one of them.
You could write it badly... i.e. "it works"... but is a dog's dinner when you try to read the code.
You could write it well... i.e. it works AND the code is easy to read.
Enjoy!
That's it for this page. That's a long way short of things you can do with the analog inputs.
I hope the page fanned your interest in, understanding of, NoviceGuard and it's free, optional library, NovGrdCore.
Page has been tested for compliance with INDUSTRY (not MS-only) standards, using the free, publicly accessible validator at validator.w3.org. Mostly passes, just a few "No attribute" issues, arising from Google code.
....... P a g e . . . E n d s .....