So basically i am trying to control a servo motor with buttons and display the angle of the motor with ST7735 display. Problem that occured is that the display updates so slowly (1 per second or so) And the motor cant run when display is updating it. Is there any way to make it faster? Or is the arduino nano just too slow for the project? Also the servo motor runs really fast without the screen.
Heres the code im using to run the motor and the screen.
#include <Servo.h>
#include <Adafruit_GFX.h> // Core graphics library
#include <Adafruit_ST7735.h> // Hardware-specific library
#include <SPI.h>
#define TFT_CS 10
#define TFT_RST 8
#define TFT_DC 9
#define TFT_SCLK 13
#define TFT_MOSI 11
Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_MOSI, TFT_SCLK, TFT_RST);
Servo servo; // create servo object to control a servo
// variables will not change:
const int BUTTON_PIN = 4; // Arduino pin connected to button's pin
const int BUTTON_PIN2 = 5; // Arduino pin connected to button's pin
const int SERVO_PIN = 6; // Arduino pin connected to servo motor's pin
// variables will change:
int angle = 0; // the current angle of servo motor
void setup() {
Serial.begin(9600);
servo.attach(SERVO_PIN);
Serial.print("servo attached\n");
servo.write(angle);
analogWrite(3, 155); //control screen brightness
pinMode(BUTTON_PIN, INPUT_PULLUP);
pinMode(BUTTON_PIN2, INPUT_PULLUP);
tft.initR(INITR_BLACKTAB);
tft.fillScreen(ST7735_BLACK);
tft.setTextSize(2);
tft.println("Hello World!");
delay(200);
Serial.println("Initialized");
}
void loop() {
if (digitalRead(BUTTON_PIN) == LOW) {
Serial.print("Button1\n");
angle++;
}
if (digitalRead(BUTTON_PIN2) == LOW) {
Serial.print("Button2\n");
angle--;
}
angle = constrain(angle, 0, 180); //clamp the angle
servo.write(angle);
Serial.println(angle);
//Draw the angle of the servo motor
tft.fillScreen(ST7735_BLACK);
tft.setTextSize(2);
tft.setCursor(50, 50);
tft.println(angle);
}