#define BUTTON_PIN 2 void setup() { ... pinMode(BUTTON_PIN, INPUT); digitalWrite(BUTTON_PIN, HIGH); // connect internal pull-up ... } void loop() { ... }
Tuesday, August 25, 2015
internal pull-up resistor
arduino RF transmitter / receiver
download VirtualWire.h
https://www.pjrc.com/teensy/td_libs_VirtualWire.html
// RX code
// Include VirtualWire library
#include <VirtualWire.h>
// Pins definition
const int led_pin = 13;
const int receive_pin = 12;
int pinSpeaker = 10;
void setup()
{
Serial.begin(9600); // Debugging only
// Initialise the IO and ISR
vw_set_rx_pin(receive_pin);
vw_setup(4000); // Transmission rate
// Start the receiver PLL
vw_rx_start();
// Set LED pin and Buzzer
pinMode(led_pin, OUTPUT);
pinMode(pinSpeaker, OUTPUT);
}
void loop()
{
uint8_t buf[VW_MAX_MESSAGE_LEN];
uint8_t buflen = VW_MAX_MESSAGE_LEN;
// Check if a message was received
if (vw_get_message(buf, &buflen))
{
if(buf[0]=='1')
{
Serial.println("Motion detected!");
digitalWrite(led_pin,1);
playTone(300, 160);
delay(150);
}
if(buf[0]=='0')
{
Serial.println("Motion ended!");
digitalWrite(led_pin,0);
playTone(0, 0);
delay(300);
}
}
}
// duration in mSecs, frequency in hertz
void playTone(long duration, int freq)
{
duration *= 1000;
int period = (1.0 / freq) * 1000000;
long elapsed_time = 0;
while (elapsed_time < duration)
{
digitalWrite(pinSpeaker,HIGH);
delayMicroseconds(period / 2);
digitalWrite(pinSpeaker, LOW);
delayMicroseconds(period / 2);
elapsed_time += (period);
}
}
VirtualWire is an Arduino library that provides features to send short messages, without addressing, retransmit or acknowledgment, a bit like UDP over wireless, using ASK (amplitude shift keying). Supports a number of inexpensive radio transmitters and receivers.
This library allow You to send and receive data"byte" and string easily ,
First Download the library from Here .
after extract the folder, and move it to " Libraries " on the arduino Folder
this is a simple code , it will send character '1' and after 2 sec will send character '0' and so on .
this code for transmitter :
==========================================================
https://www.pjrc.com/teensy/td_libs_VirtualWire.html
// RX code
// Include VirtualWire library
#include <VirtualWire.h>
// Pins definition
const int led_pin = 13;
const int receive_pin = 12;
int pinSpeaker = 10;
void setup()
{
Serial.begin(9600); // Debugging only
// Initialise the IO and ISR
vw_set_rx_pin(receive_pin);
vw_setup(4000); // Transmission rate
// Start the receiver PLL
vw_rx_start();
// Set LED pin and Buzzer
pinMode(led_pin, OUTPUT);
pinMode(pinSpeaker, OUTPUT);
}
void loop()
{
uint8_t buf[VW_MAX_MESSAGE_LEN];
uint8_t buflen = VW_MAX_MESSAGE_LEN;
// Check if a message was received
if (vw_get_message(buf, &buflen))
{
if(buf[0]=='1')
{
Serial.println("Motion detected!");
digitalWrite(led_pin,1);
playTone(300, 160);
delay(150);
}
if(buf[0]=='0')
{
Serial.println("Motion ended!");
digitalWrite(led_pin,0);
playTone(0, 0);
delay(300);
}
}
}
// duration in mSecs, frequency in hertz
void playTone(long duration, int freq)
{
duration *= 1000;
int period = (1.0 / freq) * 1000000;
long elapsed_time = 0;
while (elapsed_time < duration)
{
digitalWrite(pinSpeaker,HIGH);
delayMicroseconds(period / 2);
digitalWrite(pinSpeaker, LOW);
delayMicroseconds(period / 2);
elapsed_time += (period);
}
}
===========================================
// TX code
// Include VirtualWire library
#include <VirtualWire.h>
int led_pin = 13;
int transmit_pin = 12;
int pir_pin = 2;
int val = 0;
int pir_state = LOW;
void setup()
{
Serial.begin(9600);
vw_set_tx_pin(transmit_pin);
vw_setup(4000); // Transmission rate
pinMode(led_pin, OUTPUT);
pinMode(pir_pin,INPUT);
}
void loop()
{
char msg[1] = {'0'};
// Get sensor value
val = digitalRead(pir_pin);
// Change message if motion is detected
if (val == 1)
{
msg[0] = '1';
digitalWrite(led_pin, HIGH); // Flash a light to show transmitting
vw_send((uint8_t *)msg, 1);
vw_wait_tx(); // Wait until the whole message is gone
if (pir_state == LOW)
{
Serial.println("Motion detected!");
pir_state = HIGH;
}
}
else
{
msg[0] = '0';
digitalWrite(led_pin, LOW);
vw_send((uint8_t *)msg, 1);
vw_wait_tx(); // Wait until the whole message is gone
if (pir_state == HIGH)
{
Serial.println("Motion ended!");
pir_state = LOW;
}
}
}
===================================
another code :
VirtualWire is an Arduino library that provides features to send short messages, without addressing, retransmit or acknowledgment, a bit like UDP over wireless, using ASK (amplitude shift keying). Supports a number of inexpensive radio transmitters and receivers.
This library allow You to send and receive data"byte" and string easily ,
First Download the library from Here .
after extract the folder, and move it to " Libraries " on the arduino Folder
this is a simple code , it will send character '1' and after 2 sec will send character '0' and so on .
this code for transmitter :
//simple Tx on pin D12
//Written By : Mohannad Rawashdeh
// 3:00pm , 13/6/2013
//http://www.genotronex.com/
//..................................
#include <VirtualWire.h>
char *controller;
void setup() {
pinMode(13,OUTPUT);
vw_set_ptt_inverted(true); //
vw_set_tx_pin(12);
vw_setup(4000);// speed of data transfer Kbps
}
void loop(){
controller="1" ;
vw_send((uint8_t *)controller, strlen(controller));
vw_wait_tx(); // Wait until the whole message is gone
digitalWrite(13,1);
delay(2000);
controller="0" ;
vw_send((uint8_t *)controller, strlen(controller));
vw_wait_tx(); // Wait until the whole message is gone
digitalWrite(13,0);
delay(2000);
}
and this is code for receiver :
The D13 LED On the arduino board must be turned ON when received character '1' and Turned Off when received character '0'
The D13 LED On the arduino board must be turned ON when received character '1' and Turned Off when received character '0'
//simple Tx on pin D12
//Written By : Mohannad Rawashdeh
// 3:00pm , 13/6/2013
//http://www.genotronex.com/
//..................................
#include <VirtualWire.h>
void setup()
{
vw_set_ptt_inverted(true); // Required for DR3100
vw_set_rx_pin(12);
vw_setup(4000); // Bits per sec
pinMode(13, OUTPUT);
vw_rx_start(); // Start the receiver PLL running
}
void loop()
{
uint8_t buf[VW_MAX_MESSAGE_LEN];
uint8_t buflen = VW_MAX_MESSAGE_LEN;
if (vw_get_message(buf, &buflen)) // Non-blocking
{
if(buf[0]=='1'){
digitalWrite(13,1);
}
if(buf[0]=='0'){
digitalWrite(13,0);
}
}
}
==========================================================
how to calculate length of antenna
The way to calculate the antenna length is to divide the speed of light by the frequency to calculate the wavelength, and divide that by 4 to get a quarter length.
In my case the frequency is 433Mhz
Speed of the light is 3x10^8 m/s
Wavelength = Speed of light (c) / Frequency (f)
= ( 3x10^8) / (433x10^6)
= 0.69284 m
Antenna length = Wavelength /4
=0.69284/4 = 0.1732 m =17.32 cm or 6.82 inch
From the above calculation it comes out to about 17.3 cm or 6.8 inches. Just cut a piece of wire 6.8 inches long and solder it into that hole marked with ANT, on each module. putting the wires on there makes a great difference.
pushbutton more than 2 sec
unsigned long keyPrevMillis = 0;
const unsigned long keySampleIntervalMs = 25;
byte longKeyPressCountMax = 80; // 80 * 25 = 2000 ms
byte longKeyPressCount = 0;
byte prevKeyState = HIGH; // button is active low
const byte keyPin = 2; // button is connected to pin 2 and GND
// called when button is kept pressed for less than 2 seconds
void shortKeyPress() {
Serial.println("short");
}
// called when button is kept pressed for more than 2 seconds
void longKeyPress() {
Serial.println("long");
}
// called when key goes from not pressed to pressed
void keyPress() {
Serial.println("key press");
longKeyPressCount = 0;
}
// called when key goes from pressed to not pressed
void keyRelease() {
Serial.println("key release");
if (longKeyPressCount >= longKeyPressCountMax) {
longKeyPress();
}
else {
shortKeyPress();
}
}
void setup() {
Serial.begin(115200);
pinMode(keyPin, INPUT_PULLUP);
}
void loop() {
// key management section
if (millis() - keyPrevMillis >= keySampleIntervalMs) {
keyPrevMillis = millis();
byte currKeyState = digitalRead(keyPin);
if ((prevKeyState == HIGH) && (currKeyState == LOW)) {
keyPress();
}
else if ((prevKeyState == LOW) && (currKeyState == HIGH)) {
keyRelease();
}
else if (currKeyState == LOW) {
longKeyPressCount++;
}
prevKeyState = currKeyState;
}
// other code goes here
}
====================================================================
Friday, August 21, 2015
Infrared with interrupt
const int irRelayPin = 4; // the number of the pushbutton pin
const int alarmPin = 13; // the number of the Buzzer pin
// variables will change:
int irState = 0; // variable for reading the pushbutton status
void setup() {
attachInterrupt(0, stopAlarm, FALLING);
// initialize the LED pin as an output:
pinMode(alarmPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(irRelayPin, INPUT);
}
void loop(){
// read the state of the pushbutton value:
irState = digitalRead(irRelayPin);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (irState == HIGH) {
// turn LED on:
digitalWrite(alarmPin, HIGH);
//Serial.println("alarm");
delay(10 * 60 * 1000); //bunyikan alarm selama 10 menit
digitalWrite(alarmPin, LOW); //matiin
}
}
void stopAlarm(){
digitalWrite(alarmPin, LOW); //stop alarm
}
const int alarmPin = 13; // the number of the Buzzer pin
// variables will change:
int irState = 0; // variable for reading the pushbutton status
void setup() {
attachInterrupt(0, stopAlarm, FALLING);
// initialize the LED pin as an output:
pinMode(alarmPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(irRelayPin, INPUT);
}
void loop(){
// read the state of the pushbutton value:
irState = digitalRead(irRelayPin);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (irState == HIGH) {
// turn LED on:
digitalWrite(alarmPin, HIGH);
//Serial.println("alarm");
delay(10 * 60 * 1000); //bunyikan alarm selama 10 menit
digitalWrite(alarmPin, LOW); //matiin
}
}
void stopAlarm(){
digitalWrite(alarmPin, LOW); //stop alarm
}
PIR alarm with interrupt
int calibrationTime = 10;
//the time when the sensor outputs a low impulse
long unsigned int lowIn;
//the amount of milliseconds the sensor has to be low
//before we assume all motion has stopped
long unsigned int pause = 5000;
boolean lockLow = true;
boolean takeLowTime;
int pirPin = 6; //the digital pin connected to the PIR sensor's output
int ledPin = 10;
int resetbutton = 2;
volatile boolean buttonState=1;
volatile int buttonState1=1;
/////////////////////////////
//SETUP
void setup(){
Serial.begin(9600);
pinMode(resetbutton, INPUT); // Pin 2 is input to which a switch is connected = INT0
attachInterrupt(0, disarm, CHANGE);
pinMode(pirPin, INPUT); //input pin
pinMode(ledPin, OUTPUT); //alarm pin
digitalWrite(pirPin, LOW);
buttonState = digitalRead(resetbutton);
//give the sensor some time to calibrate
Serial.print("calibrating sensor ");
for(int i = 0; i < calibrationTime; i++){
Serial.print(".");
delay(1000);
}
Serial.println(" done");
Serial.println("SENSOR ACTIVE");
delay(50);
}
////////////////////////////
//LOOP
void loop(){
if(digitalRead(pirPin) == HIGH){
digitalWrite(ledPin, HIGH); //the led visualizes the sensors output pin state
//makes sure we wait for a transition to LOW before any further output is made:
lockLow = false;
Serial.println("---");
//Serial.print("motion detected at ");
//Serial.print(millis()/1000);
//Serial.println(" sec");
for(int i=0;i<30;i++){
delay(1000);
if(buttonState1 == 0){ //jika ada interupt
buttonState1 = 1;
digitalWrite(ledPin, LOW);
break; //keluar dari loop for
}
}
digitalWrite(ledPin, LOW);
}
}
void disarm(){
buttonState1 =0;
}
Subscribe to:
Posts (Atom)