Introduction To Arduino
Introduction To Arduino
What is ARDUINO ??
Arduino is an open-source electronics prototyping platform. Based on flexible, easy-to-use hardware and software.
It's intended for artists, designers, hobbyists, and anyone interested in creating interactive objects or environments
ARE You excited to start coding and checkout that little hardware (Arduino)
Basic Syntax
Every Arduino code has the following format void setup(){...} void loop(){...}
The setup()
function is called when a sketch starts. Use it to initialize variables, pin modes, start using libraries, etc. The setup function will only run once, after each power up or reset of the Arduino board
loop () :
After creating a setup() function, which initializes and sets the initial values the loop() function does precisely what its name suggests, and loops consecutively, allowing your program to change and respond. Use it to actively control the Arduino board.
Outline of function
Digital I/O : To control digital Input and output pins
pinMode() digitalWrite() digitalRead()
Syntax pinMode(pin, mode) Pin - the number of the pin whose mode you wish to set Mode - either INPUT or OUTPUT Returns: None
Syntax digitalWrite(pin, value) Pin - the pin number Value - HIGH or LOW Returns: none
Syntax digitalRead(pin) Pin - the number of the digital pin you want to read (int) Returns: HIGH or LOW
Pins 3, 5, 6, 9, 10, and 11 can be used as PWM. You do not need to call pinMode() to set the pin as an output before calling analogWrite().
Syntax analogWrite(pin, value) pin: the pin intended to write value: the duty cycle: between 0 (always off) and 255 (always on) Returns: None
SERIAL COMMUNICATION
Used for communication between the Arduino board and a computer or other devices.
All Arduino boards have at least one serial port (also known as a UART or USART). It communicates on digital pins 0 (RX) and 1 (TX) as well as with the computer via USB.
Thus, if you use these functions, you cannot also use pins 0 and 1 for digital input or output.
SERIAL FUNCTIONS
Serial.begin(baud_rate) Possible choices of Baud rate are: 300, 1200, 2400, 4800, 9600, 14400, 19200, 28800, 38400, 57600, or 115200
Serial.print(val) Val is the value to be printed. Any data type Serial.read() Does not take any parameters. Returns the first byte of incoming serial data available (or -1 if no data is available).
Delay functions
delay() : Pauses the program for the amount of time (in milliseconds) specified as parameter delayMicroseconds() : Pauses the program for the amount of time (in microseconds) specified as parameter
SOLUTION
// this constant won't change: const int buttonPin = 2; // the pin that the pushbutton is attached to const int ledPin = 13; // the pin that the LED is attached to // Variables will change: int buttonState = 0; // current state of the button void setup() { pinMode(buttonPin, INPUT); pinMode(ledPin, OUTPUT); Serial.begin(9600); } void loop() { buttonState = digitalRead(buttonPin); // compare the buttonState to its previous state if (buttonState == HIGH) { digitalWrite(ledPin, HIGH); Serial.println("on"); } else { digitalWrite(ledPin, LOW); Serial.println("off"); } }