You are on page 1of 4

Kalau tidak ada pesan error

maka modul ultrasonic


akan langsung bekerja
mengukur jarak benda di
depannya. Gunakan Serial
Monitor (Ctrl+Shift+M)
pada software Arduino
untuk melihat hasil
pengukuran.
/*
*
HCSR04Ultrasonic/examples/
UltrasonicDemo/UltrasonicD
emo.pde
*
* SVN Keywords
*
--------------------------------* $Author: cnobile $
* $Date: 2011-09-17
02:43:12 -0400 (Sat, 17
Sep 2011) $
* $Revision: 29 $
*
--------------------------------*/
#include <Ultrasonic.h>
#define TRIGGER_PIN 12
#define ECHO_PIN
13
Ultrasonic
ultrasonic(TRIGGER_PIN,
ECHO_PIN);
void setup()
{
Serial.begin(9600);
}
void loop()
{
float cmMsec, inMsec;
long microsec =
ultrasonic.timing();
cmMsec =
ultrasonic.convert(microse
c, Ultrasonic::CM);

inMsec =
ultrasonic.convert(microse
c, Ultrasonic::IN);
Serial.print("MS: ");
Serial.print(microsec);
Serial.print(", CM: ");
Serial.print(cmMsec);
Serial.print(", IN: ");
Serial.println(inMsec);
delay(1000);
}

Sama halnya pada


penggunaan HC-SR04,
kalau tidak ada pesan error
maka modul ultrasonic
akan langsung bekerja
mengukur jarak benda di
depannya. Gunakan Serial
Monitor (Ctrl+Shift+M)
pada software Arduino
untuk melihat hasil
pengukuran.
#define SDM_IO_TIMEOUT
1000
int TrigPin = 12;
int EchoPin = 13;
unsigned long
ultrasoundDuration;
int timeout;
unsigned long tStartPing =
0;
int sensorValue = 0;
void setup() {
Serial.begin(9600);
pinMode(TrigPin,
OUTPUT); //pin is output
pinMode(EchoPin, INPUT);
// pin is now input
}
void loop() {
sensorValue =
read_sdm_io_range();
Serial.print(sensorValue);
Serial.println(" cm");
delay(100);
}
//SDM-IO Ultrasonic Range

Sensor distance function


float read_sdm_io_range()
{
unsigned char pin = 0;
unsigned int time_flag =
0;
digitalWrite(TrigPin,
HIGH);
delayMicroseconds(2);
digitalWrite(TrigPin,
LOW);
delayMicroseconds(10);
digitalWrite(TrigPin,
HIGH);
tStartPing = micros();
timeout = 0;
pin =
digitalRead(EchoPin);
while(pin)
{
pin =
digitalRead(EchoPin);
time_flag++;
if(time_flag >
SDM_IO_TIMEOUT)
{
timeout = 1;
break;
}
}
ultrasoundDuration =
micros() - tStartPing;
Serial.print(ultrasoundDur
ation);
Serial.print(" us, ");
Serial.print(ultrasoundDur
ation*0.017, DEC); //
result in cm
Serial.print(" cm");
Serial.println();
if (timeout)
return 999;
else
return

ultrasoundDuration *
0.017; // result in cm
}

/*
Program aplikasi pengukur suhu
Arduino LM35
Loki Lang
*/
#include <LiquidCrystal.h>
LiquidCrystal lang(12, 11, 5, 4, 3,
2);
int lm35 = A0;
int val = 0;
float temp;
void setup()
{
pinMode(A0, INPUT);
lang.begin(16, 2);
lang.setCursor(0, 0);
lang.print("Arduino");
lang.setCursor(0, 1);
lang.print("Pengukur Suhu");
delay(1000);
lang.clear();
}
void loop()
{
val = analogRead(lm35);
delay(100);
lang.setCursor(0, 0);
lang.print("Suhu celsius:");
lang.setCursor(0, 1);
temp = val * 5;
temp *= 100;
temp /= 1024;
lang.print(temp);
}

#include <Wire.h>
#include <LCD.h>
#include
<LiquidCrystal_I2C.h>
#define I2C_ADDR 0x3F //
<<----- Add your address
here. Find it from I2C
Scanner
#define BACKLIGHT_PIN
3

#define En_pin
#define Rw_pin
#define Rs_pin
#define D4_pin
#define D5_pin
#define D6_pin
#define D7_pin

2
1
0
4
5
6
7

