I am S-L-O-W-L-Y getting up to speed on Arduino coding, so please bear with my naive questions.
I am creating a project that consists of a TFT Touchscreen, Arduino Mega 2560, and some RGB LEDs. For this test, I am using a single RGB LED.
The screen display and On and an Off button. When the user taps On, text displays and the RGB LED should pulse by looping through fading down and up. When the user taps Off, text should disappear and the LED should turn off.
I am using the SimpleTimer function to handle the LED pulsing, but it isn't working correctly. When Off is tapped, the LED turns off. When On is tapped, the LED turns on, but doesn't fade out and in; instead it just stays bright for just under 2 seconds, turns off for just under 2 seconds, etc.
It appears that the "level" value being sent to AnalogWrite doesn't increment/decrement, but running debugging shows that is actually changing values.
I have attached the code in hopes someone might shed some light on what I am doing wrong.
I have sort of mashed a few different sketches together, but am not sure what I missed...
#include "TFTLCD.h"
#include "TouchScreen.h"
#include "SimpleTimer.h"
/* For the 8 data pins:
Duemilanove/Diecimila/UNO/etc ('168 and '328 chips) microcontoller:
D0 connects to digital 8
D1 connects to digital 9
D2 connects to digital 2
D3 connects to digital 3
D4 connects to digital 4
D5 connects to digital 5
D6 connects to digital 6
D7 connects to digital 7
For Mega's use pins 22 thru 29 (on the double header at the end)
*/
#define YP A3 // must be an analog pin, use "An" notation!
#define XM A2 // must be an analog pin, use "An" notation!
#define YM 9 // can be a digital pin
#define XP 8 // can be a digital pin
#define TS_MINX 150
#define TS_MINY 120
#define TS_MAXX 920
#define TS_MAXY 940
// For better pressure precision, we need to know the resistance
// between X+ and X- Use any multimeter to read it
// For the one we're using, its 300 ohms across the X plate
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);
#define LCD_CS A3
#define LCD_CD A2
#define LCD_WR A1
#define LCD_RD A0
// optional
#define LCD_RESET A4
// Color definitions
#define BLACK 0x0000
#define BLUE 0x001F
#define RED 0xF800
#define GREEN 0x07E0
#define CYAN 0x07FF
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF
#define TESTCOLOR 0x1CB6
TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);
// INITIALIZE OUR TIMERS
SimpleTimer timer;
// INITIALIZE THE PINS FOR THE TEST LED
int level; //value to send
int ledpinR = 51; // Red channel
int ledpinG = 52; // Green channel
int ledpinB = 53; // Blue channel
// INITIAL OUR TRACKING BOOLEANS
boolean ledActive = false;
boolean bCountDown = false;
void setup(void) {
Serial.begin(9600);
Serial.println("Paint!");
tft.setRotation(1);
tft.reset();
uint16_t identifier = tft.readRegister(0x0);
if (identifier == 0x9325) {
Serial.println("Found ILI9325");
} else if (identifier == 0x9328) {
Serial.println("Found ILI9328");
} else {
Serial.print("Unknown driver chip ");
Serial.println(identifier, HEX);
while (1);
}
tft.initDisplay();
tft.fillScreen(BLACK);
// DRAW TEXT AND BUTTONS
tft.drawString(40, 0, "THE MAN", RED, 4);
tft.drawRect(40, 200, 80, 40, WHITE);
tft.drawRect(200, 200, 80, 40, WHITE);
tft.drawString(70, 220, "ON", WHITE, 1);
tft.drawString(230, 220, "OFF", WHITE, 1);
// KICK OFF THE TIMER
level = 255;
timer.setInterval(5, ledAction );
pinMode(13, OUTPUT);
}
#define MINPRESSURE 10
#define MAXPRESSURE 1000
void loop()
{
// RUN THE TIMER
timer.run();
digitalWrite(13, HIGH);
Point p = ts.getPoint();
digitalWrite(13, LOW);
// if you're sharing pins, you'll need to fix the directions of the touchscreen pins!
//pinMode(XP, OUTPUT);
pinMode(XM, OUTPUT);
pinMode(YP, OUTPUT);
//pinMode(YM, OUTPUT);
// we have some minimum pressure we consider 'valid'
// pressure of 0 means no pressing!
if (p.z > MINPRESSURE && p.z < MAXPRESSURE) {
// turn from 0->1023 to tft.width
p.x = map(p.x, TS_MINX, TS_MAXX, 240, 0);
p.y = map(p.y, TS_MINY, TS_MAXY, 320, 0);
/* DEBUGGING STUFF
Serial.print("X = "); Serial.print(p.x);
Serial.print("\tY = "); Serial.print(p.y);
Serial.print("\tPressure = "); Serial.println(p.z);
*/
if (p.y > 40 && p.y < 120 && p.x > 0 && p.x < 40) {
// ON BUTTON HAS BEEN PRESSED
tft.drawString(40, 0, "THE MAN", RED, 4); // DISPLAY THE TEXT
ledActive = true; // TOGGLE THE INDICATOR TO TURN ON LEDS
}
if (p.y > 200 && p.y < 280 && p.x > 0 && p.x < 40) {
// OFF BUTTON HAS BEEN PRESSED
tft.fillRect(40, 0, 300, 40, BLACK); // CLEAR OUT THE TEXT
ledActive = false; // TOGGLE THE INDICATOR TO TURN OFF LEDS
}
}
}
void ledAction()
{
int pin = ledpinB; // TEMPORARY DEBUGGING STEP - ALWAYS DEFAULT TO RED LED
// CHECK TO SEE IF THE LED SHOULD BE LIT OR NOT
if ( ledActive == true )
{
// NOW FIGURE OUT WHAT TO DO WITH THE LEVEL VALUE
if ( bCountDown == false )
{
// WE ARE COUNTING UP, INCREMENT OUR COUNTER
level = level + 1;
// CHECK TO MAKE SURE WE ARE WITHIN RANGE
if ( level >= 255 )
{
// WE HAVE HIT THE CEILING, SO TOGGLE THE BOOLEAN
bCountDown = true;
}
}
else
{
// WE ARE COUNTING DOWN, DECREMENT OUR COUNTER
level = level -1 ;
// CHECK TO MAKE SURE WE ARE WITHIN RANGE
if ( level <= 0 )
{
// WE HAVE HIT THE FLOOR, SO TOGGLE THE BOOLEAN
bCountDown = false;
}
}
}
else
// TURN OFF THE LED
{
level = 255;
}
Serial.print("\t Level is: " );Serial.print(level);Serial.println();
analogWrite(pin, level);
}