Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //✓ - car moving randomly for 60 sec
- //✓ - after 50 sec buzzer turnes on for 10 sec
- //✓ - when time is 60+ sec car and buzzer turn off
- //✓ - main if statment if loud noise withing 60 sec turn of arduino
- int State = LOW;
- long previousMillis = 0;
- long interval = 1000;
- long totalTime = 60000;
- long BuzzerTime = 40000;
- int t = 548;
- int analogValue = 0;
- int flag = 0;
- #define motor1pin1 2
- #define motor1pin2 3
- #define motor2pin1 4
- #define motor2pin2 5
- #define Buzzer 6
- #define Sound_Pin A0
- ///////////////////////////////////////////////////////////////////////////////////////////
- void setup() {
- flag = 0;
- initOutputs();
- Serial.begin(9600);
- }
- ///////////////////////////////////////////////////////////////////////////////////////////
- ///////////////////////////////////////////////////////////////////////////////////////////
- void loop() {
- unsigned long currentMillis = millis();
- analogValue = analogRead(Sound_Pin);
- if ((currentMillis >= totalTime) && flag == 0) {
- Serial.println("1st if Statment");
- flag = 1;
- }
- if ((currentMillis >= BuzzerTime) && flag == 0) {
- Serial.println("2nd if Statment");
- digitalWrite(Buzzer, 1);
- analogValue = t - 10;
- }
- //***************************************************
- // start of TotalTime
- if ((currentMillis < totalTime) && (analogValue < t) && (flag == 0)) {
- if (currentMillis - previousMillis >= interval) {
- previousMillis = currentMillis;
- Serial.println("3rd if Statment");
- digitalWrite(Buzzer, 0);
- moveCarRandomly();
- }
- else {
- previousMillis = currentMillis;
- Serial.println("1st else Statment");
- digitalWrite(Buzzer, 0);
- moveCarRandomly();
- }
- }//end of TotalTime
- //***************************************************
- else {
- Serial.println("2nd else Statment");
- flag = 1;
- digitalWrite(Buzzer, 0);
- Stop();
- }
- }// end of loop
- ///////////////////////////////////////////////////////////////////////////////////////////
- ///////////////////////////////////////////////////////////////////////////////////////////
- void moveCarRandomly() {
- if (State == LOW) {
- State = HIGH;
- front();
- delay(500);
- }
- else {
- State = LOW;
- right();
- delay(500);
- }
- }
- void initOutputs() {
- pinMode(Sound_Pin, INPUT);
- pinMode(motor1pin1, OUTPUT);
- pinMode(motor1pin2, OUTPUT);
- pinMode(motor2pin1, OUTPUT);
- pinMode(motor2pin2, OUTPUT);
- pinMode(Buzzer, OUTPUT);
- digitalWrite(motor1pin1, 0);
- digitalWrite(motor1pin2, 0);
- digitalWrite(motor2pin1, 0);
- digitalWrite(motor2pin2, 0);
- digitalWrite(Buzzer, 0);
- }
- void front() {
- digitalWrite(motor1pin1, HIGH);
- digitalWrite(motor1pin2, LOW);
- digitalWrite(motor2pin1, LOW);
- digitalWrite(motor2pin2, HIGH);
- }
- void right() {
- digitalWrite(motor1pin1, LOW);
- digitalWrite(motor1pin2, HIGH);
- digitalWrite(motor2pin1, LOW);
- digitalWrite(motor2pin2, HIGH);
- }
- void Stop() {
- digitalWrite(motor1pin1, LOW);
- digitalWrite(motor1pin2, LOW);
- digitalWrite(motor2pin1, LOW);
- digitalWrite(motor2pin2, LOW);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement