Introducció a la robòtica en trenta-una classes/Setè dia: automatitzem el moviment del vehicle

Salta a la navegació Salta a la cerca
Trescotxes.png

Primer de tot, felicitats a tots els alumnes pels cotxes que han fet amb els motors i rodes

que els vam donar dissabte passat. És increïble com els han pogut fer en només una

setmana, i el bé que han quedat. A la foto tenim els cotxes que cadascú ha fet amb els motors, les rodes i la placa arduino. En un primer moment, s’alimentaven a piles, però aviat es va millorar amb bateries recarregables.

Avui hem ampliat el programa anterior, que feia anar el cotxe endavant

ininterrompudament, fent que es puguin donar instruccions d’una manera senzilla.

Hem fet que accepti les comandes endavant, endarrera, esquerra, dreta, i quants passos

volem a cada comanda que se li doni.

Ho hem fet creant quatre funcions diferents, una per a cada comanda. Una funció és un

tros de programa que es pot cridar des del programa principal, i que a més, pot acceptar

dades d’entrada i tornar-ne com a resultats en acabar.

per exemple, veureu en el programa que la funció esquerra comença:

void esquerra (int passos) { .....

això ens diu que no retornarà cap valor (void), però si que vol una entrada (passos).

D’aquesta manera, cada cop que la cridem, li podrem dir quants de passos volem anar a

l’esquerra. La manera de cridar-la serà:

esquerra(500);

Aquí teniu el programa com ha quedat. Com a prova, va esquerra, dreta, endavant i

endarrera els mateixos passos.

void setup() { // configurem els 8 pins que emprem com a sortides
    pinMode(2,OUTPUT);
    pinMode(3,OUTPUT);
    pinMode(4,OUTPUT);
    pinMode(5,OUTPUT);
    pinMode(6,OUTPUT);
    pinMode(7,OUTPUT);
    pinMode(8,OUTPUT);
    pinMode(9,OUTPUT);
}

void loop() { // programa de prova, que fa un moviment de cada
    esquerra(250); // anem 250 passos a l’esquerra
    dreta(250); // anem 250 passos a la dreta
    endarrera(250);// anem 250 passos endarrera
    endavant(250); // anem 250 passos endavant
    while(true){} // ens quedem aqui per tal que no repeteixi
}

// a continuació les funcions que compliran cadascuna de les ordres
void esquerra(int passos) {
    while(passos >0){
        digitalWrite(2,1);
        digitalWrite(3,0);
        digitalWrite(4,0);
        digitalWrite(5,0);
        digitalWrite(6,1);
        digitalWrite(7,0);
        digitalWrite(8,0);
        digitalWrite(9,0);
        delayMicroseconds(1000);
        digitalWrite(2,1);
        digitalWrite(3,1);
        digitalWrite(4,0);
        digitalWrite(5,0);
        digitalWrite(6,1);
        digitalWrite(7,1);
        digitalWrite(8,0);
        digitalWrite(9,0);
        delayMicroseconds(1000);
        digitalWrite(2,0);
        digitalWrite(3,1);
        digitalWrite(4,0);
        digitalWrite(5,0);
        digitalWrite(6,0);
        digitalWrite(7,1);
        digitalWrite(8,0);
        digitalWrite(9,0);
        delayMicroseconds(1000);
        digitalWrite(2,0);
        digitalWrite(3,1);
        digitalWrite(4,1);
        digitalWrite(5,0);
        digitalWrite(6,0);
        digitalWrite(7,1);
        digitalWrite(8,1);
        digitalWrite(9,0);
        delayMicroseconds(1000);
        digitalWrite(2,0);
        digitalWrite(3,0);
        digitalWrite(4,1);
        digitalWrite(5,0);
        digitalWrite(6,0);
        digitalWrite(7,0);
        digitalWrite(8,1);
        digitalWrite(9,0);
        delayMicroseconds(1000);
        digitalWrite(2,0);
        digitalWrite(3,0);
        digitalWrite(4,1);
        digitalWrite(5,1);
        digitalWrite(6,0);
        digitalWrite(7,0);
        digitalWrite(8,1);
        digitalWrite(9,1);
        delayMicroseconds(1000);digitalWrite(2,0);
        digitalWrite(3,0);
        digitalWrite(4,0);
        digitalWrite(5,1);
        digitalWrite(6,0);
        digitalWrite(7,0);
        digitalWrite(8,0);
        digitalWrite(9,1);
        delayMicroseconds(1000);
        digitalWrite(2,1);
        digitalWrite(3,0);
        digitalWrite(4,0);
        digitalWrite(5,1);
        digitalWrite(6,1);
        digitalWrite(7,0);
        digitalWrite(8,0);
        digitalWrite(9,1);
        delayMicroseconds(1000);
        passos=passos-1;
    }
}

    void dreta(int passos) {
    while(passos >0) {
        digitalWrite(2,0);
        digitalWrite(3,0);
        digitalWrite(4,0);
        digitalWrite(5,1);
        digitalWrite(6,0);
        digitalWrite(7,0);
        digitalWrite(8,0);
        digitalWrite(9,1);
        delayMicroseconds(1000);
        digitalWrite(2,0);
        digitalWrite(3,0);
        digitalWrite(4,1);
        digitalWrite(5,1);
        digitalWrite(6,1);
        digitalWrite(7,0);
        digitalWrite(8,1);
        digitalWrite(9,1);
        delayMicroseconds(1000);
        digitalWrite(2,0);
        digitalWrite(3,0);
        digitalWrite(4,1);
        digitalWrite(5,0);
        digitalWrite(6,0);
        digitalWrite(7,0);
        digitalWrite(8,1);
        digitalWrite(9,0);
        delayMicroseconds(1000);
        digitalWrite(2,0);
        digitalWrite(3,1);
        digitalWrite(4,1);digitalWrite(5,0);
        digitalWrite(6,0);
        digitalWrite(7,1);
        digitalWrite(8,1);
        digitalWrite(9,0);
        delayMicroseconds(1000);
        digitalWrite(2,0);
        digitalWrite(3,1);
        digitalWrite(4,0);
        digitalWrite(5,0);
        digitalWrite(6,0);
        digitalWrite(7,1);
        digitalWrite(8,0);
        digitalWrite(9,0);
        delayMicroseconds(1000);
        digitalWrite(2,1);
        digitalWrite(3,1);
        digitalWrite(4,0);
        digitalWrite(5,0);
        digitalWrite(6,1);
        digitalWrite(7,1);
        digitalWrite(8,0);
        digitalWrite(9,0);
        delayMicroseconds(1000);
        digitalWrite(2,1);
        digitalWrite(3,0);
        digitalWrite(4,0);
        digitalWrite(5,0);
        digitalWrite(6,1);
        digitalWrite(7,0);
        digitalWrite(8,0);
        digitalWrite(9,0);
        delayMicroseconds(1000);
        digitalWrite(2,1);
        digitalWrite(3,0);
        digitalWrite(4,0);
        digitalWrite(5,1);
        digitalWrite(6,1);
        digitalWrite(7,0);
        digitalWrite(8,0);
        digitalWrite(9,1);
        delayMicroseconds(1000);
        passos=passos-1;
    }
}

void endarrera(int passos) {
    while(passos >0) {
        digitalWrite(2,1);
        digitalWrite(3,0);
        digitalWrite(4,0);
        digitalWrite(5,0);
        digitalWrite(6,0);
        digitalWrite(7,0);
        digitalWrite(8,0);
        digitalWrite(9,1);
        delayMicroseconds(1000);
        digitalWrite(2,1);
        digitalWrite(3,1);
        digitalWrite(4,0);
        digitalWrite(5,0);
        digitalWrite(6,0);
        digitalWrite(7,0);
        digitalWrite(8,1);
        digitalWrite(9,1);
        delayMicroseconds(1000);
        digitalWrite(2,0);
        digitalWrite(3,1);
        digitalWrite(4,0);
        digitalWrite(5,0);
        digitalWrite(6,0);
        digitalWrite(7,0);
        digitalWrite(8,1);
        digitalWrite(9,0);
        delayMicroseconds(1000);
        digitalWrite(2,0);
        digitalWrite(3,1);
        digitalWrite(4,1);
        digitalWrite(5,0);
        digitalWrite(6,0);
        digitalWrite(7,1);
        digitalWrite(8,1);
        digitalWrite(9,0);
        delayMicroseconds(1000);
        digitalWrite(2,0);
        digitalWrite(3,0);
        digitalWrite(4,1);
        digitalWrite(5,0);
        digitalWrite(6,0);
        digitalWrite(7,1);
        digitalWrite(8,0);
        digitalWrite(9,0);
        delayMicroseconds(1000);
        digitalWrite(2,0);
        digitalWrite(3,0);
        digitalWrite(4,1);
        digitalWrite(5,1);
        digitalWrite(6,1);
        digitalWrite(7,1);
        digitalWrite(8,0);
        digitalWrite(9,0);
        delayMicroseconds(1000);
        digitalWrite(2,0);
        digitalWrite(3,0);
        digitalWrite(4,0);
        digitalWrite(5,1);
        digitalWrite(6,1);
        digitalWrite(7,0);
        digitalWrite(8,0);
        digitalWrite(9,0);
        delayMicroseconds(1000);
        digitalWrite(2,1);
        digitalWrite(3,0);
        digitalWrite(4,0);
        digitalWrite(5,1);
        digitalWrite(6,1);
        digitalWrite(7,0);
        digitalWrite(8,0);
        digitalWrite(9,1);
        delayMicroseconds(1000);
        passos=passos-1;
    }
}

void endavant(int passos) {
    while(passos >0) {
        digitalWrite(2,0);
        digitalWrite(3,0);
        digitalWrite(4,0);
        digitalWrite(5,1);
        digitalWrite(6,1);
        digitalWrite(7,0);
        digitalWrite(8,0);
        digitalWrite(9,0);
        delayMicroseconds(1000);
        digitalWrite(2,0);
        digitalWrite(3,0);
        digitalWrite(4,1);
        digitalWrite(5,1);
        digitalWrite(6,1);
        digitalWrite(7,1);
        digitalWrite(8,0);
        digitalWrite(9,0);
        delayMicroseconds(1000);
        digitalWrite(2,0);
        digitalWrite(3,0);
        digitalWrite(4,1);
        digitalWrite(5,0);
        digitalWrite(6,0);
        digitalWrite(7,1);
        digitalWrite(8,0);
        digitalWrite(9,0);
        delayMicroseconds(1000);
        digitalWrite(2,0);
        digitalWrite(3,1);
        digitalWrite(4,1);
        digitalWrite(5,0);
        digitalWrite(6,0);
        digitalWrite(7,1);
        digitalWrite(8,1);
        digitalWrite(9,0);
        delayMicroseconds(1000);
        digitalWrite(2,0);
        digitalWrite(3,1);
        digitalWrite(4,0);
        digitalWrite(5,0);
        digitalWrite(6,0);
        digitalWrite(7,0);
        digitalWrite(8,1);
        digitalWrite(9,0);
        delayMicroseconds(1000);
        digitalWrite(2,1);
        digitalWrite(3,1);
        digitalWrite(4,0);
        digitalWrite(5,0);
        digitalWrite(6,0);
        digitalWrite(7,0);
        digitalWrite(8,1);
        digitalWrite(9,1);
        delayMicroseconds(1000);
        digitalWrite(2,1);
        digitalWrite(3,0);
        digitalWrite(4,0);
        digitalWrite(5,0);
        digitalWrite(6,0);
        digitalWrite(7,0);
        digitalWrite(8,0);
        digitalWrite(9,1);
        delayMicroseconds(1000);
        digitalWrite(2,1);
        digitalWrite(3,0);
        digitalWrite(4,0);
        digitalWrite(5,1);
        digitalWrite(6,1);
        digitalWrite(7,0);
        digitalWrite(8,0);
        digitalWrite(9,1);
        delayMicroseconds(1000);
        passos=passos-1;
    }
}

Com a exercici, podeu provar de fer que es mogui fent figures: quadre, triangle,

pentàgon...

Si poseu un llapis o retolador entre els dos motors, fins i tot, podreu comprovar si surt el

que voleu.