A servo motor has a shaft that can be, using coded signals, positioned to specific angular positions. Luckily for us, we won’t have to understand the coded signals required to rotate the shaft to a specific angle. The Arduino Servo library does it for us.Circuit DiagramAs you can see, the Vcc of the Servo (typically red) is connected to 5V, GND (typically black) to GND, and the signal pin (white in the above image, typically white or yellow or orange) is connected to pin 9 of the Arduino.Code WalkthroughWe will be walking through an example code that comes in with ... Read More
When you delve into advanced firmware, you deal with a lot of registers, whose specific bits need to be set or cleared depending on your use case. Arduino has inbuild functions to do that.SyntaxbitSet(x, index)and, bitClear(x, index)Where x is the number whose bit has to be set/ cleared and index is the position of the bit (0 for least significant or right-most bit). This function makes changes in the number x in place, and also returns the updated value of x.Note that setting a bit means setting its value to 1, and clearing it means setting its value to 0.ExampleThe ... Read More
The constrain() function in Arduino helps to, as the name suggests, constrain a number between an upper bound and a lower bound.Syntaxconstrain(val, min, max)where, val is the number to be constrained, min is the lower bound value, and max is the upper bound valueIf val is less than min, this function will return min. If val is greater than max, this function will return max. As long as val is between min and max, this function will return val.ExampleThe following example illustrates the use of this function −void setup() { // put your setup code here, to run once: ... Read More
If you are a firmware developer, then shifting numbers, or registers by certain number of bits may be quite common for you. In Arduino as well, the same bit shift operators can be used, that are used in the C language, namely > for right shift.Syntaxx >> n or x > 1); Serial.println(x
The modulo operator in Arduino is exactly the same as in C language, or most other languages for that matter. The operator is %. The syntax is: a % b and it returns the remainder when a is divided by b.ExampleThe following example illustrates the use of this operator −void setup() { // put your setup code here, to run once: Serial.begin(9600); Serial.println(); Serial.println(10%3); Serial.println(4%2); Serial.println(50%9); } void loop() { // put your main code here, to run repeatedly: }OutputThe Serial Monitor output is shown below. You can work out the remainders yourself and verify that the output is correct.
The isSpace() and isWhitespace() functions can be used to check if a character is a space or, more specifically, a whitespace. Whitespace is a subset of space. While whitespace includes only space and horizontal tab (‘\t’), space includes form feed (‘\f’), new line (‘’), carriage return (‘\r’) and even vertical tab (‘\v’).ExampleThe following example demonstrates the usage of these functions −void setup() { // put your setup code here, to run once: Serial.begin(9600); Serial.println(); char c1 = 'a'; char c2 = ' '; char c3 = '\t'; char c4 = ''; ... Read More
Just like there is a function to check if a character is alphanumeric or not, there is another one to check if a character is a punctuation mark or not. The name of the function is isPunct(). It takes a character as an input and returns a Boolean: true if the given character is a punctuation mark.ExampleThe following example demonstrates the use of this function −void setup() { // put your setup code here, to run once: Serial.begin(9600); Serial.println(); char c1 = 'a'; char c2 = ', '; char c3 = '1'; char c4 ... Read More
Depending on your use case, you may need to check if a character is alphanumeric or not in Arduino. One example can be validating password strings, wherein you’ve allowed only alphanumeric characters for passwords. Or checking file names for storage in SD Card (sometimes some special characters are not allowed in file names). Arduino has an inbuilt function which checks whether a given character is alphanumeric or not. As you would have guessed, the function is isAlphaNumeric() and it takes in a character as the argument, and returns a Boolean.Examplevoid setup() { // put your setup code here, to ... Read More
Just like other bitwise operators, bitwise XOR also applies on the corresponding bits individually.The operator is ^ and the syntax is, a ^ bwhere a and b are the two numbers to be XORed.The truth table for XOR is given below −PQP^Q000011101110As you can see, the XOR operator returns 1 only when the two bits are different.If you perform 10 ^ 3, this is the computation that will happen at the bit level (assuming your board represents integers using 16 bits)00000000000010101000000000000000113000000000000100110 ^ 3 = 9ExampleLet’s verify this on the Serial Monitor. The code is given below −void setup() { ... Read More
Logical NOT is performed using the ! operator. The truth table is given below −ExpressionOutputTFFTAs you can see, the logical NOT inverts the truth value of the expression.ExampleThe usage can be understood from the example given below −void setup() { // put your setup code here, to run once: Serial.begin(9600); Serial.println(); int i = 10; if (!(i > 10)) { Serial.println("i is NOT greater than 10"); } else { Serial.println("i is greater than 10"); } } void loop() { // put your ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP