Antarmuka Sensor Arduino

14.45 Posted by Ali Mahfud , No comments
Antarmuka Sensor Pada Arduino

1. LDR Light Sensor

Source Code:
/*
LDR blinking rate sketch
Blink a LED with a rate based on light intensity upon an LDR
*/
const int ledPin = 2; // LED connected to digital pin 2
const int ldrPin = 0; // connect LDR to analog input 0
void setup()
{
  pinMode(ledPin, OUTPUT); // enable output on the led pin
  Serial.begin(9600);
}
void loop()
{
  int rate = analogRead(ldrPin); // read the analog input
  Serial.println(rate);
  digitalWrite(ledPin, HIGH); // set the LED on
  delay(rate); // wait duration dependent on light level
  digitalWrite(ledPin, LOW); // set the LED off
  delay(rate);
}

2. LM35 Temperature Sensor

Source Code:
/*
LM35 sketch
Prints the temperature to the Serial Monitor
*/
const int lm35Pin = 0; // LM35 connected to analog input pin 0
void setup()
{
  Serial.begin(9600);
}
void loop()
{
  int tempVal = analogRead(lm35Pin);
  Serial.print(tempVal); Serial.print(" > ");
  float millivolts = (tempVal / 1024.0) * 5000;
  float celsius = millivolts / 10; // sensor output is 10mV / Celsius
  Serial.print(celsius);
  Serial.print(" degrees Celsius, ");
  Serial.print( (celsius * 9)/ 5 + 32 ); // converts to fahrenheit
  Serial.println(" degrees Fahrenheit");
  delay(1000); // wait for one second
}

3. Dimmer

Source Code:
/*
LDR dimmer sketch
Dim a LED based on light intensity upon an LDR
*/
const int ledPin = 2; // LED connected to digital pin 2
const int ldrPin = 0; // connect LDR to analog input 0
void setup()
{
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);
}
void loop()
{
  int ldrVal = analogRead(ldrPin);
  Serial.println(ldrVal);
  delay(200);
  ldrVal = constrain(ldrVal, 20,150);
  int ledLevel = map(ldrVal,20,150, 255,0);
  analogWrite (ledPin, ledLevel);
}

4. Temperature Indicator

Source Code:
/*
LM35 sketch
Turn on a LED if temperature is greater than a threshold
*/
const int lm35Pin = 0; // sensor connected to this analog pin
const int ledPin = 2; // digital output pin for LED
const int threshold = 25; // the degree that will turn on the LED
void setup()
{
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);
}
void loop()
{
  int tempVal = analogRead(lm35Pin);
  long celsius = (tempVal * 500L) /1024; // 10 mV per degree celcius
  Serial.print(celsius);
  Serial.print(" degrees Celsius: ");
  if(celsius > threshold)
  {
    digitalWrite(ledPin, HIGH); // LED on
    Serial.println("LED is ON");
  }
  else
  {
    digitalWrite(ledPin, LOW);
    Serial.println("LED is OFF"); // LED off
  }
  delay(1000); // wait for one second
}

5. PIR Motion Detector

Source Code:
/*
PIR sketch
Turn on a LED if motion is detected
*/
const int ledPin = 9; // choose the pin for the LED
const int pirPin = 2; // choose the input pin for the PIR sensor
void setup() 
{
  pinMode(ledPin, OUTPUT); // declare LED as output
  pinMode(pirPin, INPUT); // declare PIR as input
}
void loop() 
{
  int pirVal = digitalRead(pirPin); // read input value
  if (pirVal == HIGH) // check if the input is HIGH
  {
    digitalWrite(ledPin, HIGH); // turn LED on if motion is detected
    delay(500);
    digitalWrite(ledPin, LOW); // turn LED off
  }
}

6. IR Distance Sensor

Source Code:
/*
IR-distance sketch
Display distance of object from Sharp Distance Sensor 2Y0A02
(on Serial Monitor)
*/
const int irPin = 0; // choose the pin for the IR distance sensor
float irVal, cm; //Must be of type float for pow()
void setup()
{
  Serial.begin(9600);
}
void loop()
{
  irVal = analogRead(irPin);
  //inches = 4192.936 * pow(sensorValue,-0.935) - 3.937;
  cm = 10650.08 * pow(irVal,-0.935) - 10;
  delay(100);
  Serial.print("Distance (cm) : ");
  Serial.println(cm);
}

Antarmuka Switch Arduino

13.34 Posted by Ali Mahfud , No comments
Antarmuka Switch Pada Arduino

1. Push Button

Source Code:
int ledPin = 13; // choose the pin for the LED
int switchPin = 2; // choose the input pin (for a pushbutton
int val = 0; // variable for reading the pin status
void setup() 
{
  pinMode(ledPin, OUTPUT); // declare LED as output
  pinMode(switchPin, INPUT); // declare pushbutton as input
}
void loop()
{
  val = digitalRead(switchPin); // read input value
  if (val == LOW) { // check if the input is LOW
  digitalWrite(ledPin, LOW); // turn LED OFF:
}
else 
{
  digitalWrite(ledPin, HIGH); // turn LED ON:
}
}

