0% found this document useful (0 votes)
7 views32 pages

4 Arduino Programming

The document discusses the main components of an Arduino sketch including variable declaration, setup function, and main loop function. It provides examples of using digital and analog input/output, interrupts, serial communication, and other basic Arduino programming concepts. Key topics covered include reading button/switch states, analog to digital conversion, pulse width modulation for analog output, interrupt handling, and serial communication functions.

Uploaded by

ncqphuong.20it2
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
7 views32 pages

4 Arduino Programming

The document discusses the main components of an Arduino sketch including variable declaration, setup function, and main loop function. It provides examples of using digital and analog input/output, interrupts, serial communication, and other basic Arduino programming concepts. Key topics covered include reading button/switch states, analog to digital conversion, pulse width modulation for analog output, interrupt handling, and serial communication functions.

Uploaded by

ncqphuong.20it2
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 32

ĐẠI HỌC ĐÀ NẴNG

Arduino Programming

Nguyễn Văn Thọ


The Sketch
Three main parts:

• Variable declaration

• Setup function

• Main Loop function.


Variable Declaration
Variable Declaration
• The most common that we use is an integer (int).

• Other variable types are used in later examples (i.e., float, long,
unsigned int, char, byte, etc.) a

• Can rename an Arduino input/output pin number with any name.

• int ledpin = 13;


The Setup Function
• This function runs once, each time the Arduino is powered on

.
The Loop Function
• This function is where the main code is placed and will run over and
over again continuously until the Arduino is powered off
Signals
• Digital signals : The Arduino Uno/Diecimila/Duemilanove has 14 digital
input/output pins labeled D0-D13
• Hight (1) : 5V

• Low (0) : 0V

• Analog signals
Digital Signals
• Input : value = digitalRead(pin)

• Output : digitalWrite(pin)

• digitalWrite(pin)

• This function is where the main code is placed and will run over and
over again continuously until the Arduino is powered off
1-9
Digital Signals : Example
Breadboard
1-12
Digital Signals
Button : PullUp and PullDown

HIGH  LOW LOW  HIGH


Button/Swicht Debounce

- Pushbuttons often generate spurious open/close


transitions when pressed.
- Each bounce could be interpreted by the Arduino as a
button press.
- Without debouncing, pressing the button once may cause
unpredictable results
- Debouncing : checking twice in a short period of time to
make sure the pushbutton is definitely pressed
Hardware Switch Debouncing : RC Filter
Software Switch Debouncing

#define Button 2
int Counter=0;
int buttonState;
int lastButtonState = HIGHnhấn.
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 50;

void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
pinMode(Button, INPUT); // digitalWrite(Button,HIGH);
}
Software Switch Debouncing
void loop() {
int reading = digitalRead(Button);
if (reading != lastButtonState) {
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
if (reading != buttonState) {
buttonState = reading;
if (buttonState == LOW) {
Counter++;
Serial.println(Counter);
}
}
}
lastButtonState = reading;
}
Analog Input
• The input is looking for a voltage level between 0-5vdc and will scale
that voltage into a 10-bit value, or from 0-1023.

• To read an analog pin, you must use the analogRead() command


Analog Input

int pot_val; // use variable "pot_val" to store the value of the potentiometer
void setup() {
Serial.begin(9600); // start Arduino serial communication at 9600 bps
}
void loop() {
pot_value = analogRead(0); // use analogRead on analog pin 0
Serial.println(pot_val); // use the Serial.print() command to send the value
to the monitor
}
Analog output : PMW
• PMW(Pulse Width
Modulation) is a technique
for getting analog results
with digital means

• The signal is switched


between on and off to
generate a square wave

• The duration of "on time"


is called the pulse width.
PMW pins
Analog output : Example
Analog output : PMW
const int analogInPin = A0;
const int analogOutPin = 9;
int sensorValue = 0; // value read from the pot
int outputValue = 0; // value output to the PWM (analog out)
void setup()
{
Serial.begin(9600);
}
void loop()
{
sensorValue = analogRead(analogInPin); // read the analog in value
outputValue = map(sensorValue, 0, 1023, 0, 255);
analogWrite(analogOutPin, outputValue); // change the analog out value
Serial.print("sensor = "); Serial.print(sensorValue);
Serial.print("\t output = "); Serial.println(outputValue);
delay(2);
}
Interrupts Vs Polling
Process the interrupt
Arduino Interrupts
Arduino Interrupts
• attachInterrupt(digitalPinToInterrupt(pin), ISR, mode)
• detachInterrupt(digitalPinToInterrupt(pin))
• interrupts()
• nointerrupts()

pin: the Arduino pin number.


ISR: the ISR to call when the interrupt occurs;
mode: defines when the interrupt should be triggered. Four
constants are predefined as valid values:
LOW to trigger the interrupt whenever the pin is low,
CHANGE to trigger the interrupt whenever the pin changes value
RISING to trigger when the pin goes from low to high,
FALLING for when the pin goes from high to low.
Arduino Interrupts
Example
const byte ledPin = 13;
const byte interruptPin = 2;
volatile byte state = LOW;
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(interruptPin, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(interruptPin), blink, CHANGE);
}
void loop() {
digitalWrite(ledPin, state);
}
void blink() {
state = !state;
}
Serial Communication
Serial communication is the most widely used approach to
transfer information between data processing equipment and
peripherals.
The transmission modes are classified as Simplex, Half Duplex,
and Full Duplex
Serial Communication
Serial.begin(baudrate);
Serial.print(message);
Serial.println(message);
Serial.available()
Serial.read()

You might also like