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 the leds part works as intended but the servo doesn't. The code is uploaded and the servo turns clockwise indefinitely (it doesn't matter whether I touch the screen or not).
I've already centered the servo btw.
Any help would be greatly appreciated.
#include <LCDWIKI_GUI.h>
#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;
LCDWIKI_KBV mylcd(ILI9486, A3, A2, A1, A0, A4);
#define BLACK 0x0000
#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;
const int servoPin1 = 49;
const int ledPin1 = 52;
const int ledPin2 = 53;
int buttonPushCounter = 0;
int buttonState = 0;
int lastButtonState = 0;
void setup() {
pinMode(buttonPin, INPUT);
digitalWrite(13, LOW);
pinMode(ledPin1, OUTPUT);
digitalWrite(ledPin1, LOW);
pinMode(ledPin2, OUTPUT);
digitalWrite(ledPin2, LOW);
Serial.begin(9600);
Serial1.begin(9600);
myservo1.attach(49);
myservo1.writeMicroseconds(1490);
tft.begin(0x9486);
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(void) {
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);
buttonState = digitalRead(buttonPin);
if (buttonState != lastButtonState) {
if (buttonState == HIGH) {
buttonPushCounter++;
delay(100);
}
}
if (buttonPushCounter == 3)buttonPushCounter = 1;
if (buttonPushCounter == 2) {
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, HIGH);
myservo1.writeMicroseconds(2000);
delay(1000);
myservo1.writeMicroseconds(1490);
}
if (buttonPushCounter == 1) {
digitalWrite(ledPin1, HIGH);
digitalWrite(ledPin2, LOW);
myservo1.writeMicroseconds(1000);
delay(1000);
myservo1.writeMicroseconds(1490);
}
while (digitalRead(buttonPin) == LOW) {
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
myservo1.writeMicroseconds(1490);
}
}
}
}