0% found this document useful (0 votes)
10 views7 pages

Reporte

Reporte
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views7 pages

Reporte

Reporte
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Aquí tienes el código completo del archivo proporcionado:

/**
* Coded by edo0xff <[email protected]>
*/

// PIN CONFIGURATION
#define H_IN1 6
#define H_IN2 9
#define H_IN3 10
#define H_IN4 11

#define LOW_SPEED_LED 2
#define MEDIUM_SPEED_LED 3
#define HIGH_SPEED_LED 4

#define ACTION_A 12
#define ACTION_B 13

// Do not touch this


#define LOW_SPEED_WRITE_VALUE 127
#define MEDIUM_SPEED_WRITE_VALUE 191
#define HIGH_SPEED_WRITE_VALUE 255

String comando;

int speed = LOW_SPEED_WRITE_VALUE;


int geared_speed = 1;

bool action_a = false;


bool action_b = false;
void setup()
{
Serial.begin(9600);

pinMode(H_IN1, OUTPUT);
pinMode(H_IN2, OUTPUT);
pinMode(H_IN3, OUTPUT);
pinMode(H_IN4, OUTPUT);

pinMode(LOW_SPEED_LED, OUTPUT);
pinMode(MEDIUM_SPEED_LED, OUTPUT);
pinMode(HIGH_SPEED_LED, OUTPUT);

pinMode(ACTION_A, OUTPUT);
pinMode(ACTION_B, OUTPUT);
}

void loop() {
if(Serial.available())
{
comando = Serial.readStringUntil('#');

Serial.print("Comand raw: ");


Serial.print(comando);
Serial.print('\t');

char direccion = comando.charAt(0);


char boton = comando.charAt(1);

Serial.print("Direccion: ");
Serial.print(direccion);
Serial.print('\t');

Serial.print("Boton: ");
Serial.print(boton);
Serial.print('\n');

// Reading buttons inputs


if (boton == '1')
{
geared_speed--;
}
else if (boton == '3')
{
geared_speed++;
}
else if (boton == '2')
{
action_a = !action_a;
}
else if (boton == '4')
{
action_b = !action_b;
}

// Turn on/off actions


if (action_a)
{
digitalWrite(ACTION_A, HIGH);
}
else
{
digitalWrite(ACTION_A, LOW);
}

if (action_b)
{
digitalWrite(ACTION_B, HIGH);
}
else
{
digitalWrite(ACTION_B, LOW);
}

// Speed range validation


if (geared_speed < 1)
{
geared_speed = 1;
}
else if (geared_speed > 3)
{
geared_speed = 3;
}

if (geared_speed == 1)
{
speed = LOW_SPEED_WRITE_VALUE;
digitalWrite(LOW_SPEED_LED, HIGH);
digitalWrite(MEDIUM_SPEED_LED, LOW);
digitalWrite(HIGH_SPEED_LED, LOW);
}
else if (geared_speed == 2)
{
speed = MEDIUM_SPEED_WRITE_VALUE;
digitalWrite(LOW_SPEED_LED, HIGH);
digitalWrite(MEDIUM_SPEED_LED, HIGH);
digitalWrite(HIGH_SPEED_LED, LOW);
}
else if (geared_speed == 3)
{
speed = HIGH_SPEED_WRITE_VALUE;
digitalWrite(LOW_SPEED_LED, HIGH);
digitalWrite(MEDIUM_SPEED_LED, HIGH);
digitalWrite(HIGH_SPEED_LED, HIGH);
}

// Turning on/off H inputs depending of the readed direction


if (direccion == 'N')
{
analogWrite(H_IN2, 0);
analogWrite(H_IN1, speed);
analogWrite(H_IN3, 0);
analogWrite(H_IN4, speed);
}
else if (direccion == 'S')
{
analogWrite(H_IN2, speed);
analogWrite(H_IN1, 0);
analogWrite(H_IN3, speed);
analogWrite(H_IN4, 0);
}
else if (direccion == 'W')
{
analogWrite(H_IN2, 0);
analogWrite(H_IN1, speed);
analogWrite(H_IN3, speed);
analogWrite(H_IN4, 0);
}
else if (direccion == 'E')
{
analogWrite(H_IN2, speed);
analogWrite(H_IN1, 0);
analogWrite(H_IN3, 0);
analogWrite(H_IN4, speed);
}
else if (direccion == 'D')
{
analogWrite(H_IN2, 0);
analogWrite(H_IN1, speed);
analogWrite(H_IN3, 0);
analogWrite(H_IN4, 0);
}
else if (direccion == 'A')
{
analogWrite(H_IN2, 0);
analogWrite(H_IN1, 0);
analogWrite(H_IN3, 0);
analogWrite(H_IN4, speed);
}
else if (direccion == 'C')
{
analogWrite(H_IN2, speed);
analogWrite(H_IN1, 0);
analogWrite(H_IN3, 0);
analogWrite(H_IN4, 0);
}
else if (direccion == 'B')
{
analogWrite(H_IN2, 0);
analogWrite(H_IN1, 0);
analogWrite(H_IN3, speed);
analogWrite(H_IN4, 0);
}
else if (direccion == '0')
{
analogWrite(H_IN2, 0);
analogWrite(H_IN1, 0);
analogWrite(H_IN3, 0);
analogWrite(H_IN4, 0);
}
}
}

Si necesitas alguna aclaración o explicación sobre el código, no dudes en


preguntar.

You might also like