In: Computer Science
1) How do you extract a part of a string? Give the command and examples (in arduino)
2) Explain how negation and comparison operators combine together. (in Arduino)
3) What are the qualifiers? (in Arduino)
4) List the increment and decrement operators. (in Arduino)
1)
void loop() { // run over and over
if (mySerial.available()) {
String s = mySerial.readStringUntil('\n');
if (s.startsWith("$GPRMC")) {
int posHeader = s.indexOf("$GPRMC");
if (posHeader == 0) { // Got "$GPRMC"
Serial.println(s);
int posHour = s.indexOf(',');// Get first occurance of comma
","
Serial.print("Position of First Comma: ");
Serial.println(posHour);
gpsHour = s.substring((posHour + 1), 2);// Get GPS Hours from
String
Serial.print("GPS Hour: ");
Serial.println(gpsHour);
}
}
}
}
ffggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg
2)
The if
statement checks for a condition and
executes the proceeding statement or set of statements if the
condition is 'true'.
Syntax
if (condition) {
//statement(s)
}
Parameters
condition
: a boolean expression (i.e., can be
true
or false
).
Example Code
The brackets may be omitted after an if statement. If this is done, the next line (defined by the semicolon) becomes the only conditional statement.
if (x > 120) digitalWrite(LEDpin, HIGH);
if (x > 120)
digitalWrite(LEDpin, HIGH);
if (x > 120) {digitalWrite(LEDpin, HIGH);}
if (x > 120) {
digitalWrite(LEDpin1, HIGH);
digitalWrite(LEDpin2, HIGH);
}
// all are correct
Notes and Warnings
The statements being evaluated inside the parentheses require the use of one or more operators shown below.
Comparison Operators:
x == y (x is equal to y) x != y (x is not equal to y) x < y (x is less than y) x > y (x is greater than y) x <= y (x is less than or equal to y) x >= y (x is greater than or equal to y)
Beware of accidentally using the single equal sign (e.g.
if (x = 10)
). The single equal sign is the assignment
operator, and sets x
to 10 (puts the value 10 into the
variable x
). Instead use the double equal sign (e.g.
if (x == 10)
), which is the comparison operator, and
tests whether x
is equal to 10 or not. The
latter statement is only true if x
equals 10, but the
former statement will always be true.
This is because C++ evaluates the statement if
(x=10)
as follows: 10 is assigned to x
(remember that the single equal sign is the (assignment operator)),
so x
now contains 10. Then the 'if' conditional
evaluates 10, which always evaluates to TRUE
, since
any non-zero number evaluates to TRUE. Consequently, if (x =
10)
will always evaluate to TRUE
, which is not
the desired result when using an 'if' statement. Additionally, the
variable x
will be set to 10, which is also not a
desired action
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
3)
Description
volatile
is a keyword known as a variable
qualifier, it is usually used before the datatype of a
variable, to modify the way in which the compiler and subsequent
program treat the variable.
Declaring a variable volatile
is a directive to the
compiler. The compiler is software which translates your C/C++ code
into the machine code, which are the real instructions for the
Atmega chip in the Arduino.
Specifically, it directs the compiler to load the variable from RAM and not from a storage register, which is a temporary memory location where program variables are stored and manipulated. Under certain conditions, the value for a variable stored in registers can be inaccurate.
A variable should be declared volatile
whenever its
value can be changed by something beyond the control of the code
section in which it appears, such as a concurrently executing
thread. In the Arduino, the only place that this is likely to occur
is in sections of code associated with interrupts, called an
interrupt service routine.
int or long volatiles
If the volatile
variable is bigger than a byte
(e.g. a 16 bit int or a 32 bit long), then the microcontroller can
not read it in one step, because it is an 8 bit microcontroller.
This means that while your main code section (e.g. your loop) reads
the first 8 bits of the variable, the interrupt might already
change the second 8 bits. This will produce random values for the
variable.
Remedy:
While the variable is read, interrupts need to be disabled, so they can’t mess with the bits, while they are read. There are several ways to do this:
LANGUAGE noInterrupts
use the ATOMIC_BLOCK macro. Atomic operations are single MCU operations - the smallest possible unit.
Example Code
The volatile
modifier ensures that changes to the
state
variable are immediately visible in
loop()
. Without the volatile
modifier,
the state
variable may be loaded into a register when
entering the function and would not be updated anymore until the
function ends.
// Flashes the LED for 1 s if the input has changed
// in the previous second.
volatile byte changed = 0;
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
attachInterrupt(digitalPinToInterrupt(2), toggle, CHANGE);
}
void loop() {
if (changed == 1) {
// toggle() has been called from interrupts!
// Reset changed to 0
changed = 0;
// Blink LED for 200 ms
digitalWrite(LED_BUILTIN, HIGH);
delay(200);
digitalWrite(LED_BUILTIN, LOW);
}
}
void toggle() {
changed = 1;
}
To access a variable with size greater than the
microcontroller’s 8-bit data bus, use the ATOMIC_BLOCK
macro. The macro ensures that the variable is read in an atomic
operation, i.e. its contents cannot be altered while it is being
read.
#include <util/atomic.h> // this library includes the ATOMIC_BLOCK macro.
volatile int input_from_interrupt;
// Somewhere in the code, e.g. inside loop()
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
// code with interrupts blocked (consecutive atomic operations will not get interrupted)
int result = input_from_interrupt;
}
--------------------------------------------------------------------------------------------------------------------------------
4)
Assume variable A holds 10 and variable B holds 20 then −
Operator name | Operator simple | Description | Example |
---|---|---|---|
increment | ++ | Increment operator, increases integer value by one | A++ will give 11 |
decrement | -- | Decrement operator, decreases integer value by one | A-- will give 9 |