speedEC
Full Member level 6
Dear friends,
I am using PIC16F1938 & ULTRASONIC SENSOR. I have coded in C (MPLAB v8.63). If I put a object in front of ultrasonic sensor [preset (>2 CM and < 100 CM) range], it shows correct distance. But if I move the object back and forth, it displays wrong values. For example, if object placed at 10CM from Sensor, it correctly displays 10CM. If we move 2CM front or back, it displays as 115 CM or so, frequently. What could be the problem? Even after I have checked 3 times the value of sensor, it shows wrong values. Do we need any other measure to correct the sensor reading? I have tried using delays of about 500ms between reads. But same result. Any idea?
code:
No interrupts used. Only polling RB0.
thank you all,
pmk
I am using PIC16F1938 & ULTRASONIC SENSOR. I have coded in C (MPLAB v8.63). If I put a object in front of ultrasonic sensor [preset (>2 CM and < 100 CM) range], it shows correct distance. But if I move the object back and forth, it displays wrong values. For example, if object placed at 10CM from Sensor, it correctly displays 10CM. If we move 2CM front or back, it displays as 115 CM or so, frequently. What could be the problem? Even after I have checked 3 times the value of sensor, it shows wrong values. Do we need any other measure to correct the sensor reading? I have tried using delays of about 500ms between reads. But same result. Any idea?
code:
No interrupts used. Only polling RB0.
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 #include <htc.h> __CONFIG (FCMEN_OFF & CPD_OFF & CP_OFF & LVP_OFF & MCLRE_OFF & PWRTE_OFF & \ WDTE_OFF & WRT_OFF & BOREN_OFF & FOSC_INTOSC); #define LED RC6 #define TRIGGER RC1 #define ECHO RB0 #define OPEN_DOOR() {RC2 = 1; RC3 = 0; __delay_ms(1000); RC2 = 0; RC3 = 0;} #define CLOSE_DOOR() {RC2 = 0; RC3 = 1; __delay_ms(1000); RC2 = 0; RC3 = 0;} #ifndef _XTAL_FREQ // Unless already defined assume 4MHz system frequency // This definition is required to calibrate __delay_us() and __delay_ms() #define _XTAL_FREQ 4000000 #endif int distance; int checkValue; char readPortB; volatile bit BRAKE_ON; void main (void){ INTCON = 0; TRISA = 00000000; TRISB = 00000001; TRISC = 00000000; TRISE = 00000000; PORTA = 00000000; PORTB = 00000000; PORTC = 00000000; PORTE = 00000000; ANSELA = 00000000; ANSELB = 00000000; IRCF3 = 1; // 4MHz CLOCK IRCF2 = 1; IRCF1 = 0; IRCF0 = 1; SCS0 = 0; // 00 = Clock determined by FOSC<2:0> in Configuration Word 1. SCS1 = 0; __delay_ms(10); // TIMER1 MODULE SETTINGS T1CON = 0x00; // TMR1CS = 00; // 00 = Timer1 clock source is instruction clock (FOSC/4) // T1CKPS = 00; // 10 = 1:1 Prescale value // T1OSCEN = 0; // T1SYNC = 0; // TMR1ON = 0; // Timer1 OFF TMR1L = 0; TMR1H = 0; LED = 1; __delay_ms(500); LED = 0; BRAKE_ON = 0; while(1){ TMR1L = 0; TMR1H = 0; TRIGGER = 0; //TRIGGER LOW __delay_us(2); // 2uS delay // ACTIVATE ULTRASONIC SENSOR TRIGGER = 1; //TRIGGER HIGH __delay_us(10); // 10uS delay TRIGGER = 0; //TRIGGER LOW while(!ECHO); TMR1ON = 1; while(ECHO); TMR1ON = 0; distance = ((TMR1L | (TMR1H << 8)) / 59); // in CM (58.82) //DISTANCE BETWEEN 2 & 100 CM if((distance >= 2 && distance <= 100) && !BRAKE_ON){ for(int i = 0; i < 3; i++){ // check values 3 times TMR1L = 0; TMR1H = 0; TRIGGER = 0; //TRIGGER LOW __delay_us(2); // 2uS delay // ACTIVATE ULTRASONIC SENSOR TRIGGER = 1; //TRIGGER HIGH __delay_us(10); // 10uS delay TRIGGER = 0; //TRIGGER LOW while(!ECHO); TMR1ON = 1; while(ECHO); TMR1ON = 0; distance = ((TMR1L | (TMR1H << 8)) / 59); // in CM (58.82) if((distance >= 2 && distance <= 100) && !BRAKE_ON){ checkValue++; } } if (checkValue > 1){ // 2 out 3, same ranges BRAKE_ON = 1; LED = 1; __delay_ms(500); LED = 0; OPEN_DOOR(); } } // > 100 CM && < 400 CM else if(distance > 101 && distance < 400 && BRAKE_ON){ for(int i = 0; i < 3; i++){ // check values 3 times TMR1L = 0; TMR1H = 0; TRIGGER = 0; //TRIGGER LOW __delay_us(2); // 2uS delay // ACTIVATE ULTRASONIC SENSOR TRIGGER = 1; //TRIGGER HIGH __delay_us(10); // 10uS delay TRIGGER = 0; //TRIGGER LOW while(!ECHO); TMR1ON = 1; while(ECHO); TMR1ON = 0; distance = ((TMR1L | (TMR1H << 8)) / 59); // in CM (58.82) if(distance > 101 && distance < 400 && BRAKE_ON){ checkValue++; } } if (checkValue > 1){ // 2 out 3, same ranges BRAKE_ON = 0; LED = 1; __delay_ms(500); LED = 0; CLOSE_DOOR(); } } checkValue = 0; readPortB = PORTB; } }
thank you all,
pmk