151.05.integração-Codigo

int pinox = A0;

int pinoy = A1;

int buttonPin = 7;

int motorDC1 = 1;

int motorDC2 = 13;

int valx;

int valy;

byte passoX = 1;

byte passoY = 1;

int motorPinX[] = {8, 9, 10, 11};

int motorPinY[] = {3, 4, 5, 6};

long int nextPulseX = 0;

long int nextPulseY = 0;

long int pulseTime = 3000;

int count;

int mode = 0;

int delayTime = 15; void writeBits(int motorPin[4], byte passo)

{

digitalWrite(motorPin[0], passo&0b0001);

digitalWrite(motorPin[1], passo&0b0010);

digitalWrite(motorPin[2], passo&0b0100);

digitalWrite(motorPin[3], passo&0b1000);

} void desligarDC

{

digitalWrite(motorDC1, LOW);

digitalWrite(motorDC2, LOW);

} void motorCCHor

{

digitalWrite(motorDC1, LOW);

digitalWrite(motorDC2, HIGH);

} void motorCCAntiHor

{

digitalWrite(motorDC1, HIGH);

digitalWrite(motorDC2, LOW);

} int passoHor(int motorPin[4], long int *nextPulse, byte *passo)

{

if(micros > *nextPulse)

{


 * nextPulse = pulseTime + micros;

digitalWrite(motorPin[3], HIGH);

digitalWrite(motorPin[2], LOW);

digitalWrite(motorPin[1], LOW);

digitalWrite(motorPin[0], LOW);

delay(delayTime);

//passo 2

digitalWrite(motorPin[3], LOW);

digitalWrite(motorPin[2], HIGH);

digitalWrite(motorPin[1], LOW);

digitalWrite(motorPin[0], LOW);

delay(delayTime);

//passo 3

digitalWrite(motorPin[3], LOW);

digitalWrite(motorPin[2], LOW);

digitalWrite(motorPin[1], HIGH);

digitalWrite(motorPin[0], LOW);

delay(delayTime);

//passo 4

digitalWrite(motorPin[3], LOW);

digitalWrite(motorPin[2], LOW);

digitalWrite(motorPin[1], LOW);

digitalWrite(motorPin[0], HIGH);

delay(delayTime);

}

} int passoHor2(int motorPin[4], long int *nextPulse, byte *passo)

{

if(micros > *nextPulse)

{


 * nextPulse = pulseTime + micros;

digitalWrite(motorPin[3], HIGH);

digitalWrite(motorPin[2], LOW);

digitalWrite(motorPin[1], LOW);

digitalWrite(motorPin[0], LOW);

delay(delayTime);

//passo 2

digitalWrite(motorPin[3], LOW);

digitalWrite(motorPin[2], LOW);

digitalWrite(motorPin[1], HIGH);

digitalWrite(motorPin[0], LOW);

delay(delayTime);

//passo 3

digitalWrite(motorPin[3], LOW);

digitalWrite(motorPin[2], HIGH);

digitalWrite(motorPin[1], LOW);

digitalWrite(motorPin[0], LOW);

delay(delayTime);

//passo 4

digitalWrite(motorPin[3], LOW);

digitalWrite(motorPin[2], LOW);

digitalWrite(motorPin[1], LOW);

digitalWrite(motorPin[0], HIGH);

delay(delayTime);

}

} void passoAntiHor(int motorPin[4], long int *nextPulse, byte *passo)

{

if(micros > *nextPulse)

{

//*passo = (*passo<<1);

//*passo = (*passo&0b00001111)|((*passo&0b10000)>>4);

//writeBits(motorPin, *passo);

digitalWrite(motorPin[0], LOW);
 * nextPulse = pulseTime + micros;

digitalWrite(motorPin[1], HIGH);

digitalWrite(motorPin[2], LOW);

digitalWrite(motorPin[3], LOW);

delay(delayTime);

//passo 3

digitalWrite(motorPin[0], LOW);

digitalWrite(motorPin[1], LOW);

digitalWrite(motorPin[2], HIGH);

digitalWrite(motorPin[3], LOW);

delay(delayTime);

//passo 4

digitalWrite(motorPin[0], LOW);

digitalWrite(motorPin[1], LOW);

digitalWrite(motorPin[2], LOW);

digitalWrite(motorPin[3], HIGH);

delay(delayTime);

//passo 1 - este passo está no final, por coincidir com o último passo feito no sentido horário

digitalWrite(motorPin[0], HIGH);

digitalWrite(motorPin[1], LOW);

digitalWrite(motorPin[2], LOW);

digitalWrite(motorPin[3], LOW);

delay(delayTime);

}

} void passoAntiHor2(int motorPin[4], long int *nextPulse, byte *passo)

{

if(micros > *nextPulse)

{

//*passo = (*passo<<1);

//*passo = (*passo&0b00001111)|((*passo&0b10000)>>4);

//writeBits(motorPin, *passo);

digitalWrite(motorPin[0], LOW);
 * nextPulse = pulseTime + micros;

digitalWrite(motorPin[1], HIGH);

digitalWrite(motorPin[2], LOW);

digitalWrite(motorPin[3], LOW);

delay(delayTime);

//passo 3

digitalWrite(motorPin[0], LOW);

digitalWrite(motorPin[1], LOW);

digitalWrite(motorPin[2], LOW);

digitalWrite(motorPin[3], HIGH);

delay(delayTime);

//passo 4

digitalWrite(motorPin[0], HIGH);

digitalWrite(motorPin[1], LOW);

digitalWrite(motorPin[2], LOW);

digitalWrite(motorPin[3], LOW);

delay(delayTime);

//passo 1 - este passo está no final, por coincidir com o último passo feito no sentido horário

digitalWrite(motorPin[0], LOW);

digitalWrite(motorPin[1], LOW);

digitalWrite(motorPin[2], HIGH);

digitalWrite(motorPin[3], LOW);

delay(delayTime);

}

} void desligar(int motorPin[4])

{

for (count = 0; count < 4; count++)

{

digitalWrite(motorPin[count], LOW);

}

} void setup

{

Serial.begin(9600);

for (count = 0; count < 4; count++)

{

pinMode(motorPinX[count], OUTPUT);

pinMode(motorPinY[count], OUTPUT);

}

desligar(motorPinX);

desligar(motorPinY);

} void loop

{

if(digitalRead(buttonPin) == HIGH)

{

if(mode == 1)

mode = 0;

else

mode = 1;

delay(2000);

}

valx = analogRead(pinox);

valx = map(valx,0,1023,0,200);

valy = analogRead(pinoy);

valy = map(valy,0,1023,0,200);

if(mode == 0)

{

if(valx>=80 && valx<=120){

desligar(motorPinX);

} else if(valx < 80) {

Serial.println("1");

passoHor2(motorPinX, &nextPulseX, &passoX);

} else if(valx > 120) {

passoAntiHor2(motorPinX, &nextPulseX, &passoX);

}

if(valy>=85 && valy<=115){

desligar(motorPinY);

} else if(valy < 80) {

Serial.println("2");

passoHor(motorPinY, &nextPulseY, &passoY);

} else if(valy > 120) {

passoAntiHor(motorPinY, &nextPulseY, &passoY);

}

}

else

{

if(valx>=80 && valx<=120){

desligarDC;

} else if(valx < 80) {

motorCCHor;

} else if(valx > 120) {

motorCCAntiHor;

}

}

}