Instantly share code, notes, and snippets.
Created
December 8, 2011 22:05
-
Star
0
(0)
You must be signed in to star a gist -
Fork
0
(0)
You must be signed in to fork a gist
-
Save arduinoboard/1448864 to your computer and use it in GitHub Desktop.
The file that is currently on an Arduino Uno with a serial number of 02000002
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Created by WilliamK @ Wusik Dot Com (c) 2010 | |
http://arduino.wusik.com | |
*/ | |
/* SparkFun Keypad Example Code | |
by: Jim Lindblom | |
SparkFun Electronics | |
date: 4/26/11 | |
license: Creative Commons Share-Alike attribution v3.0 | |
This example code will quickly get you up and running with the | |
SparkFun U/D/L/R/Flame Keypad. Pressing the flame button will | |
turn on/off the red LED. Up and down adjust the brightness of | |
the LED. And left and right are just there to look pretty will | |
output their status via the serial monitor. | |
Multiple touches are not supported in this code. | |
The circuit: | |
SparkFun Keypad Arduino | |
----------------------------------------- | |
Wire 1 (NC) ------------- No connection | |
Wire 2 (LEDR) ----------------- D3 | |
Wire 3 (GND) ------------------GND | |
Wire 4 (P5.1) ----------------- D5 | |
Wire 5 (P5.3) ----------------- D6 | |
Wire 6 (P5.2) ----------------- D7 | |
*/ | |
#include <WProgram.h> | |
#include "Encoder.h" | |
//// Pin definitions | |
//keypad | |
int LEDpin = 4; | |
int p51 = 3; | |
int p53 = 2; | |
int p52 = 15; | |
//encoders | |
Encoder BlackEncoder = Encoder(13,12,0); | |
Encoder ClearEncoder = Encoder(6,5,7); | |
//// Global variables | |
//keypad | |
int button = 0; | |
int ledLevel = 127; | |
int onstatus = 1; | |
//encoders | |
float encoderData = 0.0f; | |
int sliderData=0; | |
int sliderTmp[4]; | |
int i=0; | |
void setup() | |
{ | |
Serial.begin(38400); | |
//keypad | |
pinMode(LEDpin, OUTPUT); | |
pinMode(p51, INPUT); | |
pinMode(p53, INPUT); | |
pinMode(p52, INPUT); | |
digitalWrite(p51, HIGH); | |
digitalWrite(p52, HIGH); | |
digitalWrite(p53, HIGH); | |
//analogWrite(LEDpin, ledLevel); | |
digitalWrite(LEDpin, HIGH); | |
//encoders | |
BlackEncoder.setMinMax(1,10000); | |
BlackEncoder.setPosition(5000); | |
BlackEncoder.setRate(0.25f); | |
BlackEncoder.setIntegerMode(false); | |
ClearEncoder.setMinMax(1,10000); | |
ClearEncoder.setPosition(5000); | |
ClearEncoder.setRate(1.0f); | |
ClearEncoder.setIntegerMode(true); | |
} | |
void loop() | |
{ | |
button = getButtonState(); // Get button status | |
if (button == 0x04) // FLAME | |
{ | |
onstatus = onstatus ^ 1; // flip on/off status of LED | |
delay(1); // "debounce" | |
while (getButtonState() == 0x04) | |
; // Wait for them to stop pressing flame | |
} | |
else if (button == 0x02) // UP | |
{ | |
Serial.println("up"); | |
} | |
else if (button == 0x01) // DOWN | |
{ | |
Serial.println("down"); | |
} | |
else if (button == 0x08) // RIGHT | |
{ | |
Serial.println("right"); | |
} | |
else if (button == 0x10) // LEFT | |
{ | |
Serial.println("left"); | |
} | |
// To turn the LED on or not to turn the LED on, that's the question | |
if (onstatus) | |
digitalWrite(LEDpin, HIGH); | |
else | |
digitalWrite(LEDpin, LOW); | |
i++; | |
if (BlackEncoder.tick()) | |
{ | |
encoderData = BlackEncoder.getPosition(); | |
Serial.print("> Black Position: "); | |
Serial.println(encoderData); | |
if (BlackEncoder.hasClick()) Serial.println("! Black Click !"); | |
if (BlackEncoder.onClickHold()) { | |
Serial.println(" ... Black Holding ..."); | |
delay(500); | |
} | |
} | |
if (ClearEncoder.tick()) | |
{ | |
encoderData = ClearEncoder.getPosition(); | |
Serial.print("> Clear Position: "); | |
Serial.println(encoderData); | |
if (ClearEncoder.hasClick()) Serial.println("! Clear Click !"); | |
if (ClearEncoder.onClickHold()) { | |
Serial.println(" ... Clear Holding ..."); | |
delay(500); | |
} | |
} | |
i=i%4; | |
sliderTmp[i]=analogRead(0); | |
if (sliderTmp[i]<(((sliderTmp[0]+sliderTmp[1]+sliderTmp[2]+sliderTmp[3])/4)-1) || sliderTmp[i]>(((sliderTmp[0]+sliderTmp[1]+sliderTmp[2]+sliderTmp[3])/4)+1)){ | |
sliderData=sliderTmp[i]; | |
Serial.println(sliderData); | |
} | |
} | |
uint8_t getButtonState() | |
{ | |
// Initially set all buttons as inputs, and pull them up | |
pinMode(p52, INPUT); | |
digitalWrite(p52, HIGH); | |
pinMode(p51, INPUT); | |
digitalWrite(p51, HIGH); | |
pinMode(p53, INPUT); | |
digitalWrite(p53, HIGH); | |
// Read the d/u/flame buttons | |
if (!digitalRead(p53)) | |
return 0x01; // Down | |
if (!digitalRead(p52)) | |
return 0x02; // Up | |
if (!digitalRead(p51)) | |
return 0x04; // Flame | |
// Read right button | |
pinMode(p52, OUTPUT); // set p52 to output, set low | |
digitalWrite(p52, LOW); | |
if (!digitalRead(p53)) | |
return 0x08; // Right | |
pinMode(p52, INPUT); // set p52 back to input and pull-up | |
digitalWrite(p52, HIGH); | |
// Read left button | |
pinMode(p51, OUTPUT); // Set p51 to output and low | |
digitalWrite(p51, LOW); | |
if (!digitalRead(p53)) | |
return 0x10; // Left | |
pinMode(p51, INPUT); // Set p51 back to input and pull-up | |
pinMode(p51, HIGH); | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment