Code meaning for Arduino Uno

Hi there,

I’m a super beginner at coding so I decided to look on youtube for projects similar to mine. I don’t understand what the code means, so could you please explain what the lines of code mean?

#include <LiquidCrystal.h>

LiquidCrystal lcd(7, 6, 5, 4, 3, 2);

void setup()
{
  lcd.begin(16, 2);
  lcd.clear();

  Serial.begin(9600);

  pinMode(8, INPUT);
  digitalWrite(8, HIGH);
  pinMode(9, INPUT);
  digitalWrite(9, HIGH);
  pinMode(10, INPUT);
  digitalWrite(10, HIGH);

}
double i = 0;
double a = millis();
double c ;
void loop()
{
lcd.clear();
lcd.print("Ready to Wrestle?");
lcd.setCursor(0,1);
lcd.print("Press to start:)");
delay(100);

 if(digitalRead(8) == LOW)
 {

    lcd.clear();
    a = millis();
   while(digitalRead(9) == HIGH)
   {

     c = millis();
   i = (c - a) / 1000;
   lcd.print(i);
   lcd.setCursor(8,0);
   lcd.print("Seconds");
   lcd.setCursor(0,0);
   Serial.println(c);
   Serial.println(a);
   Serial.println(i);
   Serial.println("...");
   delay(500);
   }

   if(digitalRead(9) == LOW)
   {
     while(digitalRead(8) == HIGH)
     {
       lcd.setCursor(0,0);
       lcd.print(i);
       lcd.setCursor(8,0);
       lcd.print("Seconds");
       lcd.setCursor(0,0);
       delay(500);
     }
   }

 }
}

Thank you

Which lines of code are you having trouble with ?

Mostly the pinModes, double and Serial.println

pinMode tells the microcontroller how to act on a pin: read data from it or send data to it. Output also increases the current to the pin so you can light a LED.
Serial.println is a variation on Serial.print. Both output data through the USB cable back to the computer for displaying it in the Serial monitor (under Tools in the menu). ln means it will be finished with a new-line character (return).
So this sketch draws both data on the attached LCD screen, and in a window on the computer.
The use of double is quite weird here. The timer only counts whole numbers, not fractions, and double is a floating point variable. You better use “unsigned long” instead, that is for whole numbers without negatives (time is never negative).

pinMode(8, INPUT);
This says make pin D8 on the Arduino an input pin (at power up time it already is an input though)

Another option to use is
pinMode(8, INPUT_PULLUP);
this says make the pin D8 an input and also turn on the internal 20k pullup resistor for this pin.


double is the data ‘type’ for the variable you are defining

on the Arduino , double is the same size as float .

Number 'type’s.

  • boolean (8 bit) - simple logical true/false, Arduino does not use single bits for bool

  • byte (8 bit) - unsigned number from 0 to 255

  • char (8 bit) - signed number from -128 to 127. The compiler will attempt to interpret this data type as a character in some circumstances, which may yield unexpected results

  • unsigned char (8 bit) - same as ‘byte’; if this is what you’re after, you should use ‘byte’ instead, for reasons of clarity

  • word (16 bit) - unsigned number from 0 to 65535

  • unsigned int (16 bit)- the same as ‘word’. Use ‘word’ instead for clarity and brevity

  • int (16 bit) - signed number from -32768 to 32767. This is most commonly what you see used for general purpose variables in Arduino example code provided with the IDE

  • unsigned long (32 bit) - unsigned number from 0 to 4,294,967,295. The most common usage of this is to store the result of the millis() function, which returns the number of milliseconds the current code has been running

  • long (32 bit) - signed number from -2,147,483,648 to 2,147,483,647

float (32 bit) - signed number from -3.4028235E38 to 3.4028235E38. Floating point on the Arduino is not native; the compiler has to jump through hoops to make it work. If you can avoid it, you should. We’ll touch on this later. Sparkfun.

You select the ‘type’ best suited for your variables.

ex:

  • your variable does not change and it defines a pin on the Arduino. const byte limitSwitchPin = 34;
  • since an analog variable can be 0 to 1023, a byte will not do, you can select ‘int’. int temperature;
  • if your variable needs to be within -64 to +64 a ‘char’ will do nicely. char joystick;
  • if your variable is used for ASCII then you need type ‘char’,
    char myText[] = {"Raspberry Pie Smells"};
  • if your variable enables some code then boolean can be used. boolean enableFlag = false;
  • millis() returns the time in ms since rebooting, unsigned long currentTime = millis();

Serial.println
Says to print something to the serial monitor the to add a line feed.

Documentation is here:
https://www.arduino.cc/reference/en/

“pinMode()” is near the very beginning, under “Functions:Digital I/O”.

“double” is down under “Variables:Data Types”

“Serial” is over on the right under “Functions:Communication” and on that page you will find “print()” and “println()”

LiquidCrystal is a built-in library so you would find it under ‘Libraries’.

Thank you! That makes sense

Hey,
Which line of code, you have face errors.

This code below because none of the jumper wires connect on pins 8,9,10 on the LCD so does this make sense?

pinMode(8, INPUT);
  digitalWrite(8, HIGH);
  pinMode(9, INPUT);
  digitalWrite(9, HIGH);
  pinMode(10, INPUT);
  digitalWrite(10, HIGH);

[/quote]

Those are input-pins with pullup resistor. Don’t you have a LCD shield or such a thing, with buttons on those pins?

Oh geez I confused the pin from the button and attached it to the arduino instead of the lcd. Thank you!!

Wait actually no I assembled it correctly, but I understand the input now.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.