Showing posts with label Arduino IDE. Show all posts
Showing posts with label Arduino IDE. Show all posts

Functions in Arduino IDE

How function works in Arduino IDE?
  • The functions enable a programmer to partition a big program into many small-small programs.
  • Each of the program performs a different task. 
  • The functions are designed to repeat a task again and again in a programme.
  • A function is a method that returns the code area from where it was called.
Some basic Benefits of Using Functions:

  • It improves the code's readability.
  • It is in charge of conceptualizing and organizing the programme.
  • It lowers the possibilities of making a mistake.
  • It reduces the program's size and complexity.
  • It prevents the same collection of statements or codes from being repeated.
  • It helps us to break down a complicated code or programme into smaller chunks.
  • The use of functions in a software simplifies the modification process.
Parts of Function in arduino.
  • Setup() and loop() are two popular Arduino procedures that are called automatically in the background. 
  • Within these functions, the code to be performed is written inside the curly brackets.
  • However, there are situations when we need to develop our own functions. Lets discuss.
How to declare Function?

Functions in Arduino
Function Declaration
  1. Function return type.
  2. Function name.
  3. Function parameters.
Function return type.
  • A function requires a return type. For example, the return value of a function can be stored as a variable.
  • As a return type, we can use any data type, such as float, char, and so on.
Function name.
  • It is made up of a name that is given to the function. It represents the function's actual body.
Function parameters.
  • The parameters provided to the function are included in function. The parameters are special variables that are used to pass information to a function.
  • Parentheses () and a semicolon must be placed after the function.
  • An argument is the data that is supplied to the function.
Example 1: Let's take an example of addition two numbers.

void setup()  
{  
  Serial.begin(9600);  
}  
void loop() {  
  int a = 5; // initialization of values to the variables a and b . 
  int b = 4;  
  int c;  
  c = myfunc(a, b); // c will now contains the value 9.  
  Serial.println(c); // to print the resulted value.  
  delay(1000); // time delay of 1 second or 1000 milliseconds.  
}  
int myfunc(int i, int j)   
{  
  int sum;  
  sum = i + j;  
  return sum;  
}  

Example 1: Let's take an example to find given number is even or odd.

int a= 0;  
int b;  
void setup()  
{  
  Serial.begin(9600);  
}  
void loop()   
{  
b = myfunc(a); // we can store the function return value in variable b  
  Serial.print(a);  
  Serial.print(" : "); // to separate even or odd text  
  if (b==1)  
  {  
    Serial.println( " Number is even");  
  }  
    else  
    {  
      Serial.println("Number is odd");  
    }                         
      a++; // the function will increment and will again run  
                     delay(1000);  
}                   
 int myfunc(int d)  
                       {  
                       if (d% 2==0)  
                       {  
                         return 1;  
                       }  
                       else  
                       {  
                         return 0;  
                       }  
                     }  
Expected Output:
Even odd



Arduino IDE

What is Arduino IDE?

  • The Arduino IDE is an open-source program for writing and uploading code to Arduino boards. The IDE program is compatible with a variety of operating systems, including Windows, Mac OS X, and Linux. 
  • C and C++ are supported programming languages. IDE stands for Integrated Development Environment in this case.
  • The program or code written in the Arduino IDE is often called as sketching.
  • To upload the sketch written in the Arduino IDE software, we must connect the Genuino and Arduino boards to the IDE. The '.ino' extension is used to save the drawing.
Arduino IDE

Button on the Toolbar
  • New, Open, Save, Upload, and Verify are the icons on the toolbar.
  • It is as follows:
Toolbar

Upload Button.
  • The Upload button compiles and executes the code that we wrote on the screen. The code is then uploaded to the linked board. We must first verify that the right board and ports are selected before uploading the drawing.
  • A USB connection is also required to connect the board to the PC. After you've completed all of the preceding steps, click the Upload button on the toolbar.
  • Before starting Upload, the newest Arduino boards can be automatically reset. On older boards, we must push the Reset button to restart them. The Tx and Rx LEDs will glow as soon as the uploading is completed successfully.
  • If the uploading fails, the notification will appear in the error window.
  • Using the Arduino Bootloader, we don't need any additional hardware to upload our code.
Serial Monitor.
  • The serial monitor button is located in the toolbar's right corner. It displays the serial monitor on the screen.
Arduino Kit
  • The Arduino Kit includes all of the parts needed to develop digital or electronic devices. The Arduino boards can also be purchased as DIY kits. 
  • DIY stands for Do It Yourself in this context. The DIY kits are designed to be used in the classroom and as a practice tool for students. 
  • Such kits can also be used by non-engineering students for their projects.
Arduino Kit
  • With the whole kit, we can quickly get started on our electronics projects. It also aids us in the development of hands-on and interesting initiatives. 
  • The kit aids with the study of programming principles like as voltage, current, and digital logic. 
  • The use of actuators and sensors in the projects helps to understand the notion of digital and analogue signals.
  • The Spaceship Interface (creating the control panel for a spaceship), Keyboard Instrument, Knock Lock (a secret code used to access the door), and other Arduino projects are examples.
The following is a list of the components included in the Starter kit:
  1. Arduino UNO board.
  2. Breadboard.
  3. LED (Bright White, Green, Red, Yellow, Blue, and RGB).
  4. LCD Alphanumeric.
  5. Wooden base that can be easily assembled.
  6. Solid core jump wires.
  7. Stranded Jump wires of RED and BLACK color.
  8. 9V Battery.
  9. Resistors of 220 Ohms, 560 Ohms, 1kOhms, 4.7kOhms, 10kOhms, 1MOhms, and 10MOhms.
  10. Small DC Motor of 6/9V.
  11. (40 x 1) Male Strip pins.
  12. Red, Blue, and Green Transparent Gels.
  13. Diodes.
  14. The Capacitors of 100uF.
  15. Optocouplers.
  16. Small servo meter.
  17. Piezo Capsule.
  18. Push Buttons.
  19. Tilt Sensor.
  20. Potentiometer.
  21. Phototransistor.
  22. Temperature Sensor.
  23. MOSFET Transistors.
  24. H-bridge Motor Driver.
  25. USB Cable

Arduino IDE
Arduino key points