Go Down

Topic: buzzer and vibraton motor wont work (Read 136 times) previous topic - next topic

MovieFan1995

Dec 04, 2018, 01:15 pm Last Edit: Dec 04, 2018, 08:56 pm by MovieFan1995
In this code I am making 2 threads to work
every thread is connected to an ultrasonic sensor
if the distance is less than 10 cm
the vib and the buzzer should work
however, they are not working
but if I use leds , the leds works but it's very faded
ps:the buzzer and the vibarton motor are not defective

Code: [Select]
#include <TimerThree.h> //Include the library corresponding to your Arduino.
#include "Thread.h"
#include "ThreadController.h"

//////////////////////////////////////////////////
//////////////vibration motor-buzzer/////////////
//defining pin numbers
const int vib = 5;
const int buzzer = 22;
/////////////////////////////////////////////////
// Create a new Class, called SensorThread, that inherits from Thread
class SensorThread: public Thread
{
public:
   int trigPin; ////////////////ultrasonic sensors////////////////
   int echoPin ;
   int trigPin2 ;
   int echoPin2 ;
  
  float duration, distance;
  float duration2, distance2;
  int safetyDistance,safetyDistance2;
  // No, "run" cannot be anything...
  // Because Thread uses the method "run" to run threads,
  // we MUST overload this method here. using anything other
  // than "run" will not work properly...
  void run(){
    // Write a pulse to the HC-SR04 Trigger Pin
  digitalWrite(trigPin, LOW);// Clears the trigPin
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);// Sets the trigPin on HIGH state for 10 micro seconds to generate the ultrasound wave
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  
  // Measure the response from the HC-SR04 Echo Pin
  // Reads the echoPin, returns the sound wave travel time in microseconds
  duration = pulseIn(echoPin, HIGH);
  
  // Determine distance from duration// Calculating the distance
  // Use 343 metres per second as speed of sound
  
    distance = (duration / 2) * 0.0343;   // Reads the analog pin, and saves it localy
  ////////////////////////////////////////////////////////////////
  digitalWrite(trigPin2, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin2, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin2, LOW);
  
  // Measure the response from the HC-SR04 Echo Pin
 
  duration2 = pulseIn(echoPin2, HIGH);
  
  // Determine distance from duration
  // Use 343 metres per second as speed of sound
  
  distance2 = (duration2 / 2) * 0.0343;

  runned();  }
};

SensorThread analog1 = SensorThread();
SensorThread analog2 = SensorThread();
// Instantiate a new ThreadController
ThreadController controller = ThreadController();
// This is the callback for the Timer
void timerCallback(){
  controller.run();
}

void setup() { // put your setup code here, to run once:

  Serial.begin(9600);
  analog1.trigPin=45;
  analog1.echoPin=44;
  pinMode(analog1.trigPin, OUTPUT);
  pinMode(analog1.echoPin, INPUT);
  analog1.setInterval(500);////////////////////////////////////

  analog2.trigPin2=41;
  analog2.echoPin2=40;
  pinMode(analog2.trigPin2, OUTPUT);
  pinMode(analog2.echoPin2, INPUT);
  analog2.setInterval(500);/////////////////////////////////////

  controller.add(&analog1);
  controller.add(&analog2);

  Timer3.initialize(20000);
  Timer3.attachInterrupt(timerCallback);
  Timer3.start();
}

void loop() {// put your main code here, to run repeatedly:

  Serial.print("Distance = ");
  Serial.print(analog1.distance);
  Serial.println(" cm   a");
  analog1.safetyDistance = analog1.distance;
  if (analog1.safetyDistance <= 10){
  digitalWrite(vib, HIGH);
  delay(1000);
  digitalWrite(vib, LOW);}
  else{
  digitalWrite(vib, LOW);}
  Serial.print("\n");
  delay(10);

  Serial.print("Distance = ");
  Serial.print(analog2.distance2);
  Serial.println(" cm   b");
  analog2.safetyDistance2 = analog2.distance2;
  if (analog2.safetyDistance2 <= 10){
  digitalWrite(buzzer, HIGH); delay(2000); digitalWrite(buzzer, LOW);}
  else{digitalWrite(buzzer, LOW);}
  Serial.print("\n");
  
  delay(10);}


Go Up