safwan092

Untitled

Nov 15th, 2022
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.08 KB | None | 0 0
  1. //✓ - car moving randomly for 60 sec
  2. //✓ - after 50 sec buzzer turnes on for 10 sec
  3. //✓ - when time is 60+ sec car and buzzer turn off
  4. //✓ - main if statment if loud noise withing 60 sec turn of arduino
  5.  
  6.  
  7. int State = LOW;
  8. long previousMillis = 0;
  9.  
  10. long interval = 1000;
  11. long totalTime = 60000;
  12.  
  13. long BuzzerTime = 40000;
  14.  
  15. int t = 548;
  16. int analogValue = 0;
  17. int flag = 0;
  18.  
  19. #define motor1pin1 2
  20. #define motor1pin2 3
  21. #define motor2pin1 4
  22. #define motor2pin2 5
  23. #define Buzzer 6
  24. #define Sound_Pin A0
  25.  
  26. ///////////////////////////////////////////////////////////////////////////////////////////
  27. void setup() {
  28. flag = 0;
  29. initOutputs();
  30. Serial.begin(9600);
  31. }
  32. ///////////////////////////////////////////////////////////////////////////////////////////
  33.  
  34.  
  35. ///////////////////////////////////////////////////////////////////////////////////////////
  36. void loop() {
  37. unsigned long currentMillis = millis();
  38. analogValue = analogRead(Sound_Pin);
  39.  
  40. if ((currentMillis >= totalTime) && flag == 0) {
  41. Serial.println("1st if Statment");
  42. flag = 1;
  43. }
  44.  
  45. if ((currentMillis >= BuzzerTime) && flag == 0) {
  46. Serial.println("2nd if Statment");
  47. digitalWrite(Buzzer, 1);
  48. }
  49.  
  50. //***************************************************
  51. // start of TotalTime
  52. if ((currentMillis < totalTime) && (analogValue < t) && (flag == 0)) {
  53. if (currentMillis - previousMillis >= interval) {
  54. previousMillis = currentMillis;
  55. Serial.println("3rd if Statment");
  56. digitalWrite(Buzzer, 0);
  57. moveCarRandomly();
  58. }
  59. else {
  60. previousMillis = currentMillis;
  61. Serial.println("1st else Statment");
  62. digitalWrite(Buzzer, 0);
  63. moveCarRandomly();
  64. }
  65. }//end of TotalTime
  66. //***************************************************
  67.  
  68. else {
  69. Serial.println("2nd else Statment");
  70. flag = 1;
  71. digitalWrite(Buzzer, 0);
  72. Stop();
  73. }
  74. }// end of loop
  75. ///////////////////////////////////////////////////////////////////////////////////////////
  76.  
  77.  
  78. ///////////////////////////////////////////////////////////////////////////////////////////
  79.  
  80. void moveCarRandomly() {
  81. if (State == LOW) {
  82. State = HIGH;
  83. front();
  84. }
  85. else {
  86. State = LOW;
  87. right();
  88. }
  89. }
  90.  
  91. void initOutputs() {
  92. pinMode(Sound_Pin, INPUT);
  93. pinMode(motor1pin1, OUTPUT);
  94. pinMode(motor1pin2, OUTPUT);
  95. pinMode(motor2pin1, OUTPUT);
  96. pinMode(motor2pin2, OUTPUT);
  97. pinMode(Buzzer, OUTPUT);
  98. digitalWrite(motor1pin1, 0);
  99. digitalWrite(motor1pin2, 0);
  100. digitalWrite(motor2pin1, 0);
  101. digitalWrite(motor2pin2, 0);
  102. digitalWrite(Buzzer, 0);
  103. }
  104.  
  105. void front() {
  106. digitalWrite(motor1pin1, HIGH);
  107. digitalWrite(motor1pin2, LOW);
  108.  
  109. digitalWrite(motor2pin1, LOW);
  110. digitalWrite(motor2pin2, HIGH);
  111.  
  112. }
  113.  
  114.  
  115. void right() {
  116. digitalWrite(motor1pin1, LOW);
  117. digitalWrite(motor1pin2, HIGH);
  118.  
  119. digitalWrite(motor2pin1, LOW);
  120. digitalWrite(motor2pin2, HIGH);
  121. }
  122.  
  123.  
  124. void Stop() {
  125. digitalWrite(motor1pin1, LOW);
  126. digitalWrite(motor1pin2, LOW);
  127.  
  128. digitalWrite(motor2pin1, LOW);
  129. digitalWrite(motor2pin2, LOW);
  130. }
Advertisement
Add Comment
Please, Sign In to add comment