Zohaibmaroof
Member level 2

source code is made for a project which uses ultrasonic range finders SRF04 that sense any obstacle come in front of them and vibrates vibration motors but constant vibration,i want to make the duty cycle of motors according to the distance between the obstacle and sensor that vibration becomes inversely proportional to the distance between the sensor & obstacle also i have a question that this source code is made for PIC16F877 which is controlling 4 sensors ,4 vibration motors so i want to ask where there all 4 sensor,motor pairs works separately or they work by 1 at time , here is the source code
#include <16F877.h> //standard includes
#include <stdio.h>
#use delay(clock=20000000) // 20 MHz clock
#define TOP_THRESHOLD 120 //define a threshold to determine near vs far, ie,
object or no object, for the top sensor
#define MID_THRESHOLD 140 //define a threshold to determine near vs far, ie,
object or no object, for the middle sensors
#define BOT_THRESHOLD 160 //define a threshold to determine near vs far, ie,
object or no object, for the bottom sensor
#define TIME_DELAY 30 //define a delay to wait while counting in order to
undersample
int time; //initliaze a variable to act as a counter
//code to deliver trigger pulse to sensors
void trigger() {
output_high(PIN_B2);
delay_us(12);
output_low(PIN_B2);
}
void main() {
while(true) {
//PIN_A1
time=0; //(re)set time to 0
trigger();
while(!input(PIN_A1)); //wait while the output of this sensor is low
//then when it goes high, count how many cycles it is high for
while(input(PIN_A1)) {
delay_us(TIME_DELAY);
time++;
}
//check time and take appropriate action, that is, vibrate or not
if (time<=TOP_THRESHOLD) {
output_high(PIN_C1);
}
else {
output_low(PIN_C1);
}
//repeat for the other 3 sensors:
//PIN_A2
time=0;
trigger();
while(!input(PIN_A2));
while(input(PIN_A2)) {
delay_us(TIME_DELAY);
time++;
}
if (time<=MID_THRESHOLD) {
output_high(PIN_C2);
}
else {
output_low(PIN_C2);
}
//PIN_A3
time=0;
trigger();
while(!input(PIN_A3));
while(input(PIN_A3)) {
delay_us(TIME_DELAY);
time++;
}
if (time<=MID_THRESHOLD) {
output_high(PIN_C3);
}
else {
output_low(PIN_C3);
}
//PIN_A4
time=0;
trigger();
while(!input(PIN_A4));
while(input(PIN_A4)) {
delay_us(TIME_DELAY);
time++;
}
if (time<=BOT_THRESHOLD) {
output_high(PIN_D0);
}
else {
output_low(PIN_D0);
}
delay_ms(3);
}
}
#include <16F877.h> //standard includes
#include <stdio.h>
#use delay(clock=20000000) // 20 MHz clock
#define TOP_THRESHOLD 120 //define a threshold to determine near vs far, ie,
object or no object, for the top sensor
#define MID_THRESHOLD 140 //define a threshold to determine near vs far, ie,
object or no object, for the middle sensors
#define BOT_THRESHOLD 160 //define a threshold to determine near vs far, ie,
object or no object, for the bottom sensor
#define TIME_DELAY 30 //define a delay to wait while counting in order to
undersample
int time; //initliaze a variable to act as a counter
//code to deliver trigger pulse to sensors
void trigger() {
output_high(PIN_B2);
delay_us(12);
output_low(PIN_B2);
}
void main() {
while(true) {
//PIN_A1
time=0; //(re)set time to 0
trigger();
while(!input(PIN_A1)); //wait while the output of this sensor is low
//then when it goes high, count how many cycles it is high for
while(input(PIN_A1)) {
delay_us(TIME_DELAY);
time++;
}
//check time and take appropriate action, that is, vibrate or not
if (time<=TOP_THRESHOLD) {
output_high(PIN_C1);
}
else {
output_low(PIN_C1);
}
//repeat for the other 3 sensors:
//PIN_A2
time=0;
trigger();
while(!input(PIN_A2));
while(input(PIN_A2)) {
delay_us(TIME_DELAY);
time++;
}
if (time<=MID_THRESHOLD) {
output_high(PIN_C2);
}
else {
output_low(PIN_C2);
}
//PIN_A3
time=0;
trigger();
while(!input(PIN_A3));
while(input(PIN_A3)) {
delay_us(TIME_DELAY);
time++;
}
if (time<=MID_THRESHOLD) {
output_high(PIN_C3);
}
else {
output_low(PIN_C3);
}
//PIN_A4
time=0;
trigger();
while(!input(PIN_A4));
while(input(PIN_A4)) {
delay_us(TIME_DELAY);
time++;
}
if (time<=BOT_THRESHOLD) {
output_high(PIN_D0);
}
else {
output_low(PIN_D0);
}
delay_ms(3);
}
}