Question

In: Computer Science

1) How do you extract a part of a string? Give the command and examples (in...

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)

Solutions

Expert Solution

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:

  1. LANGUAGE noInterrupts

  2. 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

Related Solutions

How do you build self confidence please give examples
How do you build self confidence please give examples
How do you choose the correct graph type? Explain and give examples?
How do you choose the correct graph type? Explain and give examples?
Give examples of the primary and secondary groups you are a part of, as well as...
Give examples of the primary and secondary groups you are a part of, as well as your experience with the three leadership styles (authoritarian, democratic, laissez-faire), and each type of formal organization (utilitarian, normative, and coercive). Which type of leadership style and organization are the best fit for you and how did the process of socialization influence the leadership style and type of organization you most identify with?
Part 1 readFile(String filename) In this method you are passed a String with the name of...
Part 1 readFile(String filename) In this method you are passed a String with the name of a file. This method will read the file in line by line and store each line in a String array. This String array is then returned. An example is shown below. File Contents: Purple Rain by Prince I never meant to cause you any sorrow I never meant to cause you any pain I only wanted one time to see you laughing I only...
1.) You can easily extract DNA from a strawberry. This is in part due to the...
1.) You can easily extract DNA from a strawberry. This is in part due to the fact that strawberries are octaploid so they have a lot of DNA in them, just this alludes to a disconnect between plant biologists/growers and the average consumer. When the general public were asked the following two questions there was a major disconnect between their understanding of basic biology. Question 1.) Does a regular tomato contain DNA? Consumers were likely to answer “No.” Question 2.)...
What do you understand by adjusting entries? Give examples.
What do you understand by adjusting entries? Give examples.
How do you interpret t statistic? Please give examples and describe how to interpret t statistic...
How do you interpret t statistic? Please give examples and describe how to interpret t statistic in detail What does a high/low t statistic mean?
Can you give some specific examples of institutional discrimination? How often do you think this type...
Can you give some specific examples of institutional discrimination? How often do you think this type of discrimination happens? Is it common?
Give two examples of how you can support a person’s spiritual wellbeing. Give two examples of...
Give two examples of how you can support a person’s spiritual wellbeing. Give two examples of how you can support a person’s cultural well-being Give two examples of how you can support a person’s financial well-being. How is a person’s well-being enhanced through involvement in career or occupation? Give two examples of basic requirements that enhance a person’s mental health.
how do NGO's lift comunities out of poverty do they eductae? give examples
how do NGO's lift comunities out of poverty do they eductae? give examples
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT