Voting resources, early voting, and poll worker information - VOTE. ... Adafruit is open and shipping.
0

Servo function vs 1.8 TFT Shield
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Servo function vs 1.8 TFT Shield

by tjhawkey on Fri Jun 05, 2020 9:45 pm

I am working on a digital servo tester. I have an Uno R3 an a 1.8" TFT shield with Joystick.
I use the joystick/buttons on the tft shield to set and display the servo pulse width using the arduino servo library.
Mostly it works great. But, the servo output "skips".
The obvious answer is to add some capacitance on the 5V or provide separate servo power.
I've done the former (no joy), but not the latter yet. Note--this behavior happens even if I hook up the output to a servo display (no real current draw). It acts about the same with a servo attached, so the theory of power drop outs doesn't quite match what I see. Same behavior with 9V or 12V barrel jack iput

Digging around the code I hacked together, I discovered that if I remove the lines that write the outputs to the display every loop, it works perfectly (though it's not so useful this way). The static text works fine.
The code is included below.

Any suggestions? I could of course get a different display, but the TFT is really nice.
thx!


Code: Select all | TOGGLE FULL SIZE
/***************************************************
  This is an example sketch for the Adafruit 1.8" TFT shield with joystick
  This example is for the Seesaw version
  ----> http://www.adafruit.com/products/802

  Check out the links above for our tutorials and wiring diagrams
  These displays use SPI to communicate, 4 pins are required to
  interface
  One pin is also needed for the joystick, we use analog 3
  Adafruit invests time and resources providing this open source code,
  please support Adafruit and open-source hardware by purchasing
  products from Adafruit!

  Written by Limor Fried/Ladyada for Adafruit Industries.
  MIT license, all text above must be included in any redistribution
 ****************************************************/

#include <SPI.h>
#include <SD.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ST7735.h>
// Be sure to install Adafruit seesaw library!
#include <Adafruit_seesaw.h>
#include <Adafruit_TFTShield18.h>
#include <Servo.h>

Adafruit_TFTShield18 ss;
Servo myservo;

// TFT display and SD card will share the hardware SPI interface.
// Hardware SPI pins are specific to the Arduino board type and
// cannot be remapped to alternate pins.  For Arduino Uno,
// Duemilanove, etc., pin 11 = MOSI, pin 12 = MISO, pin 13 = SCK.
#define SD_CS    4  // Chip select line for SD card on Shield
#define TFT_CS  10  // Chip select line for TFT display on Shield
#define TFT_DC   8  // Data/command line for TFT on Shield
#define TFT_RST  -1  // Reset line for TFT is handled by seesaw!

Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_RST);
float pulse_width = 1500.0;
float angle = (pulse_width-500.0)*180.0/2000.0;
float command = (pulse_width-500.0)/2000.0;

