Hi, I have been assembling a remote car controled by bluetooth via an app I have also made with app inventor.
The code uses interruptions because I have add a speed sensor that counts the holes and I use them to determinate the distane that I want to travel, for example this:
void run() //I use this void when I want to run the orders
{
char data=string[j];
switch(data) //This data is the data that comes from the bluetooth
{
case '1':
{
clean;
break;
}
case '3':
{
digitalWrite(13,1);
RightHoles = 0;
LeftHoles = 0;
while ( RightHoles<60 && LeftHoles<60 )
{
The while block doesnt work because The car doesnt go forward, it sprains a little bit to the right
The second problem is that when I have used the car for a little time, like 30 seconds, it starts beeping and it stops, the problem is not the power because I have also tested it with a power supply.
I'LL APPRECIATE YOUR HELP
Maybe the left wheel is a tiny bit bigger than the right, so even though you're sorting out the wheel speed with counting the holes, the distance / revolution is not the same?
The while block doesnt work because The car doesnt go forward, it sprains a little bit to the right
The while loop works just fine. The car doesn't. Big difference. What do your serial prints tell you is happening? Why don't you have any?
The second problem is that when I have used the car for a little time, like 30 seconds, it starts beeping and it stops, the problem is not the power because I have also tested it with a power supply.
What is beeping? There is nothing in the snippet you posted incorrectly that causes anything to beep.
I use this as void because of that, I use it when the user clicks the RUN BUTTON on the app that is the number 1. The clean its to clean the string, its a void I have putted before. And the beep problem is strange, it beeps and it stops, if you help the car with the movement it continues doing the orders, it seems that is for the energy, but its not because i have used a power supply.
void loop() // El loop es el que es repetira constantment
{
if(BT.available()) //Si el Bluetooth esta disponible fes això:
{
char data=BT.read(); //Guarda les dades caràcter a caràcter en la variable dato
string[i++]=data; //Ho posa en cadena
switch(data)
{
case '1':
{
digitalWrite(13,1); //Si es clica ON executa el programa en 2 segons i s'indica encenent el led 13(verd)
j=0; //Reset índex llista
while (j<sizeof(string))
{
run(); //executa moviement a dato
j++; //Pasem a la següent ordre
}
clean();
break;
}
case '2': //Si dato es 2 renta la cadena per a tornar a començar i para el cotxe
{
analogWrite (E1,0);
digitalWrite (M1,1);
analogWrite (E2, 0);
digitalWrite (M2,1);
clean();
break;
Motumox:
I use this as void ....... its a void I have putted before.
This is a void:-
void
vɔɪd/
noun: void; plural noun: voids
a completely empty space.
"the black void of space"
This is a function, not a void:-
void clean()
{
/ Some code here
}
And please post your code in [code]code tags[/code], not inline.
Also, post all of it, not just bits and pieces.
If it's too large for the forum to accept in a post, attach it.
Thanks.
Here you have the code, maybe there are some little mistakes because I have translated it to Spanish to English
#include <SoftwareSerial.h> //Llibreria que permet establit comunicació entre altres pins
#include <TimerOne.h> //Llibreria per a les interrupcions
SoftwareSerial BT(10,11); //10 RX, 11 TX. // Aqui conectem els pins RXD,TDX del módul Bluetooth
volatile unsigned int RightHoles=0; //Declarem les Variables dels forats de la dreta com a volatil, degut a que el seu valor variara
volatile unsigned int LeftHoles=0; //Declarem les Variables dels forats de l'esquerra com a volatil,tambè,degut a que el seu valor variara.
char string[25]; //Posem una cadena d'un màxim de 25 posicions
int i=0; //Mida de la cadena actual
int j=0;
int cl=0; //Variable per rentar la cadena
int data=0; //Variable de dato
int RightVelocity = 150; //Variable per a la velocitat del motor dret
int LeftVelocity = 150; //Variable per a la velocitta del motor esquerra
int E1 = 5; // Velocitat Motor 1
int M1 = 4; // Sentit Motor 1
int E2 = 6; //Velocitat Motor 2
int M2 = 7; // Sentit Motor 2
void setup() // El Setup es el que s'executara una sola vegada quan s'inici el programa
{
BT.begin(9600); // S'inicia el Bluetooth
Serial.begin(9600);
pinMode(13,OUTPUT); //Declarem la sortida 13 com a OUTPUT(Sortida d'informació de l'ordinador)
attachInterrupt(1, FE, RISING); //Interrupcions per als forats de l'esquerra
attachInterrupt(0, FD, RISING); //Interrupcions per als forats de la dreta
}
void FD()
{
RightHoles++;
} //Comptador del sensor de velocitat.Incrementa en 1 el comptador de la dreta
void FE() //Comptador del sensor de velocitat
{
LeftHoles++; //Incrementa en 1 el comptador de l'esquerra
}
void clean() //Això netejara la cadena
{
for (cl=0; cl<=i; cl++) //Quan això sigui cert, la cadena valdrà 0
{
string[cl]=0; //Posem cadena a 0
}
i=0;
digitalWrite(13,0); //led 13(verd) apagat no hi ha programa
RightVelocity = 150; //Variable per a la velocitat del motor dret
LeftVelocity= 150;
}
void RUN() //Això executara les ordres enviades desde el dispositiu mòbil.
{
char data=string[j]; //Hi posa a dato les ordres emmagatzemades a cadena
switch(data)
{
case '1':
{
clean; //Rentar la cadena
break;
}
case '3': //Si dato es 3, realitzara l'operació endavant
{
digitalWrite(13,1);
RightHoles = 0;
LeftHoles= 0;
while ( RightHoles<60 && LeftHoles<60 )
{
if (RightHoles>LeftHoles)
{
RightVelocity--;
LeftVelocity++;
}
if (RigthHoles<LeftHoles)
{
RightVelocity++;
LeftVelocity--;
}
if (RightHoles==LeftHoles)
{
RightVelocity= 150;
LeftVelocity= 150;
}
analogWrite (E1,RightVelocity);
digitalWrite (M1,1);
analogWrite (E2, LeftVelocity);
digitalWrite (M2,1);
}
analogWrite (E1,0);
digitalWrite (M1,1);
analogWrite (E2, 0);
digitalWrite (M2,1);
delay(1000);
break;
}
case '4': //Si dato es 4, gira a la dreta
{
RightHoles = 0;
LeftHoles = 0;
while (LeftHoles<50)
{
analogWrite (E2, LeftVelocity); //Encen motor
digitalWrite (M2,1);
}
analogWrite (E2, 0); //Atura motor
digitalWrite (M2,1);
Serial.print(j);
Serial.println(data);
delay(1000);
break;
}
case '5': //Si dato es 5, gira a la esquerra
{
RightHoles = 0;
LeftHoles = 0;
while (RightHoles<50)
{
analogWrite (E1, RightVelocity); //Encen motor
digitalWrite (M1,1);
}
analogWrite (E1, 0); //Atura motor
digitalWrite (M1,1);
Serial.print(j);
Serial.println(data);
delay(1000);
break;
}
}
}
void loop() // El loop es el que es repetira constantment
{
if(BT.available()) //Si el Bluetooth esta disponible fes això:
{
char data=BT.read(); //Guarda les dades caràcter a caràcter en la variable dato
string[i++]=data; //Ho posa en cadena
switch(data)
{
case '1':
{
digitalWrite(13,1); //Si es clica ON executa el programa en 2 segons i s'indica encenent el led 13(verd)
j=0; //Reset índex llista
while (j<sizeof(string))
{
RUN(); //executa moviement a dato
j++; //Pasem a la següent ordre
}
clean();
break;
}
case '2': //Si dato es 2 renta la cadena per a tornar a començar i para el cotxe
{
analogWrite (E1,0);
digitalWrite (M1,1);
analogWrite (E2, 0);
digitalWrite (M2,1);
clean();
break;
}
}
}
}