android - Fixing delay of sending commands to arduino through HC-06 -
me , friends working on small project on arduino. trying make remote controlled car using arduino , bluetooth module. going smooth except one. when send signal arduino make car move in specific direction making late. near 8 10 seconds. there anyway fix it? have checked delay using serial port display. command received in great delay.code given below. using arduino uno, bluetooth module hc-06, arduino ide version 1.6.7.we using android app send command mobile.
#define trigpin 7 #define echopin 6 #define yellowled 10 #define greenled 9 int motor_left[] = {2, 3}; int motor_right[] = {4, 5}; int vspeed=255; long previousmillis = -1000*10; char state = 's'; void setup() { // put setup code here, run once: serial.begin (9600); pinmode(trigpin, output); pinmode(echopin, input); pinmode(greenled, output); pinmode(yellowled, output); for(int = 0; < 2; i++){ pinmode(motor_left[i], output); pinmode(motor_right[i], output); } } void loop() { // put main code here, run repeatedly: long duration, inches, cm; if(serial.available() > 0){ state = serial.read(); } // ping))) triggered high pulse of 2 or more microseconds. // give short low pulse beforehand ensure clean high pulse: digitalwrite (trigpin, low); delaymicroseconds (2); digitalwrite (trigpin, high); delaymicroseconds (10); digitalwrite (trigpin, low); duration = pulsein (echopin, high); inches = microsecondstoinches (duration); cm = microsecondstocentimeters (duration); if (cm <= 10 || state == 's') { motor_stop (); } else { motor_ready (); } delay(100); } void motor_stop(){ serial.print (state); serial.println(); digitalwrite(motor_left[0], low); digitalwrite(motor_left[1], low); digitalwrite(motor_right[0], low); digitalwrite(motor_right[1], low); digitalwrite (yellowled, high); digitalwrite (greenled, low); delay(25); } void motor_ready () { serial.print (state); serial.println(); if (state == '0') { vspeed = 0; } else if (state == '1') { vspeed = 75; } else if (state == '2') { vspeed = 150; } else if (state == '3') { vspeed = 200; } else if (state == '4') { vspeed = 255; } if (state == 'f') { driveforward (); } else if (state == 'g') { driveforwardleft (); } else if (state == 'i') { driveforwardright (); } else if (state == 'b') { drivebackward (); } else if (state == 'h') { drivebackwardleft (); } else if (state == 'j') { drivebackwardright (); } digitalwrite (yellowled, low); digitalwrite (greenled, high); } void driveforward () { analogwrite(motor_left [0], vspeed); analogwrite(motor_left [1], 0); analogwrite(motor_right [0], vspeed); analogwrite(motor_right [1], 0); } void driveforwardleft () { analogwrite(motor_left [0], 0); analogwrite(motor_left [1], 0); analogwrite(motor_right [0], vspeed); analogwrite(motor_right [1], 0); } void driveforwardright () { analogwrite(motor_left [0], vspeed); analogwrite(motor_left [1], 0); analogwrite(motor_right [0], 0); analogwrite(motor_right [1], 0); } void drivebackward () { analogwrite(motor_left [0], 0); analogwrite(motor_left [1], vspeed); analogwrite(motor_right [0], 0); analogwrite(motor_right [1], vspeed); } void drivebackwardleft () { analogwrite(motor_left [0], 0); analogwrite(motor_left [1], 0); analogwrite(motor_right [0], 0); analogwrite(motor_right [1], vspeed); } void drivebackwardright () { analogwrite(motor_left [0], 0); analogwrite(motor_left [1], vspeed); analogwrite(motor_right [0], 0); analogwrite(motor_right [1], 0); } long microsecondstoinches(long microseconds) { return microseconds / 74 / 2; } long microsecondstocentimeters(long microseconds) { return microseconds / 29 / 2; }
Comments
Post a Comment