Using touchscreen to simultaneously 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 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);
      }
    }
  }
}

If you are trying to power the servo from the 5V pin, that is the problem.

Power motors and servos independently, and connect all the grounds.

your functional description is talking about a touch-screen and your code has the touch-screen and a additional mechanical button.

Do you want the trouch-screen to toggle between led1/2 and clockwise / counterclockwise rotation?
or

Do you want the mechanical button to toggle between led1/2 and clockwise / counterclockwise rotation?

I'm powering the servo using 4 C batteries. I measured the voltage today, it's about 4 V. Could it be I need to buy new batteries?

If your referring to the button pin, that's for the touchscreen.

Yes, I want the touch-screen to toggle between led1/2 and clockwise/counterclockwise rotation.

You should look up the maximum current the servo can draw and then buy a power-supply that can deliver 5V and this maximum current.

If the servo rotates the batteries are still OK.
To cross-test this use a small testcode t hat does nothing more than
rotate servo clockwise for 3 seconds the stop for a second
then rotate servo counterclockwise for 3 seconds

I'll try that out later today. Hopefully, this works. Thank you.

you have this codeline

which seems to be used to detect the touch of your finger on the screen.

So why the heck do you need to read in a mechanical button???

Does your touchscreen have an additional output-pin that is connected to IO-pin named buttonPin??

Most servos are rated for 4.8V minimum. So yes, buy new batteries.

The touchscreen takes up most of the pins on the Arduino mega( from reset to A5 and 0 to AREF. Pin 13 being one of them is what controls what I want. In this case, the LEDs and the servo.

this does not answer my question

to ask in a different whay what does the line

do?

what does the line

do

To your first question, that's for the sensitivity of the touch screen. When I apply a certain force with my finger the arduino considers it a touch.

To your second question, I'm setting my button pin which 13 equal to button state so when I tap the touchscreen the button will be high or low which in turn will execute the code depending whether high or low.

You are still not getting it that you have to write in a way more

precise

way

"I'm setting my button pin which 13 equal to button state" this is gramatically so wrong that I don't understand it.

I will not speculate about the meaning
tips how to improve your posting style

The button pin is equal is 13. Button state is what I choose to name when the touchscreen is pressed. ''buttonState = digitalRead(buttonPin);'' is reading whether the pin is high or low. If the button state is high then the buttonpushscounter value increases by 1.

I hope that helps clear up some things.

I guess this is huge misunderstanding how the C++-language on arduino works
and a huge misunderstanding how electronics work

The function digitalRead() delivers the result of an Arduino-IO-pin
totally regardless of what any kind of touchscreen is doing.
This means there is abolutely no relation between your touch-screen and digitalRead.

You should learn some really really basic things about electricity, electronics and logic before trying to going on with programming microcontrollers.

Again you still have not

taken to your heart

the advice of reading

which would teach you that you have to provide detailed information about

  • hardware that you are using
  • datasheets of all hardware components
  • schematics how you have things wired together

in your case providing this information is 10 times more important as your knowledge about electronics is so low.

e

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