1. Increment and Decrement
An increment and decrement operator is an arithmetic operator that is used to increment or decrement an integer variable by a value of one. We look at how to use this operator in this part of the Sketch code programming with Arduino IDE below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
int current = 8; // This is variable the keeps which light must be turn on next void setup() { Serial.print(" 14CORE | Math Test Code"); delay(3000); for (int i = 8; i < 16; i++) // set this pins 8-15 as outputs { pinMode(i, OUTPUT); } } void loop() { turn_all_off(); // turns all LEDs off digitalWrite(current, HIGH); // set the current light ON delay(200); // wait for 200ms current++; // increment current LED if (current > 15) // range check, if last LED goes back to LED 8 { current = 8; } } void turn_all_off() // function to turn off all the LEDs { for (int i = 8; i < 16; i++) // connected to digital pins 8-15 { digitalWrite(i, LOW); } } |
2. Operator Precedence Sketch Test Code
Practically every sketch code uses mathematical operators to manipulate the value of variables. This example provides a brief overview of the most common mathematical operators. Moreover, if we will not explicitly state the order in which an expression is considered, this will be evaluated based on the operator precedence. For example, in 3+6*9, the 2 first will be multiplied by 9 and then the result will be added to 6 because the ( * ) has higher precedence than the ( + ). However, to avoid ambiguity in the program, it is advised that the statement be written as 3+(9*8). The order of evaluation is controlled through the placement of parenthesis in the code syntax. Here is the table of the operator’s precedence listed below. The highest precedence is at the top of the list and the lowest is at the bottom.
- Muliplicative : * / %
- Additive : + –
- Relational : <> <= >=
- Equality: == !=
- Logical AND: &&
- Logical OR: ||
- Assigname: = += -= *= /= %=
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
void setup() { Serial.print(" 14CORE | Math Test Code"); delay(3000); Serial.begin(9600); for (int i = 0; i < 200; i += 4) { // The 30 is added to 70 and then evaluated // if it is greater than the current value of "i" // For clarity, write as "if (i > (30 + 70)) {" if (i > 30 + 70) { Serial.print(i, DEC); Serial.print(" "); } } Serial.println(); // The 2 is multiplied by the 8 and the result is added to the 5 int result = 3 + 6 * 9; Serial.println(result, DEC); // The 2 is added to the 4 and the result is multiplied by the 8 result = (3 + 6) * 9; Serial.println(result, DEC); for (int i = 0; i < 200; i += 2) { // The relational statements are evaluated // first, and then the logical AND statements and // finally the logical OR. if (i > 20 && i < 50 || i > 100 && i < 200 - 20) { Serial.print(i, DEC); Serial.print(" "); } } } void loop() { } |
3. Modulo Test Sketch Code
The modulo ( % ) operator will return the remainder of a number divided by another additionally it is often used to keep the values within a range of the set.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
int num = 0; void setup() { Serial.print(" 14CORE | Math Test Code"); delay(3000); Serial.begin(9600); } void loop() { num = (num + 1) % 10; // num goes from 0 to 9 Serial.println(num, DEC); delay(50); } |
4. Sine / Cosine Test Sketch Code
SINE – sin() & COSINE – cos(). This number between 0 and PI*2 – TW0_PI which is approximately 6.28 are placed into these functions and numbers between -1 and 1 are returned.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
void setup() { Serial.begin(9600); Serial.println("angle / cos(angle) / sin(angle)"); for (int angle = 0; angle <= 90; angle += 5) { float c = cos(radians(angle)); float s = sin(radians(angle)); Serial.print(angle); Serial.print(" / "); Serial.print(c); Serial.print(" / "); Serial.println(s); } } void loop() { } |
5. Random Test Sketch Code
Each time the code is executed the result value is different, it will generate pseudo-random numbers. In addition, the syntax is used as random() / Random(max) / random(min, max).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
int num; void setup() { Serial.print(" 14CORE | Math Test Code"); delay(3000); Serial.begin(9600); randomSeed(200); } void loop() { num = random(255); // generate a random number between 0 and 255 Serial.println(num, DEC); delay(30); } |
6. Interrupts Test Sketch Code
An interrupt is a signal provided by the hardware or software when a process or event needs immediate attention. It alerts the processor to request a priority method needing interruption of the current process. most SOCS and MCU’s IO has a dedicated pin for this purpose and it is called the Interrupt Service Routing or ISR pin. For example, case, when your car is running, and your can ECU / Microcontroller is busy sensing the speed of your car, requesting other sensor’s data, controlling an AC, Fuel Status, Engine Temperature, and so on. however, your car suddenly hit a roadblock then an accident happened! At this time controller stops all the process and send a signal to deploy an airbag. This is the rule of the interrupt signal, it is used as a very high priority for all. There are 2 types of interrupts, Hardware Interrupts and Software Interrupts – hardware interrupts occur in response to an external event, which is the state your IO pin turns to HIGH or LOW. Software interrupts are instructions to the hardware interrupts.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
void setup() { Serial.print(" 14CORE | Math Test Code"); delay(3000); // set myFunction to be called everytime // interrupt 2 is generated (everytime the pin gets from LOW to HIGH) pinMode(EI2, INPUT); // set External interrupt pin as INPUT // On Wiring S pin 18 // On Wiring v1 pin 2 pinMode(8, OUTPUT); // pin to be used as trigger in this example attachInterrupt(EXTERNAL_INTERRUPT_2, myFunction, RISING); Serial.begin(9600); // Starts serial to print data } void loop() { // pin 8 will be directly attached to the External interrupt 2 // pin 18 on Wiring S or pin 2 on Wiring v1 boards // just to test the interruption generation digitalWrite(8, LOW); delay(500); digitalWrite(8, HIGH); delay(500); } void myFunction() { // WARNING: you should avoid doing long procedures in interrupt routines // Since this is an interrupt routine // and Serial is an interrupt driven output // interrupts must be enabled before printing interrupts(); Serial.println("Interrupt generated"); } Wiring is an open project initiated |
7. Timer Sketch Test Code
A timer is similar to an interrupt and a simple clock that can measure time intervals of an event cycle, all MCU (Microcontrollers) have a built-in clock called an oscillator, for example, the ATMEGA328P currently it has 16Mhz which means it has a response speed at 16Mhz, the higher the clock frequency the higher will be the processing speed. A timer uses to counter which counts at a certain speed depending upon the MCU clock speed frequency. For most MCU it takes 1/16000000 seconds or 62 Nano Seconds to make a single count. in other words, an MCU moves from one instruction to another instruction every 62 nanoseconds.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
Demonstrates how to create a timer to print a message every 7 seconds unsigned long previousTime = 0; void setup() { Serial.print(" 14CORE | Math Test Code"); delay(3000); Serial.begin(9600); previousTime = millis(); // mark the initial time } void loop() { // if 7 seconds have passed since last mark if ((millis() - previousTime) >= 7000) { Serial.println("seven seconds have passed"); //print message previousTime = millis(); // mark again } } |
8. Serial Read & Write Test Sketch Code
Serial is used for communication for all types of microcontroller boards especially Arduino boards to speak and talk to your computer or other devices. All the most popular microcontroller development boards have at least one serial port also known as a UART or USART. Serial.read() is a function that reads incoming serial data transmission, hence Serial.write() writes binary data to the serial port and sent as a byte or series of bytes; to send the characters representing the digits of a number use the print() function.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
Simple ReadWrite Read data from the serial port and echoes it back to the serial monitor char val; // to read the char void setup() { Serial.print(" 14CORE | Math Test Code"); delay(3000); Serial.begin(9600); } void loop() { if (Serial.available()) // if data available { val = Serial.read(); // read data Serial.print(val); // print it back } delay(100); // wait 100ms for next read } |