martes, 28 de diciembre de 2010

Arduino POTENCIOMETRO COMO SELECTOR DE MENU

He desarrollado este código para el tres en raya que estoy diseñando. No he descubierto nada nuevo pero me ha costado unas vueltas.
Te permite moverte con un potenciómetro por las opciones predefinidas, redondeando los valores del potenciómetro en torno a una cantidad de números que tu estipules (en mi caso 9, que son las celdas del juego) y un botón de ok que guarda la posición del potenciómetro.

Os lo dejo para lo que os pueda servir. En concreto creo que es genial para moverse por cualquier tipo de menú.

Salud!


/*
************************************************************
* POTENCIOMETER AND "OK BUTTON" TO MOVE AROUND THE OPTIONS *
************************************************************

This demo, show via 128x64 LCD (GLCD 3 beta library) and Serial Monitor
the value of the potenciometer between 9 numbers (0 to 8).

With a potenciometer and a button, you can move around a menu, and select the
option that you want.

Hardware needed:
- push button in digitalpin 12
- 128x64 LCD with glcd library or Serial Monitor
- Potenciometer in analogPin 0

This code is in the public domain.
Code by Hombrelobo, Timosoft Team. www.mundobocata.com
*/
//#include // for LCD
//#include "fonts/Arial14.h" // for LCD
//#include "fonts/SystemFont5x7.h" // for LCD

int val = analogRead(0);
int election;

void setup() {
Serial.begin(9600);
// GLCD.Init(NON_INVERTED); // for LCD
// GLCD.SelectFont(System5x7); // for LCD
// GLCD.ClearScreen(); // for LCD
pinMode(digitalRead(12), INPUT);
}

void loop()
{
int val = analogRead(0);
val = map(val, 0, 1023, 0, 8); // Map an analog value to 9 numbers ( 0 to 8)
Serial.println ((long)val);
// GLCD.CursorTo(0,4); // for LCD
// GLCD.Puts("Estate= position "); // for LCD
// GLCD.PrintNumber(val); // for LCD
// GLCD.CursorTo(0,6); // for LCD
// GLCD.Puts("Last Option="); // for LCD
// GLCD.PrintNumber(election); // for LCD
if (digitalRead(12)==HIGH) {
election=val;
}
else{
}
}

No hay comentarios:

Publicar un comentario