Go Down

Topic: Training and testing for new users. (Read 8348 times) previous topic - next topic

larryd

Aug 23, 2018, 07:51 pm Last Edit: Oct 03, 2018, 08:02 pm by larryd
The following posts challenge a new user's knowledge of Arduino coding and basic electronics.


Arduino questions with answers, based on an Arduino UNO:

1. 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.
2. 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.
3. What is a function? It is like a subroutine in BASIC, you can 'call' it from sections within the sketch.
4. What is the difference between global and local variables?  Global, access from anywhere, Local, access from with the module.
5. Where are global variables defined?  Outside of functions, normally at the top of the sketch.
6. 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.
7. What is wrong with this line of code?          #define counter = 0;      NO = and NO ;
8. What does  /*  and */  do to the lines of code that are written between these character pairs? Comments them out.
9. What does this line of code do?     return true;  The calling code gets a 'true' returned to it.
10.   If a variable has type float, how may bytes are set aside for the variable.      float foo = 0;      4
11. What does byte do in this line of code?    byte myFunction(int number) {  }     There is a returned variable, it is type byte.
12. Why is it important to use the smallest necessary type when you define your variables? Save SRAM
13. Why is it not recommended to use the delay() function in your sketches? delay() blocks sketch from running until the delay time is up.
14. What does this line of code do?    counter++;  counter is incremented by 1
15. What is SRAM?  Read write memory, where changeable/dynamic variables are.
16. What does the bootloader do? Loads the compiled code to static memory.
17. What two ways can you restart your Arduino? Power off and push the reset button
18. 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.    
19. What is the range of numbers you can store in a variable of type byte?     0 - 255
20. What is the index of the letter F in this array?     char textMessage[] = "Have Fun with Arduino";   5, zero relative.
21. How many elements are in the above array?  22 including the null terminating character \0
22. How many iterations are we setting up in this line of code? 
for( byte counter = 1; counter < 10;  counter++ );   9
23. Is there anything wrong with this line of code, if yes, what?         
if(myVar = true) Serial.println("The light is ON"); = should be ==
24. What is the difference in these two lines of code?   Serial.println("Test");    Serial.print("Test"); println gives a CR, print does not.
25. What is the purpose of && in this line of code?     if( foo  >=  100  &&  foo <= 200); 'and', both conditions must be met.
26. Why is it important to comment your lines of code?  So you can 'easily' understand what is happening in your code.
27. 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.
28. 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;
29. What is the value of foo when these lines of code are executed?      foo = 12;      foo = foo % 10;      2
30. 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
31. 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
32. What is the range of numbers that can be used for num in this line of code.   analogWrite(13, num);  Can be 0-255
33. What does this line of code do?    temp = !temp;  compliments 'temp',  if temp was HIGH it is now LOW vv
34. What does INPUT_PULLUP do in this line of code?      pinMode(2, INPUT_PULLUP); Turns on the internal pullup resistor ~20-50K
35. What does the map() function do?  A function that maps/converts a value from one range to another range.
36. If you are told your code is blocking, what does this mean?   Code execution slows/stops, makes the sketch unresponsive.
37. What does a pull down resistor do to a floating input pin?   Ensures a logic LOW when the pin is hi Z
38. What does this line of code do?   tone(4, 750, 500);  A 750 Hz tone on pin 4 for ½ second
39. What is the value of foo after this line is executed?        unsigned long foo = 60*60*1000UL;   3,600,000 one hour
40. Describe what is happening in this line of code.         Serial.println (F("Program starting."));
The string is stored in static memory, not SRAM.
41. 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.
42. What do the functions noInterrupts( ) and interrupts( ) do?  'Disables' interrupts and 'Enables' interrupts. Prevents code between these two functions from being interrupted.
No technical PMs.
If you are asked a question, please respond with an answer.
If you are asked for more information, please supply it.
If you need clarification, ask for help.

larryd

#1
Aug 23, 2018, 07:51 pm Last Edit: Aug 24, 2018, 01:55 am by larryd
A user has connected the circuits below.
What is wrong with the circuits connected to the following pins? D2, D3, D4 and D5
D2 The LED needs a current limiting resistor ~220R
D3 5 volts will not turn on 3 series LEDs
D4 Switch needs a pulldown resistor OR connect the top of switch to GND and use INPUT_PULLUP.
D5 LED is shorted out the way it is shown.




