Grumpy_Mike:
So with one button the code increments a variable each time it is pressed and that variable controls the light pattern. So if you have four buttons just make each of the detected push ( or touch ) set that variable to a fixed value.
I'm lost!
I just replaced your digitalRead(button); with a Boolean "touched" and flipped it inside of the touch button code in order to replicate the high/low of a button press.
Here is the sketch:
#include <UTFT.h>
#include <URTouch.h>
#include <Adafruit_NeoPixel.h>
#define PINforControl 22 // pin connected to the small NeoPixels strip
#define NUMPIXELS1 12 // number of LEDs on second strip
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUMPIXELS1, PINforControl, NEO_GRB + NEO_KHZ800);
UTFT myGLCD(CPLD, 27, 26, 25, 28); // CTE DUE
URTouch myTouch(6, 5, 32, 3, 2); //byte tclk, byte tcs, byte din, byte dout, byte irq CTE DUE
extern uint8_t Blooper[];
extern uint8_t BigFont[];
int x, y;
unsigned long patternInterval = 30 ; // time between steps in the pattern
unsigned long lastUpdate = 0 ; // for millis() when last update occoured
unsigned long intervals [] = { 0, 20, 20, 50, 100 } ; // speed for each pattern
const byte button = 11; // pin to connect button switch to between pin and ground
boolean touched = false;
void setup() {
strip.begin();
wipe(); // wipes the LED buffers
myGLCD.InitLCD(); //Initialize Screen
myGLCD.clrScr(); //Clear Screen
myGLCD.setColor(VGA_BLACK); // Set font color here
myGLCD.setBackColor(VGA_LIME); // Set font backcolor here
myTouch.InitTouch();
myTouch.setPrecision(PREC_MEDIUM);
ButtonDraw();
}
void loop() {
buttonPone();
buttonPtwo();
static int pattern = 0, lastReading;
int reading = touched;
if (lastReading == true && reading == false) {
pattern++ ; // change pattern number
if (pattern > 4) pattern = 0; // wrap round if too big
patternInterval = intervals[pattern]; // set speed for this pattern
wipe(); // clear out the buffer
delay(125);
}
lastReading = reading; // save for next time
if (millis() - lastUpdate > patternInterval) updatePattern(pattern);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////
void updatePattern(int pat) { // call the pattern currently being created
switch (pat) {
case 0:
wipe(); // clear out the buffer
break;
case 1:
colorWipe(strip.Color(0, 0, 255)); // BLUE
break;
case 2:
colorWipe(strip.Color(0, 255, 0)); // GREEN
break;
case 3:
colorWipe(strip.Color(255, 0, 0)); // RED
break;
case 4:
rainbow();
break;
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////
void ButtonDraw() {
myGLCD.setFont(Blooper);
myGLCD.setBackColor(VGA_BLACK); // Set back color of Font
myGLCD.setColor(VGA_WHITE); // Set Color of Font
myGLCD.setFont(BigFont); // Set Font
myGLCD.drawRect(250, 190, 350, 290); // Button P1
myGLCD.print("LED P1", 253, 233);
myGLCD.drawRect(450, 190, 550, 290); // Button P2
myGLCD.print("LED P2", 453, 233);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////
void buttonPone() {
if (myTouch.dataAvailable()) {
myTouch.read();
x = myTouch.getX();
y = myTouch.getY();
// (x1, y1, x2, y2)
// (250, 190, 350, 290)
if ((y >= 190) && (y <= 290)) // Upper row
{
if ((x >= 250) && (x <= 350)) // top left LED P1
{
touched = !touched;
}
}
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////
void buttonPtwo() {
if (myTouch.dataAvailable()) {
myTouch.read();
x = myTouch.getX();
y = myTouch.getY();
// (x1, y1, x2, y2)
// (450, 190, 550, 290)
if ((y >= 190) && (y <= 290)) // Upper row
{
if ((x >= 450) && (x <= 550)) // top left LED P1
{
touched = !touched;
//TouchIt(350, 190, 450, 290);
}
}
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////
void TouchIt(int x1, int y1, int x2, int y2)
{
myGLCD.setColor(VGA_RED);
myGLCD.drawRoundRect (x1, y1, x2, y2);
while (myTouch.dataAvailable())
myTouch.read();
myGLCD.setColor(VGA_WHITE);
myGLCD.drawRoundRect (x1, y1, x2, y2);
touched = !touched;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////
void rainbow() { // modified from Adafruit example to make it a state machine
static uint16_t j = 0;
for (int i = 0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, Wheel((i + j) & 255));
}
strip.show();
j++;
if (j >= 256) j = 0;
lastUpdate = millis(); // time for next change to the display
}
/////////////////////////////////////////////////////////////////////////////////////////////////////
void rainbowCycle() { // modified from Adafruit example to make it a state machine
static uint16_t j = 0;
for (int i = 0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
}
strip.show();
j++;
if (j >= 256 * 5) j = 0;
lastUpdate = millis(); // time for next change to the display
}
/////////////////////////////////////////////////////////////////////////////////////////////////////
void theaterChaseRainbow() { // modified from Adafruit example to make it a state machine
static int j = 0, q = 0;
static boolean on = true;
// for (int j=0; j < 256; j++) { // cycle all 256 colors in the wheel
// for (int q=0; q < 3; q++) {
if (on) {
for (int i = 0; i < strip.numPixels(); i = i + 3) {
strip.setPixelColor(i + q, Wheel( (i + j) % 255)); //turn every third pixel on
}
}
else {
for (int i = 0; i < strip.numPixels(); i = i + 3) {
strip.setPixelColor(i + q, 0); //turn every third pixel off
}
}
on = !on; // toggel pixelse on or off for next time
strip.show(); // display
q++; // update the q variable
if (q >= 3 ) { // if it overflows reset it and update the J variable
q = 0;
j++;
if (j >= 256) j = 0;
}
lastUpdate = millis(); // time for next change to the display
}
/////////////////////////////////////////////////////////////////////////////////////////////////////
void colorWipe(uint32_t c) { // modified from Adafruit example to make it a state machine
static int i = 0;
strip.setPixelColor(i, c);
strip.show();
i++;
if (i >= strip.numPixels()) {
i = 0;
wipe(); // blank out strip
}
lastUpdate = millis(); // time for next change to the display
}
/////////////////////////////////////////////////////////////////////////////////////////////////////
void wipe() { // clear all LEDs
for (int i = 0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, strip.Color(0, 0, 0));
}
strip.show();
}
/////////////////////////////////////////////////////////////////////////////////////////////////////
uint32_t Wheel(byte WheelPos) {
WheelPos = 255 - WheelPos;
if (WheelPos < 85) {
return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
}
if (WheelPos < 170) {
WheelPos -= 85;
return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
}
WheelPos -= 170;
return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}
///////////////////////////////////////////////////////////////////////////////////////////////////
I am not sure how to do what you suggest with a Boolean?
~Dennis