This example will guide you through the process of connecting a potentiometer to your imp, and polling its value at a specified interval.
This example assumes you have a basic familiarity with Electric Imp’s impCentral™. We suggest working through our Getting Started Guide before trying this example.
Note The following instructions were written with the imp001 in mind, but they can be readily adapted to any other imp and breakout board.
For this example you are going to need the following:
In impCentral, create a new Product called “Examples” (if you have not tried another of these basic hardware examples) and a new Development Device Group called “potentiometer”. Assign your device to the new Device Group, then copy and paste the following code into the code editor’s ‘Device Code’ pane and hit ‘Build and Force Restart’.
The first thing we do is assign hardware.pin2 to a global variable called pot, this will make it easier to reference throughout the rest of our code:
pot <- hardware.pin2;
Next, we configure pot to be an analog input:
pot.configure(ANALOG_IN);
We can now use pin.read() to get the current voltage of the pin. The result of a pin.read() on an ANALOG_IN pin will be an integer value between 0 and 65,535 (2
In our example, we create a poll function, poll(), that will read and log the value of the pin ten times a second. It reads pin2 and logs the value:
server.log(pot.read());
and then schedules itself to run again in 0.1 seconds using imp.wakeup():
imp.wakeup(0.1, poll);
Finally, we start polling the pin by calling the poll() function to get the loop going:
poll();