Another User has connected a different circuit.
They cannot get the LED to flash, what is wrong with their circuit?
The power rails are separated in the middle, need to place jumpers to connect LHS to RHS.






No technical PMs.
If you are asked a question, please respond with an answer.
If you are asked for more information, please supply it.
If you need clarification, ask for help.

larryd

#2
Aug 23, 2018, 07:51 pm Last Edit: Aug 23, 2018, 08:02 pm by larryd
A user has wired up an Arduino shield in the image below.


Which circuit below matches the wiring? A, B, C or NONE           B





No technical PMs.
If you are asked a question, please respond with an answer.
If you are asked for more information, please supply it.
If you need clarification, ask for help.

larryd

#3
Aug 23, 2018, 07:51 pm Last Edit: Aug 23, 2018, 08:03 pm by larryd
Referring to the circuit below, when D2 is LOW, what will the voltmeters read?

Assume Vf for a red LED = 2V  therefore "A" will be ~1.5V   "B" will be -1.5V  and "C" will be 2V.




No technical PMs.
If you are asked a question, please respond with an answer.
If you are asked for more information, please supply it.
If you need clarification, ask for help.

larryd

#4
Aug 23, 2018, 07:51 pm Last Edit: Aug 23, 2018, 09:03 pm by larryd
1. Identify what these components are and what function they serve:
Designation Component Name   What is its Function
R17 LDR Light causes a resistance change (detects light intensity)
R19 Resistor Input pull down resistor
R16 Resistor LED current limiting resistor
R14 Resistor Limits the pin D2 current while the gate capacitance charges
D10 Diode Kickback protection for the MOS FET
D9 LED Used as an indicator light
K1 Relay Provides isolation from the drive cct. Mechanical contacts. . .
Q4 N-MOS FET Provides drive current path to energize the realy                         
2. After this line of code executes, digitalWrite( 3 , HIGH );   will D8 be on or off?   OFF
3. Do you have to send a HIGH or a LOW to pin D4 to turn on D9?  HIGH
4. analogWrite(3 , 127); works, but analogWrite(4 , 127); doesn't work. Why is this?  Pin 4 is not for PWM.
5. What must the voltage rating of K1 be?  12V
6. If R17 is 20K what is the voltage at A0 on the UNO? Rt = 30K therefore, 5V/3 = ~1.67V
7. If Q4 is turned on, what is the resistance between pin 1 and 3 of K1?  Zero ohms






No technical PMs.
If you are asked a question, please respond with an answer.
If you are asked for more information, please supply it.
If you need clarification, ask for help.

larryd

#5
Aug 23, 2018, 07:51 pm Last Edit: Aug 23, 2018, 08:12 pm by larryd
A new user is trying to get a project working.  They have connected their circuit up exactly as seen in the schematic below.  Nothing works as it should.
Every time they plug in a new fuse into the motor circuit, the fuse burns out.
D6 is installed backwards, the drain and source on the MOS FET are swapped.
The LED does not turn on when the switch is pressed.
D7 is backwards.  Also, we need a pulldown resistor from D9 to GND, ~10K.
The IDE serial monitor is supposed to show what voltage is on input A0. The serial monitor displays a varying value without adjusting the pot.   You need to connect the battery negative to the Arduino GND.
What is wrong with these circuits?





No technical PMs.
If you are asked a question, please respond with an answer.
If you are asked for more information, please supply it.
If you need clarification, ask for help.

larryd

#6
Aug 23, 2018, 07:51 pm Last Edit: Aug 23, 2018, 08:13 pm by larryd
For the image below, write a line of code to:

• read the voltage on A0.
int myVar = analogRead(A0);

• send the reading from A0 to the LED D5.
analogWrite(9 , myVar/4);

• turn the LED D4 off.
digitalWrite(10 , HIGH);




No technical PMs.
If you are asked a question, please respond with an answer.
If you are asked for more information, please supply it.
If you need clarification, ask for help.

larryd

#7
Aug 23, 2018, 07:51 pm Last Edit: Aug 23, 2018, 08:14 pm by larryd
Code: [Select]

//**************************************************************
//
// Add comments to this sketch so a reader can easily follow what is happening.
// These comments must be more than just repeating what the line of code is doing.
//
//**************************************************************

//
#define LEDon  LOW
#define LEDoff HIGH


//**************************************************************

const byte redLED    = 7;
const byte greenLED  = 10;
const byte yellowLED = 13;
const byte Outputs[] = {7, 10, 13};

