The following posts challenge a new user's knowledge of Arduino coding and basic electronics.
Arduino questions with answers, based on an Arduino UNO:
- When you are writing a sketch in the Arduino IDE, what happens to your sketch when you press Ctrl+T, Cmd+T? Formats the code to make it more easily read.
- If weird characters appear on the IDE serial monitor, what is likely wrong? You may need to match monitor baud rate to sketch baud rate.
- What is a function? It is like a subroutine in BASIC, you can ‘call’ it from sections within the sketch.
- What is the difference between global and local variables? Global, access from anywhere, Local, access from with the module.
- Where are global variables defined? Outside of functions, normally at the top of the sketch.
- What does const tell the compiler in this line of code? const int myVariable = 13; Variable will not change i.e. it is a constant, takes no SRAM.
- What is wrong with this line of code? #define counter = 0; NO = and NO ;
- What does /* and */ do to the lines of code that are written between these character pairs? Comments them out.
- What does this line of code do? return true; The calling code gets a ‘true’ returned to it.
- If a variable has type float, how may bytes are set aside for the variable. float foo = 0; 4
- What does byte do in this line of code? byte myFunction(int number) { } There is a returned variable, it is type byte.
- Why is it important to use the smallest necessary type when you define your variables? Save SRAM
- Why is it not recommended to use the delay() function in your sketches? delay() blocks sketch from running until the delay time is up.
- What does this line of code do? counter++; counter is incremented by 1
- What is SRAM? Read write memory, where changeable/dynamic variables are.
- What does the bootloader do? Loads the compiled code to static memory.
- What two ways can you restart your Arduino? Power off and push the reset button
- What does the millis() function return when it is called? Current run time in milliseconds since last reset, it resets to 0 at ~49 days and starts incrementing again.
- What is the range of numbers you can store in a variable of type byte? 0 - 255
- What is the index of the letter F in this array? char textMessage[] = “Have Fun with Arduino”; 5, zero relative.
- How many elements are in the above array? 22 including the null terminating character \0
- How many iterations are we setting up in this line of code?
for( byte counter = 1; counter < 10; counter++ ); 9 - Is there anything wrong with this line of code, if yes, what?
if(myVar = true) Serial.println(“The light is ON”); = should be == - What is the difference in these two lines of code? Serial.println(“Test”); Serial.print(“Test”); println gives a CR, print does not.
- What is the purpose of && in this line of code? if( foo >= 100 && foo <= 200); ‘and’, both conditions must be met.
- Why is it important to comment your lines of code? So you can ‘easily’ understand what is happening in your code.
- What does the word static do in this line of code? static int temperature = 0; The variable’s value is retained the next time it is accessed.
- This error is displayed after compiling: ‘temp’ was not declared in this scope. Why? temp is only available inside the for loop.
for(byte temp = 0; temp <=200; temp = temp +10) { Serial.println(temp); }
byte myVar = temp; - What is the value of foo when these lines of code are executed? foo = 12; foo = foo % 10; 2
- Is there anything wrong with this line of code, if yes, what?
for( char counter = -10; counter < 10; counter++) It is okay, char # range is -128 to 127 - If the A0 pin is connected to 2.5 volts, what is the value of foo? unsigned int foo = analogRead(A0); 2.5V is ½ of 5V therefore since the range being 0-1023, value is 512/511
- What is the range of numbers that can be used for num in this line of code. analogWrite(13, num); Can be 0-255
- What does this line of code do? temp = !temp; compliments ‘temp’, if temp was HIGH it is now LOW vv
- What does INPUT_PULLUP do in this line of code? pinMode(2, INPUT_PULLUP); Turns on the internal pullup resistor ~20-50K
- What does the map() function do? A function that maps/converts a value from one range to another range.
- If you are told your code is blocking, what does this mean? Code execution slows/stops, makes the sketch unresponsive.
- What does a pull down resistor do to a floating input pin? Ensures a logic LOW when the pin is hi Z
- What does this line of code do? tone(4, 750, 500); A 750 Hz tone on pin 4 for ½ second
- What is the value of foo after this line is executed? unsigned long foo = 60601000UL; 3,600,000 one hour
- Describe what is happening in this line of code. Serial.println (F("Program starting."));
The string is stored in static memory, not SRAM. - Why should variables, accessed outside in an interrupt service routine and modified in the ISR, be given the ‘volatile’ qualifier? Making them volatile makes them reside in RAM. This prevents them from being placed in a ‘Storage Register’ where it could be inadvertently changed.
- What do the functions noInterrupts( ) and interrupts( ) do? ‘Disables’ interrupts and ‘Enables’ interrupts. Prevents code between these two functions from being interrupted.