Arduino for Beginners: Comprehensive Beginners Guide to Learn Arduino Programming Step by Step (Arduino Programming for Beginners Book 1) by THORPE ETHAN

Arduino for Beginners: Comprehensive Beginners Guide to Learn Arduino Programming Step by Step (Arduino Programming for Beginners Book 1) by THORPE ETHAN

Author:THORPE, ETHAN [THORPE, ETHAN]
Language: eng
Format: epub
Published: 2019-07-11T16:00:00+00:00


void loop() {

​ int g = 2;

​ int h = 3;

​ int z;

​ k = myMultiplyFunction(g, h);

​ Serial.print(z);

​ delay(500);

}

Do not leave spaces in the declaration of names within your function. Arduino will only accept 0-9, A-Z and the underscore (_), a function is not allowed to start with a number.

A functions body must be contained within braces as we have seen with the “voidloop(){” and “void setup(){” examples. The type of function is declared first (int, void, const). The name of the function usually follows (myMultiplyFunction, setup, loop). After this is to be the body of the function contained within curly braces.

Within a functions body, a return type must always be present, even if it is void like in “void loop” . The function must be called within the “void setup()” body, by either simply declaring the name as in the above example or for those that are not integers;

void DashedLine() {

​ serial.println( “----------” );

}

void loop() {

}

void setup() {

​ serial.begin(9600);

​ DashedLine();

}

As you will notice the function is called without any preceding “int” call. This is because so far, we had been dealing with integers (numbers), but because the new function is simply letters, we can call on it without preceding it with any data type.

Note: we used “serial.println” and “serial.print” , both attributes do the same thing, with the exemption that “serial.println” does this on a new line while “serial.print” does it on the same line.

There are many different functions out there, we will list a few.

Digital I/O

digitalRead(pin, value) : This function reads the value of a specified digital pin (HIGH/LOW).

digitalWrite(pin, value) : This function writes a value (HIGH/LOW) to a digital pin.

pinMode(pin, mode) : This function configures the specified pin to behave as either input or output.

Analog I/O

analogRead(pin) : This function reads the value from the specified analog pin.

analogReference(type) : This function configures the reference voltage for analog input (DEFAULT/INTERNAL/EXTERNAL/INTERNAL1V1…)

analogWrite(pin, value) : This function writes an analog value to the specific pin (PWM pins).

Math

abs(x) : This calculates the absolute value of a number. It returns +x if >= 0 and -x if < 0.

constrain(x, a, b) : This function constrains a number within range. X will be the constrained number (all data types), a is the lower end of the range (all data types) and b will be the upper range (all data types). It will return x - if within range, a – if less than a and b – if greater than b.

map(value, fromLow, fromHigh, toLow, toHigh) : This re-maps a number from one range to another. It returns the mapped value

max(x, y) : This calculates the maxima of two numbers. It returns the larger of the two numbers

min(x, y) : This calculates the minimum of two numbers. It will return the smaller of the numbers.

pow(base, exponent) : It calculates the value of a number raised to a power. Used to generate curves… It returns the exponent result. The data type for exponent is float, for base is float and for return is double.

sq(x) : This will calculate the square root of a number.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.