byte State;

const unsigned long redInterval    = 10000;
const unsigned long yellowInterval = 5000;
const unsigned long greenInterval  = 10000;

unsigned long LEDmillis;

//**************************************************************
void setup()
{
  for (byte x = 0; x < sizeof(Outputs); x++)
  {
    pinMode(Outputs[x], OUTPUT);
    digitalWrite(Outputs[x], LEDoff);
  }

  digitalWrite(redLED, LEDon);
 
  LEDmillis = millis();
 
  State = 0;   

  }

//**************************************************************
void loop()
{
  checkLEDs();
}

//**************************************************************
void checkLEDs()
{
  switch (State)
  {
    //**************************
    case 0:
      if (millis() - LEDmillis >= redInterval)
      {
        digitalWrite(redLED, LEDoff);
        digitalWrite(greenLED, LEDon);
        digitalWrite(yellowLED, LEDoff);

        State = 1;
        LEDmillis = millis();
      }
      break;

    //**************************
    case 1:
      if (millis() - LEDmillis >= greenInterval)
      {
        digitalWrite(redLED, LEDoff);
        digitalWrite(greenLED, LEDoff);
        digitalWrite(yellowLED, LEDon);

        State = 2;
        LEDmillis = millis();
      }
      break;

    //**************************
    case 2:
      if (millis() - LEDmillis >= yellowInterval)
      {
        digitalWrite(redLED, LEDon);
        digitalWrite(greenLED, LEDoff);
        digitalWrite(yellowLED, LEDoff);

        State = 0;
        LEDmillis = millis();
      }
      break;
  }
}

//**************************************************************






No technical PMs.
If you are asked a question, please respond with an answer.
If you are asked for more information, please supply it.
If you need clarification, ask for help.

larryd

#8
Aug 23, 2018, 07:52 pm Last Edit: Aug 23, 2018, 08:15 pm by larryd
Code: [Select]
//What does this sketch do?

const int ledPin          = 13;
const int potentiometer   = A0;
unsigned long startTime;

void setup()
{
  pinMode(ledPin, OUTPUT);
}

void loop()
{
  int rate = analogRead(potentiometer);
  rate = map(rate, 0, 1023, 5000, 100);
  checkTime(rate);
}

void checkTime(int interval)
{
  static byte LEDstate = HIGH;
  if (millis() - startTime >= interval)
  {
    startTime = millis();
    digitalWrite(ledPin, LEDstate);
    LEDstate = !LEDstate;
  }
}

No technical PMs.
If you are asked a question, please respond with an answer.
If you are asked for more information, please supply it.
If you need clarification, ask for help.

larryd

#9
Aug 23, 2018, 07:52 pm Last Edit: Aug 23, 2018, 08:16 pm by larryd
Future
No technical PMs.
If you are asked a question, please respond with an answer.
If you are asked for more information, please supply it.
If you need clarification, ask for help.

ElectroDFW

Wow. Talk about throwing us in the deep end.
As a hardware technician just getting started with an Uno, 95% of those questions are Greek to me.
Also, the inline images of the circuit diagrams are missing in replies 2 through 6.

Regards,
David M.

larryd

#11
Aug 27, 2018, 07:05 pm Last Edit: Aug 27, 2018, 07:22 pm by larryd
Images are included and should be visible in your browser.
If you cannot see them, try a different browser.

All answers are in beige ink on white, included except post #7 and 8.










No technical PMs.
If you are asked a question, please respond with an answer.
If you are asked for more information, please supply it.
If you need clarification, ask for help.

lastchancename

#12
Aug 28, 2018, 12:17 am Last Edit: Aug 28, 2018, 12:17 am by lastchancename
PLEASE DONT POST YOUR ANSWERS WITHIN THIS THREAD !

It will be better if you discuss the problems offline, or in a new, different thread - so this topic remains 'clean', and doesn't confuse other newbies with incorrect 'guesses' that mislead others.

Nice work LarryD
Experienced responders have a nose for laziness, (they were beginners once)... Sure, there are trolls, chest-beaters, and pretenders - but the help you'll get here is about as good as it gets - if you try to help youself!.

westfw

Quote
8. What does  /*  and *\ do to the lines of code that are written between these character pairs?
Is that a typo, or a trick question?

westfw

(Ctrl-t doesn't do anything on MY Mac)


Go Up