12. Arithmetic Operators
+ - * / % =
void loop () {
int a = 9, b = 4, c;
c = a + b;
c = a – b;
c = a * b;
c = a / b;
c = a % b;
}
13. Arithmetic Operators
+ - * / % =
void loop () {
int a = 9, b = 4, c;
c = a + b; //13
c = a – b; //5
c = a * b; //36
c = a / b; //2
c = a % b; //1
}
16. Boolean Operators
&& || !
void loop () {
int a = 9, b = 4
if((a > b) && (b < a)){}
if((a == b) || (b < a)){}
if( !(a == b) && (b < a)){}
}
17. Boolean Operators
&& || !
void loop () {
int a = 9, b = 4
if((a > b) && (b < a)){} // true
if((a == b) || (b < a)){} // true
if( !(a == b) && (b < a)){} // true
}
18. Bitwise Operators
& | ^ ~ << >>
void loop () {
int a = 10; // 0b00001010
int b = 20; // 0b00010100
int c;
c = a & b ;
c = a | b ;
c = a ^ b ;
c = ~a ;
c = a << 2 ;
c = b >> 2 ;
}
19. Bitwise Operators
& | ^ ~ << >>
void loop () {
int a = 10; // 0b00001010
int b = 20; // 0b00010100
int c;
c = a & b ; // 0b00000000
c = a | b ; // 0b00011110
c = a ^ b ; // 0b00011110
c = ~a ; // 0b11110101
c = a << 2 ; // 0b00101000
c = b >> 2 ; // 0b00000101
}
20. Bitwise Operators
++ – += -= *= /= %= |= &=
void loop () {
int a = 10, b = 20
int c = 0;
a++;
a--;
b += a; // b=b+a
b -= a; // b=b-a
}
21. Bitwise Operators
++ – += -= *= /= %= |= &=
void loop () {
int a = 10, b = 20
int c = 0;
a++; // 11
a--; // 9
b += a; // 30
b -= a; // 10
}
22. Challenges:
1. Take user input from the keyboard to change the color of an RGB LED.
https://www.arduino.cc/en/Tutorial/BuiltInExamples/SwitchCase2
2. Create an array of 255 distinct values (bonus if the numbers are meaningful in some way).
https://www.arduino.cc/reference/en/language/variables/data-types/array/
3. Tell if a user's input was even or odd.
https://www.tutorialspoint.com/arduino/arduino_arithmetic_operators.htm
4. Take a diameter of a circle from the user and return the circumference as a decimal (e.g. 8.56930).
https://www.arduino.cc/reference/en/language/variables/data-types/float/
5. BONUS: Make code that generates abstract ASCII art/drawings in the serial monitor!!
https://en.wikipedia.org/wiki/ASCII_art
https://manytools.org/hacker-tools/convert-images-to-ascii-art/
6. *BONUS BONUS*: Make a text-based "Choose your own adventure" video game!!
https://en.wikipedia.org/wiki/Colossal_Cave_Adventure
https://en.wikipedia.org/wiki/Choose_Your_Own_Adventure
24. Challenges:
2. Create an array of 255 distinct values (bonus if the numbers are meaningful in some way).
https://www.arduino.cc/reference/en/language/variables/data-types/array/
void setup(){
Serial.begin(9600);
}
void loop(){
delay(100);
int my_array[252];
for (int i = 0; i < 252; i++) {
my_array[i]=i;
delay(100);
Serial.println(my_array[i]);
}
}
OR:
int A[255]; //create an array composed of 255 elements
void setup() {
Serial.begin(9600); //initialize the serial monitor
for( int i = 254; i >= 0; i--){ //initialize a variable "i" which the value decrements until it reaches 0.
A[i] = i; //define an element of the array as "i"
Serial.println(A[i]); //display the array in the serial monitor
}
}
void loop() {
}
26. Challenges:
4. Take a diameter of a circle from the user and return the circumference as a decimal (e.g. 8.56930).
https://www.arduino.cc/reference/en/language/variables/data-types/float/
float Pi = 3.14; //Create a variable Pi
int d = 0; //Creat a variable "d" as diameter
void setup() {
Serial.begin(9600);
}
void loop() {
if (Serial.available() > 0.0) {
d = Serial.read()-48; //convert from ASCII to integer
float c = Pi * (float(d)); //Create "c", Circonference is equal to Pi x diameter
Serial.println(c);
}
}