In previous article we have seen the steps in creating the software for the reading Potentiometer value using Simulink for Arduino board. In this article we will look at another way of generating the software and loading to the Arduino board. We will use Arduino IDE and C programming language to achieve the same functionality as the previous article.
If you already doesn't have the Arduino IDE head to the following link and download the IDE.
https://www.arduino.cc/en/software
The following are the functions used in our C Program
- pinMode
- digitalWrite
- delay
- analogRead
- map
Functions used in the Potentiometer value reading software for Arduino
For the function help related to pinMode, digitalWrite, delay pleasse visit this article. In this article we are using two new functions and they are analogRead and map functions. Let see the how analogRead and map functions work in Arduino.
analogRead Reads the value from the specified analog pin. This function takes the name of the analog input pin to read from as function argument/parameter. The function call returns the analog value of the reading in the range from 0-1023, this is due to that the analog to digital convertors on the Arduino are the 10 bit ADC's.
map function takes the value and then remaps this value from one range to another range. The function call for map looks like map(analog_value, analog_LSB, analog_MSB, newLow, newHigh), as you can see this function takes the value and then maps the value from one range to another range. It is always useful to know the signal maximum and minimum values beforehand. This function returns the value in the new range based in input value.
Lets dive into the code for reading the potentiometer value and showing the mapped value using the LED's.
Step 1: Set the pin mode in setup function
#define Bit0_LED 3 #define Bit1_LED 4 #define Bit2_LED 5 int pot_value = 0; int mapped_value = 0; void setup() { // put your setup code here, to run once: // Set the pin mode for arduino digital pins. pinMode(Bit0_LED, OUTPUT); pinMode(Bit1_LED, OUTPUT); pinMode(Bit2_LED, OUTPUT); // Set the initial state for the digital pins. digitalWrite(Bit0_LED, LOW); digitalWrite(Bit1_LED, LOW); digitalWrite(Bit2_LED, LOW); }
Step 2: Set up the logic in loop function
void loop() { // put your main code here, to run repeatedly: pot_value = analogRead(A2); mapped_value = map(pot_value, 0, 1023, 1, 4); digitalWrite(Bit0_LED, mapped_value&0x1); digitalWrite(Bit1_LED, ((mapped_value&0x2)>>1)); digitalWrite(Bit2_LED, ((mapped_value&0x4)>>2)); delay(10); }
As you can see in the loop function analogRead function is called by passing the A2 as the parameter, potentiometer is connected to the pin A2 of the arduino. Once the analogRead function is called the value is mapped to lower range i.e 1 to 4 and then the mapped value is used to turn on and off the LED's.
In the loop function to turn on and off the LEDs if else logic is not used but just the hexadecimal bit manipulation is used. You may wounder how this is working. Let consider the mapped value is 1 so for that we need to light only the Bit0_LED and rest of the LED's need to be turned off.
Following is the math behind the hexadecimal trick
mapped_value = 1, multiply this with the hex 1 so we will get 1 as the output and we are passing this to the digitalWrite function with Bit0_LED as the first parameter, so this LED will be turned on. Multiplying mapped_value = 1 with wither 0x2 or 0x4 returns 0 so the other LED's will be turned off the same logic goes for any other number.
Complete program for reading the potentiometer value
#define Bit0_LED 3 #define Bit1_LED 4 #define Bit2_LED 5 int pot_value = 0; int mapped_value = 0; void setup() { // put your setup code here, to run once: // Set the pin mode for arduino digital pins. pinMode(Bit0_LED, OUTPUT); pinMode(Bit1_LED, OUTPUT); pinMode(Bit2_LED, OUTPUT); // Set the initial state for the digital pins. digitalWrite(Bit0_LED, LOW); digitalWrite(Bit1_LED, LOW); digitalWrite(Bit2_LED, LOW); } void loop() { // put your main code here, to run repeatedly: pot_value = analogRead(A2); mapped_value = map(pot_value, 0, 1023, 1, 4); digitalWrite(Bit0_LED, mapped_value&0x1); digitalWrite(Bit1_LED, ((mapped_value&0x2)>>1)); digitalWrite(Bit2_LED, ((mapped_value&0x4)>>2)); delay(10); }
No comments:
Post a Comment