#include <SPI.h>
#include <Wire.h> // libreria para bus I2C
#include <Adafruit_GFX.h> // libreria para pantallas graficas
#include <Adafruit_SSD1306.h> // libreria para controlador SSD1306
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define IMAGE_HEIGHT 64
#define IMAGE_WIDTH 128
#define OLED_RESET 4 // necesario por la libreria pero no usado
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
#include <Keypad.h>
char pulsacion; // almacena la tecla pulsada
int contador = 1; // cuenta el nivel del menu en el que se esta
const byte ROWS = 4; //Cuatro Filas
const byte COLS = 4; //Cuatro Columnas
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = {9,8,7,6}; //Puntos de conexion del teclado al arduino FILAS
byte colPins[COLS] = {5,4,3,2}; //Puntos de conexion del teclado al arduino COLUMNAS
Keypad Teclado1 = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS ); //Configura el teclado
int pin2=10; //Entrada 2 del L293D
int pin7=11; //Entrada 7 del L293D
int pote=A1; //Potenciómetro
int tp1=A2;
int tp2=A3;
int valorpote; //Variable que recoge el valor del potenciómetro
int pwm1; //Variable del PWM 1
int pwm2; //Variable del PWM 2
int TS1;
int TS2;
int TPS1;
int TPS2;
int ciclotrabajo;
int IN1 = 0; // pin digital 8 de Arduino a IN1 de modulo controlador
int IN2 = 1; // pin digital 9 de Arduino a IN2 de modulo controlador
int IN3 = 12; // pin digital 10 de Arduino a IN3 de modulo controlador
int IN4 = 13; // pin digital 11 de Arduino a IN4 de modulo controlador
int pasos;
static const unsigned char PROGMEM image[] = {
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80, 0x01, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x00, 0x00, 0x0f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x1f, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x03, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x07, 0xf8, 0x3f, 0xe0, 0x00, 0x7f, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xf8, 0x00, 0x7f, 0xf0, 0x1f, 0xfe, 0x00, 0x3f, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xe0, 0x03, 0xff, 0xe0, 0x0f, 0xff, 0x80, 0x0f, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xc0, 0x0f, 0xff, 0xe0, 0x07, 0xff, 0xf0, 0x03, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0x80, 0x3f, 0xff, 0xc0, 0x07, 0xff, 0xf8, 0x01, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xfe, 0x00, 0x7f, 0xff, 0xc3, 0x83, 0xff, 0xfc, 0x00, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xfc, 0x00, 0x7f, 0xff, 0x87, 0xc3, 0xff, 0xfe, 0x00, 0x7f, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xf8, 0x00, 0xff, 0xff, 0x8f, 0xe1, 0xff, 0xfe, 0x00, 0x3f, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xf0, 0x00, 0x7f, 0xff, 0x8f, 0xf1, 0xff, 0xfe, 0x00, 0x1f, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xe0, 0x00, 0x7f, 0xff, 0x1f, 0xf1, 0xff, 0xfe, 0x04, 0x0f, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xe0, 0xe0, 0x3f, 0xff, 0x1f, 0xf8, 0xff, 0xfc, 0x06, 0x07, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xc0, 0xe0, 0x1f, 0xff, 0x3f, 0xf8, 0xff, 0xf8, 0x0f, 0x03, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xc1, 0xf0, 0x0f, 0xff, 0x3f, 0xf8, 0xff, 0xe0, 0x1f, 0x83, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0x83, 0xf8, 0x03, 0xfe, 0x3f, 0xf8, 0xff, 0x80, 0x3f, 0xc1, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0x83, 0xfe, 0x00, 0x3e, 0x3f, 0xfc, 0xfc, 0x00, 0x7f, 0xc1, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0x07, 0xff, 0x00, 0x00, 0x3f, 0xfc, 0x00, 0x01, 0xff, 0xc1, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0x07, 0xff, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x07, 0xff, 0xe0, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0x0f, 0xff, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x1f, 0xff, 0xe0, 0xff, 0xff, 0xff,
0xff, 0xff, 0xfe, 0x0f, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xe0, 0xff, 0xff, 0xff,
0xff, 0xff, 0xfe, 0x0f, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x3f, 0xff, 0xff, 0xe0, 0xff, 0xff, 0xff,
0xff, 0xff, 0xfe, 0x0f, 0xff, 0xff, 0xfe, 0x7f, 0xfc, 0x7f, 0xff, 0xff, 0xe0, 0xff, 0xff, 0xff,
0xff, 0xff, 0xfe, 0x0f, 0xff, 0xff, 0xfe, 0x7f, 0xfc, 0x7f, 0xff, 0xff, 0xe0, 0xff, 0xff, 0xff,
0xff, 0xff, 0xfe, 0x0f, 0xff, 0xff, 0xfe, 0x7f, 0xfc, 0x7f, 0xff, 0xff, 0xe0, 0xff, 0xff, 0xff,
0xff, 0xff, 0xfe, 0x0f, 0xff, 0xff, 0xfe, 0x7f, 0xfc, 0x7f, 0xff, 0xff, 0xe0, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0x0f, 0xff, 0xff, 0xfe, 0x7f, 0xfc, 0x7f, 0xff, 0xff, 0xe0, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0x07, 0xff, 0xff, 0xfe, 0x7f, 0xfc, 0xff, 0xff, 0xff, 0xe0, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0x07, 0xff, 0xff, 0xfe, 0x7f, 0xfc, 0xff, 0xff, 0xff, 0xc1, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0x83, 0xff, 0xff, 0xfe, 0x3f, 0xfc, 0xff, 0xff, 0xff, 0xc1, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0x83, 0xff, 0xff, 0xfe, 0x3f, 0xfc, 0xff, 0xff, 0xff, 0x81, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xc1, 0xff, 0xff, 0xff, 0x3f, 0xf8, 0xff, 0xff, 0xff, 0x83, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xc0, 0xff, 0xff, 0xff, 0x3f, 0xf8, 0xff, 0xff, 0xff, 0x07, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xe0, 0x7f, 0xff, 0xff, 0x1f, 0xf8, 0xff, 0xff, 0xfe, 0x07, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xf0, 0x7f, 0xff, 0xff, 0x1f, 0xf1, 0xff, 0xff, 0xfc, 0x0f, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xf0, 0x3f, 0xff, 0xff, 0x1f, 0xf1, 0xff, 0xff, 0xf8, 0x1f, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xf8, 0x0f, 0xff, 0xff, 0x8f, 0xf1, 0xff, 0xff, 0xf0, 0x3f, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xfc, 0x07, 0xff, 0xff, 0x87, 0xe1, 0xff, 0xff, 0xc0, 0x7f, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0x01, 0xff, 0xff, 0xc3, 0xc3, 0xff, 0xff, 0x80, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0x80, 0x7f, 0xff, 0xc0, 0x03, 0xff, 0xfe, 0x01, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xc0, 0x1f, 0xff, 0xe0, 0x07, 0xff, 0xf8, 0x03, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xf0, 0x07, 0xff, 0xe0, 0x0f, 0xff, 0xe0, 0x0f, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0xff, 0xf0, 0x0f, 0xff, 0x00, 0x3f, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x1f, 0xf8, 0x1f, 0xf8, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x00, 0xf8, 0x1e, 0x00, 0x03, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x1f, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x00, 0x00, 0x0f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x01, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xfc, 0x03, 0xe0, 0xf9, 0xfb, 0xf0, 0xf8, 0x03, 0xf7, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xf8, 0x01, 0xc0, 0x78, 0xf3, 0xc0, 0x30, 0x03, 0xc3, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0x0f, 0x8e, 0x38, 0xe3, 0x8e, 0x3f, 0x1f, 0xc1, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0x9f, 0x1f, 0x1c, 0x47, 0x1f, 0x1f, 0x3f, 0xc1, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0x9f, 0x3f, 0x9e, 0x0f, 0x1f, 0x9f, 0x3f, 0x89, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0x9f, 0x3f, 0x9e, 0x0f, 0x3f, 0x9f, 0x3f, 0x98, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0x9f, 0x3f, 0x9f, 0x1f, 0x3f, 0x9f, 0x3f, 0x9c, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0x9f, 0x3f, 0x9f, 0x1f, 0x1f, 0x9f, 0x3f, 0x00, 0x7f, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0x9f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x3f, 0x00, 0x7f, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0x9f, 0x8e, 0x1f, 0x1f, 0x8e, 0x1f, 0x3e, 0x3e, 0x7f, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0x9f, 0x80, 0x3f, 0x1f, 0x80, 0x3f, 0x3e, 0x7e, 0x3f, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0x9f, 0xe0, 0xff, 0x1f, 0xe0, 0x7f, 0x3e, 0x7f, 0x3f, 0xff, 0xff, 0xff};
void setup () {
// initialize serial port
digitalWrite(IN1,LOW);
digitalWrite(IN2,LOW);
digitalWrite(IN3,LOW);
digitalWrite(IN4,LOW);
Serial.begin(9600);
Wire.begin();
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
if(!oled.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}
oled.clearDisplay();
oled.drawBitmap(0, 0, image, IMAGE_WIDTH, IMAGE_HEIGHT, WHITE);
oled.display(); // Show the display buffer on the screen
delay(2000); // Pause for 2 seconds
pinMode(pin2,OUTPUT);
pinMode(pin7,OUTPUT);
pinMode(IN1,OUTPUT);
pinMode(IN2,OUTPUT);
pinMode(IN3,OUTPUT);
pinMode(IN4,OUTPUT);
digitalWrite(IN1,LOW);
digitalWrite(IN2,LOW);
digitalWrite(IN3,LOW);
digitalWrite(IN4,LOW);
Wire.begin(); // inicializa bus I2C
oled.begin(SSD1306_SWITCHCAPVCC, 0x3C);
intro_0(); // muestra el intro de bienvenida
}
////////////////////////// Void loop() ///////////////////////
void loop(){
pulsacion = Teclado1.getKey() ; // Lee la tecla pulsada
if (pulsacion != 0) {
oled.clearDisplay(); // limpia pantalla
delay(100);
}
if(contador == 4){ menu_4();accion_4();}
if(contador == 3){ menu_3();accion_3();}
if(contador == 2){ menu_2();accion_2();}
if(contador == 1){ menu_1();accion_1();}
} ///////// Fin del void loop(), inician las Funciones///////////////
/////////////////////Intro_0 //////////////////////////////
void intro_0(){
oled.clearDisplay(); // limpia pantalla
oled.setTextColor(WHITE); // establece color al unico disponible (pantalla monocromo)
oled.setCursor(0, 0); // ubica cursor en inicio de coordenadas 0,0
oled.setTextSize(2); // establece tamano de texto en 1
oled.print("Bienvenido"); // escribe en pantalla el texto
oled.setCursor(0, 20); // ubica cursor en inicio de coordenadas 0,0
oled.print("Menu"); // escribe en pantalla el texto
oled.display(); // muestra
delay(1000);
}
/////////////////////Menu_1 //////////////////////////////////
void menu_1(){
oled.clearDisplay(); // limpia pantalla
oled.setTextColor(WHITE); // establece color al unico disponible (pantalla monocromo)
oled.setCursor(0, 0); // ubica cursor en inicio de coordenadas 0,0
oled.setTextSize(2); // establece tamano de texto en 1
oled.print("STP1>1"); // escribe en pantalla el texto
oled.setCursor(60, 0); // ubica cursor en inicio de coordenadas 0,0
oled.print("STP2>2"); // escribe en pantalla el texto
oled.setCursor(0, 20); // ubica cursor en inicio de coordenadas 0,0
oled.print("STP3>3"); // escribe en pantalla el texto
oled.setCursor(60, 20); // ubica cursor en inicio de coordenadas 0,0
oled.print("MAS>*"); // escribe en pantalla el texto
oled.display(); // muestra
digitalWrite(11,LOW);
digitalWrite(10,LOW);
digitalWrite(IN1,LOW);
digitalWrite(IN2,LOW);
digitalWrite(IN3,LOW);
digitalWrite(IN4,LOW);
}
/////////////////////Accion 1 //////////////////////////////
void accion_1(){
if(pulsacion == '1') digitalWrite(0,LOW);
if(pulsacion == '2') digitalWrite(1,LOW);
if(pulsacion == '3') intermitente();
if(pulsacion == '*') contador=2;
}
/////////////////////Menu_2 //////////////////////////////////
void menu_2(){
oled.clearDisplay(); // limpia pantalla
oled.setTextColor(WHITE); // establece color al unico disponible (pantalla monocromo)
oled.setCursor(0, 0); // ubica cursor en inicio de coordenadas 0,0
oled.setTextSize(2); // establece tamano de texto en 1
oled.print("PWM>1"); // escribe en pantalla el texto
oled.setCursor(60, 0); // ubica cursor en inicio de coordenadas 0,0
oled.print("Auto>2"); // escribe en pantalla el texto
oled.setCursor(0, 20); // ubica cursor en inicio de coordenadas 0,0
oled.print("MENU>#"); // escribe en pantalla el texto
oled.display(); // muestra
}
/////////////////////accion_2 //////////////////////////////////
void accion_2(){
if(pulsacion == '1') contador=3;
if(pulsacion == '2') contador=4;
if(pulsacion == '#') contador=1;
}
/////////////////////Menu_3 //////////////////////////////////
void menu_3(){
valorpote=analogRead(pote);
TS1=analogRead(tp1);
TS2=analogRead(tp2);
//Como la entrada analógica del Arduino es de 10 bits, el rango va de 0 a 1023.
//En cambio, la salidas del Arduio son de 8 bits, quiere decir, rango entre 0 a 255.
//Por esta razón tenemos que mapear el número de un rango a otro usando este código.
pwm1 = map(valorpote, 0, 1023, 250, 0);
TPS1 = map(TS1, 0, 1023, 5000, 0);
TPS2 = map(TS2, 0, 1023, 5000, 0);
pwm2 = map(valorpote, 0, 1023, 255, 0); //El PWM 2 esta invertido respecto al PWM 1
ciclotrabajo = map(pwm1, 250, 0, 100, 0);
//Sacamos el PWM de las dos salidas usando analogWrite(pin,valor)
analogWrite(pin2,pwm1);
analogWrite(pin7,pwm2);
oled.clearDisplay(); // limpia pantalla
oled.setTextColor(WHITE); // establece color al unico disponible (pantalla monocromo)
oled.setCursor(0, 0); // ubica cursor en inicio de coordenadas 0,0
oled.setTextSize(1); // establece tamano de texto en 1
oled.print("Acelerador motor"); // escribe en pantalla el texto
oled.setCursor (0, 10); // ubica cursor en coordenas 10,30
oled.print("Ciclo");
oled.setCursor (0, 30); // ubica cursor en coordenas 10,30
oled.print("TPS1");
oled.setCursor (0, 50); // ubica cursor en coordenas 10,30
oled.print("TPS2");
oled.setCursor (25, 10); // ubica cursor en coordenas 10,30
oled.setTextSize(2); // establece tamano de texto en 2
oled.print(ciclotrabajo); // escribe texto
oled.print("%"); // escribe texto
oled.display(); // muestra en pantalla todo lo establecido anteriormente
oled.setCursor (25, 30);
oled.setTextSize(2);
oled.print(TPS1);
oled.print("mV");
oled.display();
oled.setCursor (25, 50);
oled.setTextSize(2);
oled.print(TPS2);
oled.print("mV");
oled.display();
}
/////////////////////accion_3 //////////////////////////////////
void accion_3(){
if(pulsacion == '#') contador=1;
if(pulsacion == '0') contador=2;
}
/////////////////////Menu_4 //////////////////////////////////
void menu_4(){
unsigned long tiempo1 = (millis()/1000);
oled.clearDisplay(); // limpia pantalla
oled.setTextColor(WHITE); // establece color al unico disponible (pantalla monocromo)
oled.setCursor(0, 0); // ubica cursor en inicio de coordenadas 0,0
oled.setTextSize(2); // establece tamano de texto en 1
oled.print("Time"); // escribe en pantalla el texto
oled.setCursor(60, 0); // ubica cursor en inicio de coordenadas 0,0
oled.print(tiempo1); // escribe en pantalla el texto
oled.setCursor(0, 20); // ubica cursor en inicio de coordenadas 0,0
oled.print("MENU>#"); // escribe en pantalla el texto
oled.display(); // muestra
}
/////////////////////accion_4 //////////////////////////////////
void accion_4(){
if(pulsacion == '#') contador=1;
if(pulsacion == '0') contador=2;
}
/////////////////////intermitente //////////////////////////////////
void intermitente(){
for (int i = 0; i < 512; i++) // 512*4 = 2048 pasos
{
valorpote=analogRead(pote);
pasos = map(valorpote, 0, 1023, 0, 250);
digitalWrite(IN1, HIGH); // paso 1
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
delay(pasos);
digitalWrite(IN1, LOW); // paso 2
digitalWrite(IN2, HIGH);
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
delay(pasos);
digitalWrite(IN1, LOW); // paso 3
digitalWrite(IN2, LOW);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
delay(pasos);
digitalWrite(IN1, LOW); // paso 4
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
delay(pasos);
}
}