i have small logic problem in code, when forward motor runs. need stop running motors when sensor detects something. continuously running till give next command.
this code. if statement inside loop().
moderator edit: code tags. please, explain me why it's difficult? awol
code: [select]
//b , d sensor values
//sr=0 or 1 or 2 arrives android.
void loop() {
sensor();
if(serial.available())
{
//serial.print("serial in ");
sr= serial.read();
if(b>50)
{
if(sr=='0')
{
applybreak();
}
else if(sr=='3')
{
left();
}
else if(sr=='4')
{
right();
}
else if(sr=='1' && frwd)
{
forward();
}
else if(sr=='5')
{
forwardspeedup();
}
}
else
{
applybreak();
}
if(d>50)
{
if(sr=='2' || back)
{
backward();
}
else if(sr=='0')
{
applybreak();
}
// else
// {
// applybreak();
// }
}
else
{
applybreak();
}
}
else
{
// serial.print(" serial error...");
digitalwrite(error,high);
}
}
sensor()
{
//two sensor code
// b , d read sensor value here.
}
void forward()
{
frwd=true;
back=false
serial.print(" forward b d : ");
analogwrite(motor1, 160);
analogwrite(motor2, 0);
analogwrite(motor3, 160);
analogwrite(motor4, 0);
}
void backward()
{
back=true;
frwd=false;
analogwrite(motor1, 0);
analogwrite(motor2, 160);
analogwrite(motor3, 0);
analogwrite(motor4, 160);
}
void left()
{
digitalwrite(error,low);
analogwrite(motor1, 190);
analogwrite(motor2, 0);
analogwrite(motor3, 100);
analogwrite(motor4, 0);
delay(1500);
}
void right()
{
digitalwrite(error,low);
analogwrite(motor1, 100);
analogwrite(motor2, 0);
analogwrite(motor3, 190);
analogwrite(motor4, 0);
delay(1500);
}
void applybreak()
{
digitalwrite(error,high);
digitalwrite(motor1, low);
digitalwrite(motor2, low);
digitalwrite(motor3, low);
digitalwrite(motor4, low);
delaymicroseconds(1);
}
this code. if statement inside loop().
moderator edit: code tags. please, explain me why it's difficult? awol
Arduino Forum > Using Arduino > Programming Questions > Ultra Sonic Sensor logic problem in program
arduino
Comments
Post a Comment