Suppose we have an array of n strings called names. We have to make n directories in the file system such that, at the ith minute, we will create a directory with the name names[i]. Two files cannot have the same name, if we enter a duplicate directory name the system will have a suffix addition to its name in the form of (k), here, k is the smallest positive integer such that the obtained name remains unique. We have to find an array of strings of length n where ans[i] is the actual name that will be assign to ... Read More
Suppose we have two positive values n and k. Now consider we have a list of all factors of n sorted in ascending order, we have to find the kth factor in this list. If there are less than k factors, then return -1.So, if the input is like n = 28 k = 4, then the output will be 7 because, the factors of 28 are [1, 2, 4, 7, 14, 28], fourth one is 7.To solve this, we will follow these steps −if k is same as 1, thenreturn 1cand := a list with one element [1]for i ... Read More
The do-while loop’s syntax in Arduino is similar to the syntax in C. It is given below −do{ //Code } while (condition);Note the semicolon at the end.Examplevoid setup() { // put your setup code here, to run once: Serial.begin(9600); Serial.println(); int i = 5; do{ Serial.println(i); i--; } while(i > 0); } void loop() { // put your main code here, to run repeatedly: }OutputThe Serial Monitor output is shown below −
In a previous article, we used the TimerOne library to add timer interrupts to Arduino. But what if we wish to generate timer interrupts without a third-party library? In that case, you will directly have to meddle with the timer registers in Arduino. In this article, we will just introduce the registers relevant to timer operations and explain their significance. We will also provide the page numbers of the ATmega328 (used in Arduino Uno) datasheet wherein you can find detailed information on these registers.You can find the datasheet here −https://ww1.microchip.com/downloads/en/DeviceDoc/Atmel7810-Automotive-Microcontrollers-ATmega328P_Datasheet.pdfTCCRxA and TCCRxBThese are timer control registers. The x stands for ... Read More
Arduino has support for several popular math functions, square and square root being among them. Let’s look at the square root first.Syntaxsqrt(x)where x is a number of any data type. It returns a double.For square, you ideally shouldn’t need a separate function. You can just multiply the number by itself.x_squared = x*x;However, Arduino does have a separate function for calculating squares. The syntax is −sq(x) where x is a number of any data type. This again returns a double.ExampleThe following example illustrates the use of these functions −void setup() { // put your setup code here, to run once: ... Read More
Arduino provides 3 basic trigonometric functions: sin(), cos() and tan(). All other trigonometric expressions can be derived from these three functions.All the three functions take in angle in radians (type float) as the input. They return a double.For sin() and cos(), the value is between -1 and 1. The value for tan() has no such bounds.ExampleThe example code below illustrates the use of these functions −void setup() { // put your setup code here, to run once: Serial.begin(9600); Serial.println(); float pi = 3.14159; float angle_deg = 30; float angle_rad = angle_deg*pi/180; Serial.println(sin(angle_rad)); ... Read More
Generating random numbers is one of the key requirements from microcontrollers. Random numbers have several applications. Let’s not get there. You must have an application in mind, which brought you to this page. Generating random numbers is very easy in Arduino, thanks to the inbuilt random() function.Syntaxrandom(min, max)ORrandom(max)where min is 0 by default.Min is inclusive, while max is exclusive. Thus, random(10, 50) will return a number integer between 10 and 49 (10 and 49 included). random(100) will return a random number between 0 and 99, both included. Note that the random function’s return type is long.Examplevoid setup() { // put ... Read More
In an earlier article, we have seen how PWM can be set on Arduino Uno using the analogWrite() function. Pins 3, 5, 6, 9, 10 and 11 of Arduino Uno can support PWM. The frequency of the square wave is 490 Hz (about 2 ms time period) on all pins except 5 and 6, on which it is 980 Hz (about 1s time period). With analogWrite() you get control over the duty cycle, but not on the frequency of the generated square wave.In this article, we will look at another way of setting PWM in Arduino Uno, specific to Timer1. The advantage ... Read More
Suppose we have a binary array called nums, we can delete one element from it. We have to find the size of the longest non-empty subarray which is containing only 1's in the resulting array. If there is no such subarray, then return 0.So, if the input is like nums = [1, 0, 1, 1, 1, 0, 1, 1, 0], then the output will be 5 because by removing 0 from position 5, we can get a subarray [1, 1, 1, 1, 1] there are five 1s.To solve this, we will follow these steps −if 0 is not in nums, ... Read More
Suppose we have an array called nums, this array contains even number of elements, and have another value k. We have to split nums into exactly n/2 pairs such that the sum of each pair is divisible by k. If we can do so then return true, otherwise false.So, if the input is like nums = [9, 5, 3, 4, 7, 10, 20, 8] k = 3, then the output will be True because we can make pairs like (9, 3), (5, 7), (4, 20), (8, 10), sum of all pairs are divisible by 3.To solve this, we will follow ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP