- Published on
Level 3
Table of Contents
Introduction
Look at the code snippet given below:
int ledPin = 13;
int time = 1000;
void setup()
{
pinMode(ledPin,OUTPUT);
}
void loop()
{
digitalWrite(ledPin,HIGH);
delay(1000); // Wait for 1000 millisecond(s)
digitalWrite(ledPin,LOW);
delay(1000); // Wait for 1000 millisecond(s)
}
Let us now break down the code and understand each element written in it
void setup()
This is a pre existing function which will always be present whenever you open any arduino based IDE. As the name suggests, “setup” means to initialize the arduino, and tell it the necessary information required for it to connect with the peripheral devices connected to it (its pretty obvious why we need to do that).
Here's a link for the setup() function
void loop()
Also a pre existing function in arduino, this is a basic loop function, any code written inside it will go on in loop until power is cut off to the arduino, keep in mind void setup just runs at the starting of arduino just to initialize things, after that the code inside the loop gets run over and over.
A link for the loop() function
pinMode()
Before you send/receive a signal to/from a particular pin of the arduino, you need to define it as an input, or an output pin.
- Defining it as an input pin means that you want to receive a signal from it, as soon as it get triggered by say, a device connected to that pin.
- Defining it as an output pin means that you want the pin to send out a signal to that particular device, and instruct it to initiate a task. For example, turning on/off an LED by sending out a HIGH/LOW signal respectively.
Syntax : pinMode(pin, mode)
Here is the link for documentation of the pinmode function
digitalWrite()
We all know signals are of two types digital and analog, analog signals are continuous within a given range of amplitude and digital signals are discrete namely (0 and 1), or in our case(LOW and HIGH).
HIGH:- simply means the pin will have the voltage equal to the voltage “VCC” supplied to the board from an external power source. LOW:- Simply means the pin will have the voltage equal to zero.
Note that there is a capital W in digitalWrite() Syntax : digitalWrite(digitalPin_number,value)
digitalWrite() function documentation
delay()
Every processor runs on its “clock frequency”, and the instructions are executed in some fixed amount of “clock cycles”, obviously for good processing of complex codes you need a high clock frequency. A delay is a function that makes the processor run in a “wait state” meaning the processor just sits ideally until the time passes that the user has specified, in our case we gave input as 1000 milliseconds, (note that the unitof number passed on to delay() function is in milliseconds).
- So suppose you didn't give a delay ( go ahead scratch both delays off that code and run) you would see that the LED is glowing continuously but at a dim light than before(this will be visible if done on actual hardware)(on tinker cad it will show just glow).
- To understand, think of it that you are switching on and off a fan really fast, before the fan stops you switch on the power the fan gets an impulse, u switch off it still is rotating and u do this loop at a very high speed, the result is, fan keeps on rotating.
Syntax : delay(time in milliseconds) Time has to be an integer
delay() function documentation
digitalRead()
This in-built arduino function reads a value from a GPIO digital pin, either HIGH or LOW. This value may be stored in an integer type variable.
Note that there is a capital R in digitalRead(). Look for other such minute details :)
Syntax : digitalRead(digitalPin_number)
digitalRead() function documentation
Using a Pushbutton to make an LED Blink
int ledPin = 13; // LED connected to digital pin 13
int inputPin = 7; // Pushbutton connected to digital pin 7
int value = 0; // integer type variable to store the read value
void setup()
{
pinMode(ledPin,OUTPUT); // setting the digital pin 13 as output
pinMode(inputPin,INPUT); // setting the digital pin 7 as input
}
void loop()
{
value = digitalRead(inputPin); // read the input pin
digitalWrite(ledPin,value); // sets the LED to the button's value
}
In this video, Paul demonstrates how a push button can be used as a toggle switch.
It's considered good practice to use variables to store values ( like time and ledPin in this case) and use those variables instead of hard-coding all the values manually. If we ever want to change the values, ( which we will be doing a lot while trying out stuff ) we'll only have to change the value of the variable at one place, and not at all the places the variable has been used in the program.
Everything you've been taught till now, has been beautifully explained by Paul McWhorter here. We really recommend you go through this. He also demonstrates how you can get an external LED to blink in TinkerCAD.
If you have any problems with the stuff taught here, or in any other videos, you're more than welcome to put your doubts/suggestions in the #discussion channel of our discord server.
Loops in C
Loops in any programming language help us in executing a group of statements repeatedly. C provides us with the following types of loops to complete such tasks:
for loop
The syntax for setting up a for loop is : for(initialisation; condition; updation_statement)
int i; // declaring an integer type variable
for(i=1; i<=4; i=i+1) // for loop
{
Serial.println("Hello 2020 batch");
}
OUTPUT
Hello 2020 Batch
Hello 2020 Batch
Hello 2020 Batch
Hello 2020 Batch
Here's a link on how for loops work
while loop
The syntax for setting up a while loop is :
initialisation_of_variable;
while(condition)
{
//statements to be executed
updation_statement;
}
Here's a piece of code that does the same thing we did before, using the while loop
int i = 4; // initialising an integer type variable
while(i!=0) // while loop
{
Serial.println("Hello 2020 batch");
i = i - 1; // or i--
}
OUTPUT
Hello 2020 Batch
Hello 2020 Batch
Hello 2020 Batch
Hello 2020 Batch
A link on how while loops work
do while loop
The syntax for setting up a do while loop is :
initialisation_of_variable;
do {
// statements to be executed
updation_statement;
} while(condition);
A piece of code that does the same thing we did before, using the do while loop.
int i = 4; // initialising an integer type variable
do { // while loop
Serial.println("Hello 2020 batch");
i = i - 1; // or i--
} while(i!=0);
OUTPUT
Hello 2020 Batch
Hello 2020 Batch
Hello 2020 Batch
Hello 2020 Batch
A link on how do while loops work
Pay attention to the difference between flow of control in a do while loop and a while loop.
Onshape : 3D
You are expected to watch the following videos, and practice a few questions.
Onshape : 2 : Basic 2D Drawing and Easy 3D Shapes with Extrusion
Onshape : 3 : Complex shapes using Only Extrude