Hi, I'm trying to control a servo with a Pushbutton, RF Receiver, and Touchscreen. These 3 will work interchangeably. The servo should move clockwise and then counterclockwise with each press of either one of the 3 methods. The touchscreen and the push button work flawlessly together. When I use the RF Receiver before or after any of the methods, the issue occurs.
I press the RF Receiver button once and the servo moves clockwise infinitely or the servo moves counterclockwise infinitely, depending on the other 2 methods last button press (I don't want it to move infinitely). Click again and the servo moves counterclockwise for the 1000 milliseconds it's supposed to. I've tried changing the braces({ }) and oldSwitchState = newSwitchState; by adding them or removing them from the code, but this is the closest I've gotten it to work.
This is the code where I have all three methods:
#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 buttonPin1 = 42; //RF RECEIVER
const int buttonPin2 = 27; //Push Button
const int servoPin1 = 49;
const int ledPin1 = 52;
const int ledPin2 = 53;
boolean oldSwitchState = LOW; //Keeping this for the RF Receiver since it seems like it won't work without it.
boolean newSwitchState = LOW;
byte state = 0;
void setup() {
pinMode(buttonPin, INPUT); //Touchscreen
pinMode(buttonPin1, INPUT_PULLUP); //RF RECEIVER
pinMode(buttonPin2, INPUT_PULLUP); //Push Button
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();
rFReceiverCode();
pushButtonCode();
}
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); // read the pushbutton input pin:
if (newSwitchState == HIGH ) // if the current state is HIGH then the button went from off to on
{
state++; // increment the counter
delay(50); // Delay a little bit to avoid bouncing
}
if (state > 2) {
state = 1;
}
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);
}
}
}
}
void rFReceiverCode() {
newSwitchState = digitalRead(buttonPin1); // read the pushbutton input pin:
if ( newSwitchState != oldSwitchState ) // compare the buttonState to its previous state
{
if (newSwitchState == HIGH ) // if the current state is HIGH then the button went from off to on
delay(50); // Delay a little bit to avoid bouncing
state++; //increment the counter
{
if (state > 2) {
state = 1;
}
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; // save the current state as the last state, for next time through the loop
}
}
void pushButtonCode() {
newSwitchState = digitalRead(buttonPin2); // read the pushbutton input pin:
if ( newSwitchState != oldSwitchState ) // compare the buttonState to its previous state
{
if (newSwitchState == HIGH ) // if the current state is HIGH then the button went from off to on
state++; //increment the counter
delay(50); // Delay a little bit to avoid bouncing
// Had to put it under state++ in order for it to work
{
if (state > 2) {
state = 1;
}
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);
}
}
}
}
This is the code where I have the touchscreen and the Pushbutton working flawlessly:
#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 buttonPin2 = 27; //Push Button
const int servoPin1 = 49;
const int ledPin1 = 52;
const int ledPin2 = 53;
boolean oldSwitchState = LOW; //Keeping this for the RF Receiver since it seems like it won't work without it.
boolean newSwitchState = LOW;
byte state = 0;
void setup() {
pinMode(buttonPin, INPUT); //Touchscreen
pinMode(buttonPin2, INPUT_PULLUP); //Push Button
digitalWrite(13, LOW); //Touchscreen
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); //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();
pushButtonCode();
}
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); // read the pushbutton input pin:
if (newSwitchState == HIGH ) // if the current state is HIGH then the button went from off to on
{
state++; // increment the counter
delay(50); // Delay a little bit to avoid bouncing
}
if (state > 2) {
state = 1;
}
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);
}
}
}
}
void pushButtonCode() {
newSwitchState = digitalRead(buttonPin2); // read the pushbutton input pin:
if ( newSwitchState != oldSwitchState ) // compare the buttonState to its previous state
{
if (newSwitchState == HIGH ) // if the current state is HIGH then the button went from off to on
state++; //increment the counter
delay(50); // Delay a little bit to avoid bouncing
// Had to put it under state++ in order for it to work
{
if (state > 2) {
state = 1;
}
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);
}
}
}
}
This is the code where I have the touchscreen and the RF Reciever working flawlessly:
#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 buttonPin1 = 42; //RF RECEIVER
const int servoPin1 = 49;
const int ledPin1 = 52;
const int ledPin2 = 53;
boolean oldSwitchState = LOW; //Keeping this for the RF Receiver since it seems like it won't work without it.
boolean newSwitchState = LOW;
byte state = 0;
void setup() {
pinMode(buttonPin, INPUT); //Touchscreen
pinMode(buttonPin1, INPUT_PULLUP); //RF RECEIVER
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();
rFReceiverCode();
}
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); // read the pushbutton input pin:
if (newSwitchState == HIGH ) // if the current state is HIGH then the button went from off to on
{
state++; // increment the counter
delay(50); // Delay a little bit to avoid bouncing
}
if (state > 2) {
state = 1;
}
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);
}
}
}
}
void rFReceiverCode() {
newSwitchState = digitalRead(buttonPin1); // read the pushbutton input pin:
if ( newSwitchState != oldSwitchState ) // compare the buttonState to its previous state
{
if (newSwitchState == HIGH ) // if the current state is HIGH then the button went from off to on
delay(50); // Delay a little bit to avoid bouncing
state++; //increment the counter
{
if (state > 2) {
state = 1;
}
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; // save the current state as the last state, for next time through the loop
}
}
This is the code where I have the RF Reciever working flawlessly by itself:
#include <Servo.h>
Servo myservo1;
const int buttonPin1 = 42; //RF RECEIVER
const int servoPin1 = 49;
const int ledPin1 = 52;
const int ledPin2 = 53;
boolean oldSwitchState = LOW; //Keeping this for the RF Receiver since it seems like it won't work without it.
boolean newSwitchState = LOW;
byte state = 0;
void setup() {
pinMode(buttonPin1, INPUT_PULLUP); //RF RECEIVER
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);
}
void loop() {
rFReceiverCode();
}
void rFReceiverCode() {
newSwitchState = digitalRead(buttonPin1); // read the pushbutton input pin:
if ( newSwitchState != oldSwitchState ) // compare the buttonState to its previous state
{
if (newSwitchState == HIGH ) // if the current state is HIGH then the button went from off to on
delay(50); // Delay a little bit to avoid bouncing
state++; //increment the counter
{
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; // save the current state as the last state, for next time through the loop
}
}
Is 2 loop codes in the void loop the max amount? Is 3 too many?
I don't know what else to do. Any help would be greatly appreciated.