Joystick availabilty at any point of the code

Hi

Im working with a 5110 lcd and some sensors that provide information. (DHT22, engine crankcase oilcap termistor, etc.)
Information is provided in several "screens" and the movement among them is selected by a joystick.

Im a begginer so for my mental healthness, I will build the proyect (that my first idea) based on secuences. One secuence for each screen.

void setup()
void secuence1()
void secuence2()
...
void loop()
secuence1()

At this very moment at the end of each secuence there is a code that indicates by the joystick movement which is the secuence to move next.

Cons:

  • I repeat almost the same code at the end of each secuence.
  • While secuence is working the joystick is not responsive.

The second point is what now concerns me. Could you give any idea on how to make the joystick "available" at any time? Just an idea and I will work on it.

Please find the code below.

Thanks

//*******************************************librerias********************

#include <math.h>
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_PCD8544.h>
#include "DHT.h"

// Software SPI (slower updates, more flexible pin options):
// pin 7 - Serial clock out (SCLK)
// pin 6 - Serial data out (DIN)
// pin 5 - Data/Command select (D/C)
// pin 4 - LCD chip select (CS)
// pin 3 - LCD reset (RST)


//****************************************DEFINICIÓN DE VARIABLES Y PINES

#define DHTPIN 2  
#define DHTTYPE DHT22

Adafruit_PCD8544 display = Adafruit_PCD8544(7, 6, 5, 4, 3); 
#define NUMFLAKES 10
#define XPOS 0
#define YPOS 1
#define DELTAY 2
DHT dht(DHTPIN, DHTTYPE);
 int RGBb = 9; // led rgb azul
 int RGBg =10; // led rgb verde
 int RGBr = 11; // led rgb rojo
 
//********************************************Variables del joystick********************
 long ejejoysty = A1;
 long ejejoystx = A2;
 int valorjoystx =0;// 

 
void setup()   {
               
  pinMode (ejejoystx, INPUT);
  pinMode (ejejoysty, INPUT);
  
  
  Serial.begin(9600);
  display.begin();
  display.setContrast(60);
  dht.begin();
  
  display.display(); // show splashscreen
  delay(2000);  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  display.clearDisplay();   // clears the screen and buffer
  // Reading temperature or humidity takes about 250 milliseconds!

  display.setTextSize(1);
  display.setTextColor(BLACK);
  display.setCursor(0,20);
  delay(2500);
}

  float Termistor() {
  float Temp;
  Temp = log(((1024L*10000L/analogRead(0)) - 10000L));
  Temp = 1 / (0.001606355783 + (0.0002632458140 * Temp) + (-0.0000001658963196 * Temp * Temp * Temp));
  Temp = Temp - 273.15;           // Convert Kelvin to Celcius
  return Temp;
  }





//********************************************************SECUENCIAS************************
 //********************************************************************************************
 
 void secuencia1temphumedad(){ //******************************SECUENCIA  1*******secuencia1temphumedad**************************
  for (int m=0;m<100;m++){
  float h = dht.readHumidity();
  // Read temperature as Celsius (the default)
  float t = dht.readTemperature();
  // Read temperature as Fahrenheit (isFahrenheit = true)
  //float f = dht.readTemperature(true);
  delay(4000);
  display.clearDisplay();
  // text display tests
  display.setTextSize(1);
  display.setTextColor(BLACK);
 
  display.setCursor(0,10);
  display.print("Temp: ");
  display.print(t);
  display.println("C");
  display.print("H%:   ");
  display.print(h);
  display.println("%");
  Serial.print(analogRead(0));   //********************** comprobar R y Tª
  Serial.println ("      Comprobacion R Termistor");
  Serial.print(Termistor(),2);   //Comprobar T Motor
  Serial.println ("      Comprobacion T Motor");  //------------comprobar R y Tª
  display.print("Motor:");
  display.print(Termistor(),2);
  display.println("C");
  display.setCursor(50,0);
  display.print(m);  
  display.display();  
  
//******Si la temperatura es inferior hay riesgo de hielo (parpadeo de Led azul)
  
  if (Termistor(),2 <  25.50)  {        
    for (int a=0;a<=253;a++) 
 {
  analogWrite (RGBb,a);
  delay(4);
  }
  for (int a=254;a>=0;a--) 
  {
analogWrite (RGBb,a);
delay(3);
}
delay(2000);
//*****************************************************comprobar movimiento Joystick******
valorjoystx = analogRead (ejejoystx);  //valor del eje x de 0 a 1023
Serial.print(valorjoystx); // para comprobar en serial monitor que los valores del eje se mueven
Serial.println("       valor ejex");  // para comprobar en serial monitor que los valores del eje se mueven
Serial.println();

// ahora planteo que el eje se mueva a un lado o a otro
// A derecha

 if(valorjoystx<=200){// caso contrario, si el joystick lo muevo al inicio de su recorrido los leds se encienden en sentido contrario
 analogWrite (11,254); // se enciende el led rojo para indicar que sale de secuencia1temphumedad
 delay(500);
 digitalWrite (11,0);
   display.clearDisplay();
  display.display();
 secuencia2motor();   //************************************* SECUENCIA   2*************secuencia2motor*****
}
} 
}
 }
 
 void secuencia2motor(){
    for (int m=0;m<100;m++){
  Serial.println ("          secuencia2");
  analogWrite (10,254);   // lez verde para indicar que ha llegado a secuencia2motor
  delay(500);
  digitalWrite (10,0);
  display.clearDisplay();
  // text display tests
  display.setTextSize(1);
  display.setTextColor(BLACK);
 
  display.setCursor(0,10);
  Serial.print(analogRead(0));   //********************** comprobar R y Tª
  Serial.println ("      Comprobacion R Termistor");
  Serial.print(Termistor(),2);   //Comprobar T Motor
  Serial.println ("      Comprobacion T Motor");  //------------comprobar R y Tª
  display.print("Motor:");
  display.print(Termistor(),2);
  display.println("C");
  display.setCursor(50,0);
  display.print(m);  
  display.display();  
  delay(1000);

 //*****************************************************comprobar movimiento Joystick******

 
 Serial.print(valorjoystx); // para comprobar en serial monitor que los valores del eje se mueven
Serial.println("       valor ejex--------------");  // para comprobar en serial monitor que los valores del eje se mueven   
valorjoystx = analogRead (ejejoystx);  
   if(valorjoystx>=1000) {
Serial.print(valorjoystx); // para comprobar en serial monitor que los valores del eje se mueven
Serial.println("       valor ejex--------------");  // para comprobar en serial monitor que los valores del eje se mueven
       
    for (int a=0;a<=253;a++) 
 {
  analogWrite (RGBb,a);
  delay(4);
  }
  for (int a=254;a>=0;a--) 
  {
analogWrite (RGBb,a);
delay(3);
//*------------
}
delay(2000);
secuencia1temphumedad();
  }
  }
 }

 
void loop() {

secuencia1temphumedad();
  

}

Get rid of all delays (changing your logic dramatically), read the joystick at the top of the loop.

It's as easy as that.

The demo Several Things at a Time includes code for a potentiometer that can be moved at any time. It should provide a model for your joystick system. Note how the code is designed as separate small functions.

The demo also illustrates the use of millis() to manage timing without blocking.

The code in Planning and Implementing a Program is a more extended example of all this.

...R

Thanks a lot

I'll have a look at them