2. Swtich to Blink or Fade
Source Code:
/*
Blink or fade switch
Choose between Blink or Fade the LED connected to digital
pin 9, when pressing a pushbutton attached to pin 2.
The circuit:
* LED attached from pin 9 to ground
* pushbutton attached to pin 2 from +5V
* 10K resistor attached to pin 2 from ground
*/
int ledPin = 9; // choose the pin for the LED
int switchPin = 2; // choose the input pin (for a pushbutton
int val = 0; // variable for reading the pin status
int fadeval = 0;
void setup()
{
  pinMode(ledPin, OUTPUT); // declare LED as output
  pinMode(switchPin, INPUT); // declare pushbutton as input
}
void loop()
{
  val = digitalRead(switchPin); // read input value
  if (val == HIGH) { // pushed button means do blinking
    digitalWrite(ledPin, LOW); // turn LED OFF:
    delay(50);
    digitalWrite(ledPin,HIGH); //turn LED ON:
    delay(50);
  }
  else
  { // else button isn't pressed so do fading
    for(fadeval = 0 ; fadeval <= 255 ; fadeval +=5) { // fade in
    analogWrite(ledPin, fadeval); // sets the value
    delay(10);
    }
    for(fadeval = 255; fadeval >= 0 ; fadeval -=5) { // fade out
    analogWrite(ledPin, fadeval);
    }
  }
}
3. Toggle
Source Code:
// Toggle switch
/* The circuit:
* LED attached from pin 13 to ground
* pushbutton attached to pin 8 from +5V
* 10K resistor attached to pin 8 from ground
*/
int switchPin = 8;
int ledPin = 13;
boolean lastButton = LOW;
boolean ledOn = false;
void setup()
{
  pinMode(switchPin, INPUT);
  pinMode(ledPin, OUTPUT);
}
void loop()
{
  if (digitalRead(switchPin) == HIGH && lastButton == LOW)
  {
    ledOn = !ledOn;
    lastButton = HIGH;
  }
  else
  {
    lastButton = digitalRead(switchPin);
  }
  digitalWrite(ledPin, ledOn);
}

4. PWM Switch

Source Code:
// PWM Switch (with debounce)
int switchPin = 8;
int ledPin = 3;
boolean lastButton = LOW;
boolean currentButton = LOW;
int ledLevel = 0;
void setup()
{
  pinMode(switchPin, INPUT);
  pinMode(ledPin, OUTPUT);
}
boolean debounce(boolean last)
{
  boolean current = digitalRead(switchPin);
  if (last != current)
{
  delay(5);
  current = digitalRead(switchPin);
}
return current;
}
void loop()
{
  currentButton = debounce(lastButton);
  if (lastButton == LOW && currentButton == HIGH)
  {
    ledLevel = ledLevel + 51;
  }
  lastButton = currentButton;
  if (ledLevel > 255) ledLevel = 0;
  analogWrite(ledPin, ledLevel);
}

5. Interactive Traffic Lights

Source Code:
// Interactive Traffic Lights
int carRed = 12; // assign the car lights
int carYellow = 11;
int carGreen = 10;
int pedRed = 9; // assign the pedestrian lights
int pedGreen = 8;
int button = 2; // button pin
int crossTime = 5000; // time allowed to cross
unsigned long changeTime; // time since button pressed
void setup() 
{
  pinMode(carRed, OUTPUT);
  pinMode(carYellow, OUTPUT);
  pinMode(carGreen, OUTPUT);
  pinMode(pedRed, OUTPUT);
  pinMode(pedGreen, OUTPUT);
  pinMode(button, INPUT); // button on pin 2
  digitalWrite(carGreen, HIGH); // turn on the green light
  digitalWrite(pedRed, HIGH);
}
void loop() 
{
  int state = digitalRead(button);
  // check if button is pressed and it is over 5s since last button press
  if (state == HIGH && (millis() - changeTime) > 5000) {
    changeLights(); // Call the function to change the lights
  }
}
void changeLights()
{
  digitalWrite(carGreen, LOW); // green off
  digitalWrite(carYellow, HIGH); // yellow on
  delay(2000); // wait 2 seconds
  digitalWrite(carYellow, LOW); // yellow off
  digitalWrite(carRed, HIGH); // red on
  delay(1000); // wait 1 second till its safe
  digitalWrite(pedRed, LOW); // ped red off
  digitalWrite(pedGreen, HIGH); // ped green on
  delay(crossTime); // wait for preset time period
                    // flash the ped green
  for (int x=0; x<10; x++)
  {
    digitalWrite(pedGreen, HIGH);
    delay(250);
    digitalWrite(pedGreen, LOW);
    delay(250);
  }
  // turn ped red on
  digitalWrite(pedRed, HIGH);
  delay(500);
  digitalWrite(carYellow, HIGH); // yellow on
  digitalWrite(carRed, LOW); // red off
  delay(1000);
  digitalWrite(carGreen, HIGH);
  digitalWrite(carYellow, LOW); // yellow off
  changeTime = millis(); // record the time since last change of
                        // lights
}