void setup(void) {
  myservo.attach(9,500,2500);
  myservo.writeMicroseconds(pulse_width);  // set servo to mid-point

  Serial.begin(9600);
 
  // start by disabling both SD and TFT
  pinMode(TFT_CS, OUTPUT);
  digitalWrite(TFT_CS, HIGH);
  pinMode(SD_CS, OUTPUT);
  digitalWrite(SD_CS, HIGH);

  // Start seesaw helper chip
  if (!ss.begin()){
    Serial.println("seesaw could not be initialized!");
    while(1);
  }
  Serial.println("seesaw started");
  Serial.print("Version: "); Serial.println(ss.getVersion(), HEX);

  // Start set the backlight off
  ss.setBacklight(TFTSHIELD_BACKLIGHT_OFF);
  // Reset the TFT
  ss.tftReset();

  // Initialize 1.8" TFT
  tft.initR(INITR_BLACKTAB);   // initialize a ST7735S chip, black tab

  Serial.println("TFT OK!");
  tft.fillScreen(ST77XX_CYAN);


  // Set backlight on fully
  // ss.setBacklight(TFTSHIELD_BACKLIGHT_ON);
  // Or you can set the backlight one third on
  // ss.setBacklight(TFTSHIELD_BACKLIGHT_ON / 3);
  // Or dim it up
  for (int32_t i=TFTSHIELD_BACKLIGHT_OFF; i<TFTSHIELD_BACKLIGHT_ON; i+=10) {
    ss.setBacklight(i);
    delay(1);
  }
  delay(100);
  tft.fillScreen(ST77XX_RED);
  delay(100);
  tft.fillScreen(ST77XX_GREEN);
  delay(100);
  tft.fillScreen(ST77XX_BLUE);
  delay(100);
  tft.fillScreen(ST77XX_BLACK);
 
  tft.setRotation(1);

  tft.setTextSize(2);
  tft.setTextColor(ST77XX_BLUE);
  tft.setCursor(0, 0);
  tft.print("Servo Tester");

  tft.setTextSize(2);
  tft.setTextColor(ST77XX_WHITE);
  tft.setCursor(0, 25);
  tft.print("Command");

  tft.setTextColor(ST77XX_RED);
  tft.setTextSize(2);
  tft.setCursor(100,50);
  tft.print("micS");

  tft.setTextColor(ST77XX_YELLOW);
  tft.setTextSize(2);
  tft.setCursor(100,75);
  tft.print("deg");
   
  tft.setTextColor(ST77XX_GREEN);
  tft.setTextSize(2);
  tft.setCursor(100,100);
  tft.print("Code");

}

void loop() {
   
  uint32_t buttons = ss.readButtons();

  if(! (buttons & TFTSHIELD_BUTTON_DOWN)&& (pulse_width > 500.0)){
    pulse_width = pulse_width - 50.0;
  }
  if(! (buttons & TFTSHIELD_BUTTON_LEFT)&& (pulse_width > 500.0)){
    pulse_width = pulse_width - 50.0;
  }
  if(! (buttons & TFTSHIELD_BUTTON_UP)&& (pulse_width <2500.0)){
    pulse_width = pulse_width + 50.0;
  }
  if(! (buttons & TFTSHIELD_BUTTON_RIGHT)&& (pulse_width <2500.0)){
    pulse_width = pulse_width + 50.0;
  }
  if(! (buttons & TFTSHIELD_BUTTON_1)){
    pulse_width = 500;
  }
  if(! (buttons & TFTSHIELD_BUTTON_2)){
    pulse_width = 1500;
  }
  if(! (buttons & TFTSHIELD_BUTTON_3)){
    pulse_width = 2500;
  }
  if (! (buttons & TFTSHIELD_BUTTON_IN)) {
    pulse_width = 1500;
  }

    delay(100);
    myservo.writeMicroseconds(pulse_width);
   
    angle = (pulse_width-500.0)*180.0/2000.0;
    command = (pulse_width-500.0)/2000.0;
   
    tft.setTextColor(ST77XX_RED,ST77XX_BLACK);
    tft.setTextSize(2);
    tft.setCursor(0, 50);
    tft.print(pulse_width);

    tft.setTextColor(ST77XX_YELLOW,ST77XX_BLACK);
    tft.setTextSize(2);
    tft.setCursor(0, 75);
    tft.print(angle);
   
    tft.setTextColor(ST77XX_GREEN,ST77XX_BLACK);
    tft.setTextSize(2);
    tft.setCursor(0, 100);
    tft.print(command);

}

tjhawkey
 
Posts: 13
Joined: Thu Aug 10, 2017 10:23 pm

Re: Servo function vs 1.8 TFT Shield

by adafruit_support_bill on Sat Jun 06, 2020 6:14 am

I suspect what you are seeing is not a power problem, but an interruption if the servo control signal due to interrupt contention during communication with the display.

I'd try adding some logic to only update the display when there has been an actual change in the pulse width.

adafruit_support_bill
 
Posts: 78735
Joined: Sat Feb 07, 2009 10:11 am

Re: Servo function vs 1.8 TFT Shield

by tjhawkey on Sat Jun 06, 2020 9:34 am

Thanks! That works great.

tjhawkey
 
Posts: 13
Joined: Thu Aug 10, 2017 10:23 pm

Please be positive and constructive with your questions and comments.