/* 24/07/2021 mathematicsv1 Addition, how to make something run once in the main loop. importance of commenting code How integers can over run with addition */ int q; int w; int myAnswer; long myLongAnswer; void setup() { // Serial.begin(9600) starts serial communication. 9600bps bits per second = 1200 characters per second Serial.begin(9600); //Send script name Serial.println("mathematicsv1..."); //this will print a blank line Serial.println(" "); } void loop() { if (q < 1) { //Example 1 //add 1 to the value of q so the if statement will fail next time round q++; Serial.print("q = "); Serial.println(q); //Simple addition Serial.println("Simple addition"); for (w = 0; w < 10; w++) { myAnswer = w + q; Serial.print(" w ="); Serial.print(w); Serial.print(" myAnswer ="); Serial.println(myAnswer); } //Example 2 //change the value of q to 32760 q = 32760; Serial.println("Simple addition...The problem of overflowing the variable type"); Serial.print("q = "); Serial.print(q); for (w = 0; w < 10; w++) { myAnswer = w + q; Serial.print(" w ="); Serial.print(w); Serial.print(" myAnswer ="); Serial.println(myAnswer); } //Example 3 q = 32760; Serial.println("Simple addition... 2 integers don't make a long"); Serial.print("q = "); Serial.print(q); for (w = 0; w < 10; w++) { myLongAnswer = w + q; Serial.print(" w ="); Serial.print(w); Serial.print(" myLongAnswer ="); Serial.println(myLongAnswer); } //Example 4 q = 32760; Serial.println("Simple addition... 2 integers converted to a long"); Serial.print("q = "); Serial.print(q); for (w = 0; w < 10; w++) { myLongAnswer = long(w) + long(q); Serial.print(" w ="); Serial.print(w); Serial.print(" myLongAnswer ="); Serial.println(myLongAnswer); } } }