int n = 1;
LiquidCrystal_I2C
lcd(I2C_ADDR,En_pin,Rw_p
in,Rs_pin,D4_pin,D5_pin,D6
_pin,D7_pin);
void setup()
{
lcd.begin (16,2); // <<----My LCD was 16x2
// Switch on the backlight
lcd.setBacklightPin(BACKLI
GHT_PIN,POSITIVE);
lcd.setBacklight(HIGH);
lcd.home (); // go home
lcd.print("SainSmartI2C16x2"
);
}
void loop()
{
// Backlight on/off every 3
seconds
lcd.setCursor (0,1);
// go
to start of 2nd line
lcd.print(n++,DEC);
lcd.setBacklight(LOW);
//
Backlight off
delay(3000);
lcd.setBacklight(HIGH); //
Backlight on
delay(3000);
}

Reset
/*External Reset Button
Circuit:
-One side of Push button
attached to RESET pin, and
to 5 volts through a 10K
resistor
-Other side connected to
one of the GND pins
Created MAY 2014
By Michael James
http://opensourcehardwareg
roup.com
This code is in the public
domain
*/
void setup(){
//begin Serial
Communications
Serial.begin(9600);
}//close setup
void loop(){
//capture the time the
board has been running
int time =
millis()/1000;
//print the time from
last reset
Serial.print("I was
reset ");
Serial.print(time);
Serial.println(" seconds
ago.");
Serial.println("Will you
stop Dave. I am afraid.");
delay(1000);
}//close loop

Bermain dengan switching

Read Two Switches With One

center-off slide or toggle switch

* +5 V

I/O Pin

where the states are mutually

*|

There are handy 20K pullup

exclusive.

*\

resistors (resistors connected

*/

internally between Arduino I/O

* \ 10K

pins and VCC - +5 volts in the

/*

*/

Arduino's case) built into the

*\

Atmega chip upon which

Read_Two_Switches_On_One_

*|

Freeduino's are based. They are

Pin

* / switch 1 or 1/2 of center-off

accessible from software by

* Read two pushbutton switches

toggle or slide switch

using the digitalWrite()

or one center-off toggle switch

*/

function, when the pin is set to

with one Arduino pin

*|

an input. This sketch exploits

* Paul Badger 2008

* digital pin

the pullup resistors under

* From an idea in EDN

________+_____________/\/\/\

software control. The idea is

(Electronic Design News)

____________ ground

that an external 200K resistor to

*|

ground will cause an input pin

* Exploits the pullup resistors

* | 200K to 1M (not critical)

to report LOW when the

available on each I/O and

*/

internal (20K) pullup resistor is

analog pin

* / switch 2 or 1/2 of center-off

turned off. When the internal

* The idea is that the 200K

toggle or slide switch

pullup resistor is turned on

resistor to ground will cause the

*|

however, it will overwhelm the

input pin to report LOW when

*|

external 200K resistor and the

the

* _____

pin will report HIGH. One

* (20K) pullup resistor is turned

* ___ ground

downside of the scheme (there

off, but when the pullup resistor

*_

always has to be a downside

is turned on,

doesn't there?) is that one can't

* it will overwhelm the 200K

*/

tell if both buttons are

resistor and the pin will report

#define swPin 2 // pin for input

pushed at the same time. In this

HIGH.

- note: no semicolon after

case the scheme just reports that

#define

sw2 is pushed. The job of the

* Schematic Diagram ( can't

int stateA, stateB; // variables to

10K series resistor, incidentally,

belive I drew this funky ascii

store pin states

is to prevent a short circuit if a

schematic )

int sw1, sw2; // variables to

pesky user pushes both buttons

represent switch states

at once. It can be omitted on a

void setup()

){ // both states HIGH - switch

sw1 = 0; // no switches pushed -

Serial.begin(9600);

1 must be pushed

or center-off toggle in middle

sw1 = 1;

position

void loop()

sw2 = 0;

sw2 = 0;

digitalWrite(swPin, LOW); //

else if ( stateA == 0 && stateB

Serial.print(sw1);

make sure the puillup resistors

== 0 ){ // both states LOW -

Serial.print(" "); // pad some

are off

switch 2 must be pushed

spaces to format print output

stateA = digitalRead(swPin);

sw1 = 0;

Serial.println(sw2);

digitalWrite(swPin, HIGH); //

sw2 = 1;

delay(100);

turn on the puillup resistors

stateB = digitalRead(swPin);

else{ // stateA HIGH and stateB

if ( stateA == 1 && stateB == 1

LOW