#define touchpin 4// sets the capactitive touch sensor @pin 4
#define echoPin 9
#define tringPin 10
long duration;
int distance;
int ledPin = 2; // sets the LED @pin 2
int vccPin = 8;
int rgbPin = 7;
int nontouchPin = 5;
void setup() {
pinMode(touchpin, INPUT); //sets the touch sensor as input
pinMode(ledPin, OUTPUT); //sets the led as output
pinMode(tringPin, OUTPUT); // Sets the trigPin as an OUTPUT
pinMode(echoPin, INPUT); // Sets the echoPin as an INPUT
pinMode(vccPin, OUTPUT);
pinMode(rgbPin, OUTPUT);
pinMode(nontouchPin,OUTPUT);
}
void loop() {
int touchValue = digitalRead(touchpin); //reads the touch sensor signal
if (touchValue == HIGH){ //if sensor is HIGH
digitalWrite(vccPin, HIGH);
digitalWrite(ledPin, LOW);
digitalWrite(rgbPin,HIGH);
}
else{ //otherwise
digitalWrite(vccPin,LOW);
digitalWrite(ledPin,LOW);
digitalWrite(rgbPin,LOW);
digitalWrite(nontouchPin,HIGH);
}
delay(300); //delay of 300milliseconds
int duration, distance;
digitalWrite(tringPin, HIGH);
delay(1);
digitalWrite(tringPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
if (distance <= 50 && distance >= 0) {
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
}
delay(600);
}
Comments