Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Menu de opciones para Arduino

El fin de semana me solicitaron ayuda para crear un menú de opciones para Arduino, la idea es usar un teclado de entrada (en este caso un keypad 4×4) y permitir ingresar una opción y a continuación una cantidad; el código es bastante fácil pero muchas veces se tiene problemas para expresarlo dentro del método loop. Dado que este parece ser un problema común les comparto el código de ejemplo, quizás le pueda servir a alguien más.

#include 
 
const byte ROWS = 4;
const byte COLS = 4;
 
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 };
byte colPins[COLS] = { 5, 4, 3, 2 };
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
char key, option;
int quantity;
bool flag;
 
void setup() {
	Serial.begin(9600);
	Serial.print("Iniciando menu...");
}
 
void loop() {
	quantity = 0;
	Serial.println("Seleccione una opcion (A-D):");
	flag = true;
	while (flag) {
		key = keypad.getKey();
		if (key) {
			switch (key)
			{
			case 'A':
			case 'B':
			case 'C':
			case 'D':
				Serial.println(key);
				option = key;
				flag = false;
				break;
			default:
				break;
			}
		}
	}
	Serial.println("Ingrese una cantidad :");
	Serial.println("(Presione '#' para finalizar)");
	flag = true;
	while (flag) {
		key = keypad.getKey();
		if (key) {
			switch (key)
			{
			case '0':
			case '1':
			case '2':
			case '3':
			case '4':
			case '5':
			case '6':
			case '7':
			case '8':
			case '9':
				Serial.print(key);
				quantity = quantity * 10 + key - '0';
				break;
			case '#':
				Serial.println();
				flag = false;
			default:
				break;
			}
		}
	}
}

Hasta la próxima!
–Rp

Share the post

Menu de opciones para Arduino

×

Subscribe to Msdn Blogs | Get The Latest Information, Insights, Announcements, And News From Microsoft Experts And Developers In The Msdn Blogs.

Get updates delivered right to your inbox!

Thank you for your subscription

×