Using Touchscreen to control LEDs and Servo

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;
    }
  }
}

only in case your mechanical button is pressed

you increase the variable state
touchung the touch-screen is only a pre-condition for the button

I have the impression that you took some ready to use code
did a quick reading of this code without really understanding how this code works
and started to modify the code.

As you can see now. Trying to be quick slows you down.
You should start from a small easy to understand code and work up from there

writing

is unclear to me. You are using a state-variable. Do you want the described functionality but without a variable named "state"?

This says just what you don't want. What do you want instead?
best regards Stefan

The method that I'm using in the loop code is what I want to use. I don't want to use a different method for the counter.

I was just letting know those who read my post.

This is the reason why your code does not work as expected:

Ok, so what would you suggest I do?

Take a look into this tutorial:

Arduino Programming Course

It is easy to understand and has a good mixture between explaining important concepts and example-codes to get you going. So give it a try and report your opinion about this tutorial.

best regards Stefan

I read through it and it did a good job of explaining the concepts.

Looking back at my code I still don't know what's wrong with the part that you mentioned. When the touchscreen is pressed, the new switch state becomes high which then increases the state by 1. So what would you suggest I modify in my code?

I suggest that you learn more about which line of code is doing what.

You have to learn more basics.
digitalRead()

Video digitalRead()

If you expect that somebody will write line by line the code for you
not me!

If you expect that I will write the 101th tutorial about real basics customised to your project: not me

So far it seems that you have taken some other code made some modifications without even understanding the most basic things and are nwo asking for
"which letter on the keyboard shall I press next to modify the code."

As I understand this forum it is help for selfhelp
If you were able to ask a

specific

question
instead of generalised questions like

you will get helpful answers.

To give you a generalised answer to your question

Remove reading in the hardware-button and adapt the logic to use the touch-button

If you don't know yet what you have to do
Go and write down a much more specific question where this question is related to a single line of code

best regards Stefan

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.