Hello haroldsutton,
can you please help me integrate your code into mine?
I tried to implement it all day and I got all kind of errors since I'm not as experienced as you are.
This is my code for now:
#define echoPin 2
#define trigPin 3
long duration;
int distance;
int red_LED = 10;
int green_LED = 9;
int toggle = 0;
const int button = 8;
int buzzer = 4;
void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(red_LED, OUTPUT);
pinMode(green_LED, OUTPUT);
pinMode(button, INPUT);
pinMode(buzzer, OUTPUT);
bool buttonPressed = false;
analogWrite(red_LED, 255);
Serial.begin(9600);
}
void loop() {
//==============================================================
// ULTRASONIC SENSOR
//==============================================================
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration * 0.034 / 2;
Serial.print("Distance: ");
Serial.print(distance);
Serial.print(" cm, ");
//==============================================================
// OCCUPANCY CODE
//==============================================================
if (distance < 10) {
analogWrite(red_LED, toggle * 255);
if (toggle == 1) toggle = 0; else toggle = 1;
analogWrite(green_LED, toggle * 255);
delay (7000);
}
//==============================================================
// BUTTON CODE
//==============================================================
bool buttonPressed = digitalRead(button);
Serial.print ("Button Pressed = ");
Serial.println(buttonPressed);
}
The sensor toggles the LED when I pass my hand over it, so it works fine, the push button is connected and it shows 1/0 when pressed, and the buzzer is connected and working, so everything works individually, but I don't know how to put them together, looks impossible to me since I got an error at almost every move I make.