Skip to content

Instantly share code, notes, and snippets.

@jaydp17
Created August 16, 2013 15:44
Show Gist options
  • Save jaydp17/6251008 to your computer and use it in GitHub Desktop.
Save jaydp17/6251008 to your computer and use it in GitHub Desktop.
/*
Code developed by Team 7, B.Tech 2011, DA-IICT, India
for making controller for operating in arduino project on TicTacToe
Please feel free to download, use and share
Cheers !!
*/
int j = 1; // integer used in scanning the array designating column number
//2-dimensional array for asigning the buttons and there high and low values
// 1 = Left
// 2 = TOP
// 3 = RIGHT
// 4 = DOWN
// 5 = OK
// 6 = SWITCH MODE
int Button[6][3] = {{1, 805, 838}, // button 1
{2, 720, 750}, // button 2
{3, 560, 611}, // button 3
{4, 260, 300}, // button 4
{5, 150, 179}, // button 5
{6, 60, 90}}; // button 6
int analogpin = A0;// analog pin to read the buttons
int label = 0; // for reporting the button label
int counter = 0; // how many times we have seen new value
long time = 0; // the last time the output pin was sampled
int debounce_count = 100; // number of millis/samples to consider before declaring a debounced input
int current_state = 0; // the debounced input value
int ButtonVal;
void setup()
{
Serial.begin(9600);
}
void loop()
{
// If we have gone on to the next millisecond
if (millis() != time)
{
// check analog pin for the button value and save it to ButtonVal
ButtonVal = analogRead(analogpin);
Serial.println(ButtonVal);
if(ButtonVal == current_state && counter >0)
{
counter--;
}
if(ButtonVal != current_state)
{
counter++;
}
// If ButtonVal has shown the same value for long enough let's switch it
if (counter >= debounce_count)
{
counter = 0;
current_state = ButtonVal;
//Checks which button has been pressed
if (ButtonVal > 0)
{
ButtonCheck();
}
}
time = millis();
}
}
void ButtonCheck()
{
// loop for scanning the button array.
for(int i = 0; i <= 6; i++)
{
// checks the ButtonVal against the high and low vales in the array
if(ButtonVal >= Button[i][j] && ButtonVal <= Button[i][j+1])
{
// stores the button number to a variable
label = Button[i][0];
Action();
}
}
}
void Action()
{
if(label == 1)
{
Serial.println("LEFT");
}
if(label == 2)
{
Serial.println("TOP");
}
if(label == 3)
{
Serial.println("Right");
}
if(label == 4)
{
Serial.println("DOWN");
}
if(label == 5)
{
Serial.println("OK");
}
if(label == 6)
{
Serial.println("MODE");
}
//Serial.println("Button =:");
//Serial.println(label);
//delay(200);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment