combine 1.pdf
Document Details
Uploaded by Deleted User
Tags
Full Transcript
BASIC C++ COMMANDS/FUNCTIONS ARDUINO Creative Technology 9 VO ID S E T U P( ) The setup() function is a special function in Arduino sketches (programs) that runs once when the microcontroller (like an Arduino board) starts up or resets. It’s where you set up...
BASIC C++ COMMANDS/FUNCTIONS ARDUINO Creative Technology 9 VO ID S E T U P( ) The setup() function is a special function in Arduino sketches (programs) that runs once when the microcontroller (like an Arduino board) starts up or resets. It’s where you set up initial configurations, pin modes, and any other one-time tasks before the main loop begins. VO ID S E T U P( ) Initialization: You typically use setup() to configure pins, serial communication, timers, and other hardware-related settings. Runs Once: Unlike the loop() function (which runs repeatedly), setup() executes only once during startup. VO ID S E T U P( ) void setup() { // Initialize pin modes pinMode(ledPin, OUTPUT); pinMode(buttonPin, INPUT); } VO ID LO O P The loop() function is a crucial part of an Arduino sketch (program). It runs continuously after the setup() function completes. Any code inside loop() executes repeatedly until the microcontroller is powered off or reset. VO ID LO O P Main Execution: You place your primary code logic inside loop(). Continuous Operation: Whatever you write in loop() will keep running indefinitely. VO ID LO O P void loop() { // Your main code here digitalWrite(ledPin, HIGH); // Turns on an LED delay(1000); // Wait for 1 second digitalWrite(ledPin, LOW); // Turns off the LED delay(1000); // Wait again } PINMODE() The pinMode() function is used to configure the mode of a digital pin on an Arduino board. It determines whether the pin will be used for input or output. PINMODE() Syntax: pinMode(pin numbers, mode) pin: The number of the pin you want to configure (e.g., 13, A0, etc.). mode: Either INPUT or OUTPUT. OUTPUT/INPUT OUTPUT is for pins that provide voltage (e.g., to control LEDs, motors, or relays). INPUT is for pins that read external signals (e.g., from buttons, sensors, or switches). OUTPUT/INPUT When a pin is set to “OUTPUT,” it becomes capable of providing voltage (either HIGH or LOW) to external components. In the case of an LED connected to ledPin, setting it to “OUTPUT” allows you to turn the LED on (HIGH voltage) or off (LOW voltage). OUTPUT/INPUT When a pin is set to “OUTPUT,” it becomes capable of providing voltage (either HIGH or LOW) to external components. In the case of an LED connected to ledPin, setting it to “OUTPUT” allows you to turn the LED on (HIGH voltage) or off (LOW voltage). You can use digitalWrite(ledPin, HIGH); to turn the LED on and digitalWrite(ledPin, LOW); to turn it off. OUTPUT/INPUT When a pin is set to “INPUT,” it becomes capable of reading external signals (e.g., from a button, sensor, or switch). In the case of a button connected to buttonPin, setting it to “INPUT” allows you to detect whether the button is pressed (HIGH voltage) or not (LOW voltage). You can use digitalRead(buttonPin) to read the button state (HIGH or LOW). DELAY The delay() function in Arduino programming serves a straightforward purpose: it introduces a pause or delay in the execution of your code. DELAY delay(ms) pauses the program for a specified time in milliseconds (ms). During this delay, the microcontroller (like an Arduino) does nothing except wait. VA R I A B L E I N T A N D C O N S T An int is a data type used to represent whole numbers (both positive and negative) in most programming languages. It stands for “integer.” Commonly used for counting, indexing, and arithmetic operations. VA R I A B L E I N T A N D C O N S T VA R I A B L E I N T A N D C O N S T The const keyword is used to declare a constant—a value that cannot be changed after it’s assigned. Constants are helpful for ensuring that certain values remain fixed throughout your program. VA R I A B L E I N T A N D C O N S T FOR() LOOP A for loop is a control structure that allows you to repeat a block of code a specific number of times. It’s particularly useful when you know in advance how many iterations (repetitions) you need. FOR() LOOP A typical for loop consists of three parts: Initialization: Setting an initial value (usually a counter variable). Condition: Checking if the loop should continue (based on the condition). Update: Modifying the counter variable after each iteration. The loop continues until the condition evaluates to false. FOR() LOOP Syntax: for(initialization, condition, update){ code to be excuted} FOR() LOOP int i = 1 initializes the counter variable i. i