Skip to content

Instantly share code, notes, and snippets.

@ArduinoBasics
Created March 13, 2017 00:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save ArduinoBasics/d0e0613cc10542751ac6e9f8b3272ebd to your computer and use it in GitHub Desktop.
Save ArduinoBasics/d0e0613cc10542751ac6e9f8b3272ebd to your computer and use it in GitHub Desktop.
Use a HCSR04 ultrasonic sensor to trigger a signal to be sent wirelessly via a 433MHz RF transmitter
/* ===============================================================
Project: CHAIN BLOCKS : HC-SR04 433MHz RF Transmitter sketch
Author: Scott C
Created: 26th Feb 2017
Arduino IDE: 1.8.1
Website: http://arduinobasics.blogspot.com.au
Description: Use a HC-SR04 ultrasonic sensor to trigger a signal to be sent via
a 433Mhz RF transmitter.
================================================================== */
#define trig 3 //Connect Arduino Digital Pin3 to the Trig pin on the HC-SR04 sensor
#define echo 4 //Connect Arduino Digital Pin4 to the Echo pin on the HC-SR04 sensor
#define rfTransmit 5 //Connect Arduino Digital Pin5 to the data pin on the 433MHz RF transmitter
#define red 6 //Connect Arduino Digital Pin6 to the RED LED.
#define green 9 //Connect Arduino Digital Pin9 to the GREEN LED.
float TempC = 24.0; //Current Temperature will affect speed of sound.
float speedOfSound;
long duration, distance; //Duration will used to calculate the distance
boolean activated = false; //If distance is less than 30cm, the "activated" variable will be TRUE.
void setup() {
// put your setup code here, to run once:
pinMode(trig, OUTPUT);
pinMode(echo, INPUT);
pinMode(rfTransmit, OUTPUT);
pinMode(red, OUTPUT);
pinMode(green, OUTPUT);
}
void loop() {
activated = checkDistance(); //If distance is less than 30cm, the "activated" variable will be TRUE.
if(activated){
digitalWrite(red,LOW); //Turn off the RED LED
digitalWrite(green, HIGH); //Turn on the GREEN LED
analogWrite(rfTransmit, 150); //Transmit a signal with a duty cycle of 150/255 via the 433Mhz transmitter
}else {
digitalWrite(green,LOW); //Turn off the GREEN LED
digitalWrite(red, HIGH); //Turn on the RED LED
analogWrite(rfTransmit, 100); //Transmit a signal with a duty cycle of 100/255 via the 433Mhz transmitter
}
delay(100);
}
/* =====================================================================
* checkDistance()
* is a function that takes a reading from the HCSR04 ultrasonic sensor to determine the distance of an object to the sensor.
* When the sensor detects an object within 30cm, it will return TRUE.
* Otherwise it will return FALSE.
* =====================================================================
*/
boolean checkDistance(){
digitalWrite(trig, LOW);
delayMicroseconds(2);
digitalWrite(trig, HIGH);
delayMicroseconds(10);
digitalWrite(trig, LOW);
duration = pulseIn(echo, HIGH);
speedOfSound = (331.3 + (0.6*TempC)) / 10000.0; //Speed of sound in "cm per microsecond"
distance = (duration*speedOfSound)/2; //Calculate the distance (in cm) based on the speed of sound.
if(distance<30){ //When the distance detected is <30cm, it will return TRUE
return true;
}
return false;
}
@mavar700206
Copy link

scheme to transform Arduino into programmer for Attiny 85 chips

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment