RAI Final Exam PDF
Document Details
Uploaded by PatientVignette
Tags
Summary
This document explains fundamental concepts of Arduino programming, including libraries, variables, arrays, functions, pin configuration, and loops. It also touches on conditional statements and timing and control concepts.
Full Transcript
Common arduino code concepts: 1. Libraries and Includes #include This directive tells the compiler to include a library, providing pre-written functions and classes for specific hardware or functionalities. Libraries reduce the need to write code from scratch. For...
Common arduino code concepts: 1. Libraries and Includes #include This directive tells the compiler to include a library, providing pre-written functions and classes for specific hardware or functionalities. Libraries reduce the need to write code from scratch. For example, libraries like Adafruit_NeoPixel control RGB LED strips, and Wire handles I2C communication. 2. Preprocessor Directives #define NAME VALUE Creates a constant or macro without consuming memory. For instance, #define PIN 6 makes PIN a placeholder for 6 throughout the code. Unlike variables, these values are immutable and resolved at compile-time. 3. Variable Declarations int, float, byte int: Integer data type, typically used for discrete values like pin numbers. float: Floating-point data type, often used for measurements requiring decimals (e.g., sensor readings). byte: Efficiently stores small integers (0–255), suitable for memory-constrained applications like LEDs. 4. Arrays int arrayName[] = {value1, value2,...}; Stores multiple values of the same type in a contiguous memory block. Access values with indices: arrayName[index]. Arrays simplify repetitive tasks, such as controlling multiple LEDs or storing musical tones. 5. Functions Declaration: void functionName(parameters) { code } Encapsulates logic for reuse. The void return type indicates the function does not return a value. Example: void setup() is a required Arduino function, executed once during initialization. 6. Pin Configuration pinMode(pin, mode) Configures a pin as either INPUT, OUTPUT, or INPUT_PULLUP. For example: INPUT: Reads external signals. OUTPUT: Sends signals or controls devices like LEDs. INPUT_PULLUP: Uses internal resistors to prevent floating states on input pins. 7. Conditional Logic if(condition) { code } Executes code if condition evaluates as true. Often used with digitalRead() to respond to button presses or sensor input. Example: if(digitalRead(buttonPin) == LOW) detects when a button is pressed. 8. Loops for and while for (initialization; condition; increment) { code }: Repeats code a specific number of times. Example: Lighting up an LED array sequentially. while(condition) { code }: Executes as long as condition is true. Commonly used for real-time monitoring. 9. Mapping Values map(value, fromLow, fromHigh, toLow, toHigh) Scales a value from one range to another. Example: map(analogRead(A0), 0, 1023, 0, 255) converts a sensor's 10- bit reading (0–1023) to an 8-bit value (0–255). 10. Timing and Delays delay(milliseconds) Pauses program execution for a specified time. Commonly used to control animation or debounce buttons. millis() Returns the time (in milliseconds) since the program started. Enables non-blocking delays for tasks requiring precise timing. 11. PWM and Analog Control analogWrite(pin, value) Writes a simulated analog output (pulse-width modulation) to a pin, with value ranging from 0 to 255. Used for dimming LEDs or controlling motor speed. 12. Communication Serial Monitor Serial.begin(baudRate): Initializes communication with the PC at the specified baudRate (e.g., 9600). Serial.println(value): Sends a value or message to the Serial Monitor for debugging or displaying sensor data. 13. Libraries in Action Object Initialization Example: Adafruit_NeoPixel pixels(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800); Creates an object (pixels) to interact with NeoPixel LEDs, specifying LED count, pin, and data protocol. Calling Methods pixels.begin(): Initializes the NeoPixel object. pixels.show(): Updates the LED colors based on prior settings. projects 6 til 12, their usage,parts required and a brief explanation of what are they used for Project 6: Rainbow Strip Light What It Does: Creates a decorative, ambient light effect using a WS2812B RGB LED strip. The lights transition smoothly through a rainbow of colors. Demonstrates control over addressable LEDs and HSV-to-RGB color manipulation. Usage: Decorative lighting for rooms, events, or furniture. Learning how to control addressable RGB LED strips. Parts Required: Arduino board WS2812B RGB LED strip (32 LEDs, 5V) Breadboard Jumper wires Project 7: NeoPixel Compass What It Does: Displays the direction (compass heading) using a NeoPixel ring and a three-axis HMC5883L magnetic sensor. The LED pointing north lights up, and other LEDs display intermediary directions. Demonstrates sensor data integration and LED visualization. Usage: Navigation aid for small-scale applications. Educational tool to understand magnetometers and directional data. Parts Required: Arduino board Adafruit NeoPixel ring (16 LEDs) HMC5883L three-axis sensor Breadboard 9V battery pack with 6 AA batteries Jumper wires Project 8: Arduino Piano What It Does: Creates a basic piano where each button corresponds to a musical note played on a piezo buzzer. Demonstrates tone generation and button input handling. Usage: Fun educational tool for learning basic music or electronics. Introduction to sound synthesis using Arduino. Parts Required: Arduino board Piezo sounder 8 momentary pushbuttons 8 resistors (1k ohm each) Breadboard Jumper wires Project 9: Audio LED Visualizer What It Does: Uses a 3.5 mm headphone jack to input audio signals and controls LEDs to light up in response to the sound's intensity. Acts as a basic audio visualizer. Usage: Fun and interactive way to visualize music or sound. Demonstrates signal processing with Arduino. Parts Required: Arduino board 9 LEDs (3 each of red, yellow, green) 9 resistors (220 ohms) 3.5 mm headphone jack Breadboard Jumper wires Project 10: Old-School Analog Dial What It Does: Simulates an analog gauge using a servomotor controlled by a photoresistor. The gauge needle (servo) moves in response to light intensity detected by the photoresistor. Usage: Demonstrates vintage analog displays using servos. Educational tool for understanding servomotor control and light-sensing. Parts Required: Arduino board Tower Pro SG90 9g servomotor Photoresistor (LDR) 10k-ohm resistor Breadboard Jumper wires Project 11: Stepper Motor What It Does: Uses a 28BYJ-48 stepper motor to rotate a shaft in small, precise increments. Demonstrates motor control using the ULN2003 driver module. Usage: Useful for applications requiring precise rotational control (e.g., robotic arms, camera pans). Demonstrates stepper motor principles and control. Parts Required: Arduino board 28BYJ-48 stepper motor ULN2003 driver module Breadboard Jumper wires Project 12: Temperature-Controlled Fan What It Does: Automatically adjusts a fan's speed based on temperature measured by an LM35 sensor. Demonstrates analog sensor input and PWM motor control. Usage: Practical application for temperature regulation (e.g., cooling systems). Educational tool for analog sensor integration and motor speed control. Parts Required: Arduino board LM35 temperature sensor 12V mini computer cooling fan 5V single-channel relay module Breadboard Jumper wires tinkercad link for each project: led strip light:https://www.tinkercad.com/things/3MDGT1gCVqv- led-strip-lights-? sharecode=avuFAAai1M9kLDFoX5jy7eoNudKsmdbE9XmLv20FH sg Neo-pixel compass:https://www.tinkercad.com/things/6LZAgXfRsxV -neopixel-ring-24? sharecode=_vYj458nTlGyIyIXexfSf8Zcs98rNwLlza5VgAPl MX4 Arduino piano:https://www.tinkercad.com/things/0LUE3qrw1Lb- arduino-piano Arduino audio led visualization:cannot be created on tinkercad Stepper motor:https://www.tinkercad.com/things/4ryGDIbbMvN- stepper-motor? sharecode=pj7ehmxYQeHoIZAv1CvCwgcBZIcBDAZ4D- LSjMuBh6s Temperature controlled fan:https://www.tinkercad.com/things/1c7R1RZ38AK- copy-of-temperature-controlled-fan Parts Required 1. Arduino board The microcontroller that runs the code and controls components. 2. LEDs (standard, RGB, or NeoPixel) Light-emitting diodes used for visual effects, ranging from single colors to full-spectrum addressable LEDs. 3. Resistors (varied values: 220 ohm, 1k ohm, 10k ohm, etc.) Limit current to protect LEDs, sensors, or other components. 4. Sensors (e.g., photoresistor, HMC5883L, LM35) Components that detect environmental changes like light, magnetic fields, or temperature. 5. Motors (e.g., servomotor, stepper motor, fan) Actuators for moving or rotating parts based on signals from the Arduino. 6. Driver Modules (e.g., ULN2003 for stepper motors) Interface between the Arduino and motors for managing higher currents or voltages. 7. Piezo Sounder A small speaker that produces sound or tones. 8. Pushbuttons Input devices for triggering actions or controlling functionality. 9. Breadboard and Jumper Wires Used for prototyping circuits without soldering, enabling flexible connections. 10.Power Supply (e.g., battery packs, USB) Provides necessary power to the Arduino and connected components.