Hi,
I'm using an Arduino Mega and Touchscreen to control two LEDs and a servo. What I want to happen is I upload the code, the servo and leds are off. When I touch the touchscreen, ledpin1 turns on while ledpin2 stays off and the servo turns clockwise. When I touch the touchscreen a second time, ledpin1 turns off while ledpin2 turns on and the servo turns counterclockwise and so on.
What actually happens is that I upload the code both LEDS and servo are off. I tap the touch screen and ledpin1 turns on while ledpin2 stays off and the servo turns clockwise. So far so good. I click a second time and nothing happens. ledpin1 is still on and the servo is still in the same position.
I do want to use "state" method in the loop code.
Any help would be greatly appreciated.
#include <LCDWIKI_GUI.h> //libraries are below
#include <LCDWIKI_KBV.h>
#include <Adafruit_GFX.h>
#include <Adafruit_TFTLCD.h>
#include <TouchScreen.h>
#include <MCUFRIEND_kbv.h>
int LFT = 115, RT = 908, TOP = 964, BOT = 80; //calibration of the four corners of the touchscreen
LCDWIKI_KBV mylcd(ILI9486, A3, A2, A1, A0, A4);
#define BLACK 0x0000 //colors for touchscreen
#define NAVY 0x000F
#define DARKGREEN 0x03E0
#define DARKCYAN 0x03EF
#define MAROON 0x7800
#define PURPLE 0x780F
#define OLIVE 0x7BE0
#define LIGHTGREY 0xC618
#define DARKGREY 0x7BEF
#define BLUE 0x001F
#define GREEN 0x07E0
#define CYAN 0x07FF
#define RED 0xF800
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF
#define ORANGE 0xFD20
#define GREENYELLOW 0xAFE5
#define PINK 0xF81F
#define YP A3
#define XM A2
#define YM 9
#define XP 8
MCUFRIEND_kbv tft;
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);
#include <Servo.h>
Servo myservo1;
const int buttonPin = 13;//Touchscreen
const int servoPin1 = 49;
const int ledPin1 = 52;
const int ledPin2 = 53;
boolean oldSwitchState = LOW;
boolean newSwitchState = LOW;
byte state = 0;
void setup() {
pinMode(buttonPin, INPUT); //Touchscreen
digitalWrite(13, LOW); //Touchscreen
pinMode(ledPin1, OUTPUT);
digitalWrite(ledPin1, LOW);
pinMode(ledPin2, OUTPUT);
digitalWrite(ledPin2, LOW);
Serial.begin(9600);
Serial1.begin(9600);
pinMode(servoPin1, OUTPUT); //RF RECEIVER
digitalWrite(servoPin1, LOW); //RF RECEIVER
myservo1.attach(49);
myservo1.writeMicroseconds(1490);
tft.begin(0x9486); //Touchscreen button setup
Serial.print("TFT size is "); Serial.print(tft.width()); Serial.print("x"); Serial.println(tft.height());
tft.setRotation(2);
tft.fillScreen(WHITE);
tft.fillCircle(160, 240, 60, 0x7BEF);
}
void loop() {
touchScreenLoopCode();
}
void touchScreenLoopCode() {
int button_on = 0;
int x, y;
TSPoint p = ts.getPoint();
if (p.z > ts.pressureThreshhold) {
//Portrait Calibration
x = map(p.x, LFT = 115, RT = 908, 0, 320);
y = map(p.y, TOP = 964, BOT = 80, 0, 480);
if (x > 100 && x < 220 && y > 180 && y < 300 ) {
digitalWrite(13, HIGH);
newSwitchState = digitalRead(buttonPin);
if ( newSwitchState != oldSwitchState )
{
if (newSwitchState == HIGH )
{
state++;
delay(50);
}
if (state > 2) {
state = 1;
}
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
myservo1.writeMicroseconds(1490);
if (state == 1) {
digitalWrite(ledPin1, HIGH);
digitalWrite(ledPin2, LOW);
myservo1.writeMicroseconds(1000);
delay(1000);
myservo1.writeMicroseconds(1490);
}
if (state == 2) {
digitalWrite(ledPin2, HIGH);
digitalWrite(ledPin1, LOW);
myservo1.writeMicroseconds(2000);
delay(1000);
myservo1.writeMicroseconds(1490);
}
}
oldSwitchState = newSwitchState;
}
}
}