Msquare, thanks for your excellent answer and your suggestions. Now I can understand better the how works the serial communication... I understood what you said about use millis(), but I think that it can't solve my problem; what I really need is to "brake" the motors, while both are moving until x time, but x is not reached yet. ie in my proyect the time per movement is 5 seconds, the motors start to move, the robot is going to hit an obstacle and the 5 seconds has not pass yet, so I will write a command with the intention that the robot can stops before crash.
...Yes I lied, I'm not making a simple car and doing a robot. I'm finish the Junior year in my high school, so I decided to make a great final proyect. I started a week ago and my proyect still under construction, but my idea is that the robot can recognize voice commands and in addition to that, it can move in an spefic direction, speak or show expressions in a led matrix.
If you and anybody want to see the code what I have. This is it (sorry, I'm a spanish native so almost all the code is commented and structured in spanish):
#include <stdlib.h>
#include <Servo.h>
//declaracion de variables
Servo izquierdo;
Servo derecho;
int speed = 95; // velocidad inicial servos
int speedMatrix = 5; // velocidad matriz, numero de veces que se repite cada fotograma
int rowA[] = {9,8,7,6,5,4,3,2}; // pins filas matriz led
long tiempoanterior = 0; // tiempo transcurrido antes del movimiento
long intervalo = 5000;
int colA[] = {17,16,15,14,13,12}; // pins columnas matriz
byte data[] = {0,0,0,0,0,0,0,0};
//--------------------------------------------------
//Inicio del programa de recepcion de comandos
//------------------------------------------------------------------------------------------------------------
void setup()
{
iniciarServo();
Serial.begin(9600);
for(int i = 0; i <8; i++){ //establecer todos los pines de la matriz como OUTPUT
pinMode(rowA[i], OUTPUT);
pinMode(colA[i], OUTPUT);
}
}
void loop()
{
bocacerrada();
refrescarDatos();
}
void refrescarDatos()
{
Parar();
if (Serial.available() > 0) {
char *datos = "";
*datos = Serial.read();
int comando = atol (datos);
Serial.println(comando);
procesarcomando(comando);
}
}
void procesarcomando(int comando) {
if (comando == 1) {
irAdelante();
delay(5000);
Parar();
tiempoanterior = millis();
}
else if (comando == 2) {
irAtras();
delay(5000);
Parar();
tiempoanterior = millis();
}
else if (comando == 3) {
irDerecha();
delay(5000);
Parar();
tiempoanterior = millis();
}
else if (comando == 4) {
irIzquierda();
delay(5000);
Parar();
tiempoanterior = millis();
}
else if (comando == 5) {
long boca = 1300;
feliz(boca);
tiempoanterior = millis();
}
else if (comando == 6) {
long boca = 1300;
triste(boca);
tiempoanterior = millis();
}
else if (comando == 7) {
long boca = 1300;
enamorado(boca);
tiempoanterior = millis();
}
else if (comando == 8) {
long boca = 300;
bocaabierta(boca);
tiempoanterior = millis();
}
else if (comando == 9) {
irDerecha();
delay(2000);
irIzquierda();
delay(1000);
irDerecha();
delay(1000);
irAdelante();
delay(500);
irAtras();
delay(500);
irAdelante();
delay(500);
irAtras();
delay(300);
irAdelante();
delay(100);
irAtras();
delay(200);
irIzquierda();
delay(7000);
irDerecha();
delay(2000);
}
else {
irAdelante();
delay(200);
irAtras();
delay(200);
}
}
//Planteamiento de funciones de movimiento
void iniciarServo()
{
izquierdo.attach(11);
derecho.attach(10);
}
void setSpeed(int nuevaVel)
{
if(nuevaVel >= 100) {
nuevaVel = 100;
}
if(nuevaVel <= 0) {
nuevaVel = 0;
}
speed = nuevaVel * 0.95;
}
void irAdelante()
{
izquierdo.write(95 + speed);
derecho.write(95 - speed);
}
void irAtras()
{
izquierdo.write(95 - speed);
derecho.write(95 + speed);
}
void irDerecha()
{
izquierdo.write(95 + speed);
derecho.write(95 + speed);
}
void irIzquierda()
{
izquierdo.write(95 - speed);
derecho.write(95 - speed);
}
void Parar()
{
izquierdo.write(95);
derecho.write(95);
}
void bocacerrada() {
data[0] = B11000011; //figura que aparece en la matriz
data[1] = B11000011;
data[2] = B00000000;
data[3] = B11111111;
data[4] = B11111111;
data[5] = B00000000;
showSprite(speedMatrix);
}
void bocaabierta(long boca){
tiempoanterior = millis();
while (millis() - tiempoanterior < boca) {
data[0] = B11000011; //figura que aparece en la matriz
data[1] = B11000011;
data[2] = B00000000;
data[3] = B11111111;
data[4] = B10000001;
data[5] = B11111111;
showSprite(speedMatrix);
showSprite(speedMatrix);
}
}
void feliz(long boca){
tiempoanterior = millis();
while (millis() - tiempoanterior < boca) {
data[0] = B11000011; //figura que aparece en la matriz
data[1] = B11000011;
data[2] = B00000000;
data[3] = B11111111;
data[4] = B10000001;
data[5] = B01111110;
showSprite(speedMatrix);
showSprite(speedMatrix);
}
}
void triste(long boca){
tiempoanterior = millis();
while (millis() - tiempoanterior < boca) {
data[0] = B11000011; //figura que aparece en la matriz
data[1] = B11000011;
data[2] = B00000000;
data[3] = B01111110;
data[4] = B10000001;
data[5] = B11111111;
showSprite(speedMatrix);
showSprite(speedMatrix);
}
}
void enamorado(long boca){
tiempoanterior = millis();
while (millis() - tiempoanterior < boca) {
data[0] = B01100011; //figura que aparece en la matriz
data[1] = B01010101;
data[2] = B01001001;
data[3] = B00100010;
data[4] = B00010100;
data[5] = B00001000;
showSprite(speedMatrix);
showSprite(speedMatrix);
}
}
// Funcion de la matriz
//An array to store power values to act as bit masks
const int powers[] = {1,2,4,8,16,32,64,128};
//Runs a pattern where each LED is lit one after another
void showSprite(int speed2){
for(int iii = 0; iii < speed2; iii++){ //show the current frame speed2 times
for(int column = 0; column < 8; column++){ //iterate through each column
for(int i = 0; i < 8; i++){
digitalWrite(rowA[i], LOW); //turn off all row pins
}
for(int i = 0; i < 8; i++){ //Set only the one pin
if(i == column){ digitalWrite(colA[i], LOW);} //turns the current row on
else{ digitalWrite(colA[i], HIGH); }//turns the rest of the rows off
}
for(int row = 0; row < 8; row++){ //iterate through each pixel in the current column
int bit = (data[column] >> row) & 1;
if(bit == 1){
digitalWrite(rowA[row], HIGH); //if the bit in the data array is set turn the LED on
}
} //leave the column on for pauseDelay microseconds (too high a delay causes flicker)
}
}
}
Click on the link If you want to see a little preview of the Robot (remember that I speak spanish and that I'm still working in my proyect)
*Please give me opinions and suggestions of all my proyect
*In a few weeks I will leave the breadboard and I will post my progress :).