Start a new topic

Slow reaction to any input.

// This sketch verifies, uploads and works on my nextion 2.8, but it is very slow to react. it takes over 2 seconds to turn on a LED, energize 2 relays and the scrolling text is slower than when I ran it by itself.


any advise on how to speed this up would be appreciated.


//Libraries
#include <SoftwareSerial.h>
#include <Nextion.h>
#include <Wire.h>
#include <Servo.h>
#include <LiquidCrystal_I2C.h>
#include <IRremote.h>

//Relay
const int relayaPin = 8;  //MEGA pin 8
const int relaybPin = 9;  //MEGA pin 9

//LED
const int RED_PIN = 2;  //MEGA pin 2
const int GREEN_PIN = 3;  //MEGA pin 3
const int BLUE_PIN = 4;  //MEGA pin 4

//Servo
int ServoHorizontalPin = 12;  //Servo Wire to MEGA pin 12 
int ServoVerticalPin =  13;  //Servo Wire to MEGA pin 13
int HorizontalPotPin = A0;  //Joystick X wite to MEGA pin A0
int VerticalPotPin = A1;  //Joystick Y wite to MEGA pin A1
int ServoH_Min = 0; 
int ServoH_Max = 180;
int ServoV_Min = 0;
int ServoV_Max = 180;
int HorizontalPotValue; 
int HorizontalServoPosition; 
int VerticalPotValue; 
int VerticalServoPosition;
Servo HorizontalServo; 
Servo VerticalServo;

//Nextion Code
SoftwareSerial nextion(10, 11);// Nextion TX to pin 10 and RX to pin 11 of Arduino
Nextion myNextion(nextion, 9600); //create a Nextion object named myNextion using the nextion serial port @ 9600bps

//LCD
LiquidCrystal_I2C lcd(0x27,16,2);  // set the LCD address to 0x27 for a 16 chars and 2 line display

//Joystick
const int JoystickkeyPin = 52; //the number of the pin from Joystick Button
const int JoystickledPin = 50; //the number of the pin to LED/Laser

//IRremote
const int irReceiverPin =A7; //the SIG of receiver module attach to pin47
const int irReceiverledPin = 39;//pin 39 built-in led
IRrecv irrecv(irReceiverPin); //Creates a variable of type IRrecv
decode_results results;

void setup() {
  Serial.begin(9600);
  myNextion.init(); // send the initialization commands for Page 0

/****************Relay********************************************************/
pinMode(relayaPin, OUTPUT);
pinMode(relaybPin, OUTPUT);

/****************LED**********************************************************/
pinMode(RED_PIN, OUTPUT);
pinMode(GREEN_PIN, OUTPUT);
pinMode(BLUE_PIN, OUTPUT);

// Off (all LEDs off):
digitalWrite(RED_PIN, HIGH);
digitalWrite(GREEN_PIN, HIGH);
digitalWrite(BLUE_PIN, HIGH);

/****************Servo*******************************************/
HorizontalServo.attach(ServoHorizontalPin); 
VerticalServo.attach(ServoVerticalPin); 

/****************LCD*******************************************/
  lcd.init();  // initialize the lcd
  lcd.init();
  // Print a message to the LCD.
  lcd.backlight();
  lcd.setCursor(1,0);
  lcd.print("Anthony Rotter");
  lcd.setCursor(18,3); //0,3
  lcd.print("Arduino Project!");
  delay(1000);

/****************Joystick Button*************************************************************/
  pinMode(JoystickkeyPin,INPUT);//initialize the key pin as input from Joystick Button
  pinMode(JoystickledPin,OUTPUT);//initialize the led pin as output from Joystick Button
 
/****************IRreceiver*************************************************************/
  pinMode(irReceiverledPin,OUTPUT);//set ledpin as OUTPUT
  irrecv.enableIRIn(); //enable ir receiver module
}
void loop()
{

  String message = myNextion.listen(); //check for message
 
/*****LED*****************************************************/
/****************RED*******************************************/
  if (message == "70 nodered~togbt0=1")
  {
// Red (turn just the red LED on):
 digitalWrite(RED_PIN, LOW);
 digitalWrite(GREEN_PIN, HIGH);
 digitalWrite(BLUE_PIN, HIGH);
  }
  if (message == "70 nodered~togbt0=0")
  {
// Red (turn just the red LED off):
 digitalWrite(RED_PIN, HIGH);
 digitalWrite(GREEN_PIN, HIGH);
 digitalWrite(BLUE_PIN, HIGH);
  }
/****************Green*******************************************/ 
  if (message == "70 nodered~togbt1=1")
  {
// Red (turn just the red LED on):
 digitalWrite(RED_PIN, HIGH);
 digitalWrite(GREEN_PIN, LOW);
 digitalWrite(BLUE_PIN, HIGH);
  }
  if (message == "70 nodered~togbt1=0")
  {
// Red (turn just the red LED off):
 digitalWrite(RED_PIN, HIGH);
 digitalWrite(GREEN_PIN, HIGH);
 digitalWrite(BLUE_PIN, HIGH);
  }
/****************BLUE*******************************************/ 
  if (message == "70 nodered~togbt2=1")
  {
// Red (turn just the red LED on):
 digitalWrite(RED_PIN, HIGH);
 digitalWrite(GREEN_PIN, HIGH);
 digitalWrite(BLUE_PIN, LOW);
  }
  if (message == "70 nodered~togbt2=0")
  {
// Red (turn just the red LED off):
 digitalWrite(RED_PIN, HIGH);
 digitalWrite(GREEN_PIN, HIGH);
 digitalWrite(BLUE_PIN, HIGH);
  }
/****************YELLOW*******************************************/ 
  if (message == "70 nodered~togbt3=1")
  {
// Red (turn just the red LED on):
 digitalWrite(RED_PIN, LOW);
 digitalWrite(GREEN_PIN, LOW);
 digitalWrite(BLUE_PIN, HIGH);
  }
  if (message == "70 nodered~togbt3=0")
  {
// Red (turn just the red LED off):
 digitalWrite(RED_PIN, HIGH);
 digitalWrite(GREEN_PIN, HIGH);
 digitalWrite(BLUE_PIN, HIGH);
  }
/*****RELAY a*****************************************************/
  if (message == "70 nodered~togbt4=1")
  {
 digitalWrite(relayaPin, HIGH);
  }
  if (message == "70 nodered~togbt4=0")
  {
 digitalWrite(relayaPin, LOW);
  }
  /*********************************************************/
  if (message == "70 2x~relay1ab=1")
  {
  digitalWrite(relayaPin, HIGH);
  }
  if (message == "70 2x~relay1ab=3")
  {
  digitalWrite(relayaPin, LOW);
  }
/*****RELAY b*****************************************************/
  if (message == "70 nodered~togbt5=1")
  {
 digitalWrite(relaybPin, HIGH);
  }
  if (message == "70 nodered~togbt5=0")
  {
 digitalWrite(relaybPin, LOW);
  }
  /*********************************************************/
  if (message == "70 2x~relay1ab=0")
  {
  digitalWrite(relaybPin, HIGH);
  }
  if (message == "70 2x~relay1ab=2")
  {
  digitalWrite(relaybPin, LOW);
  }
/*****SERVO CONTROL************************************************/

// letsarduino.com
// 2 Servos Using a Joystick 

  HorizontalPotValue  = analogRead(HorizontalPotPin);
  VerticalPotValue  = analogRead(VerticalPotPin); 
  HorizontalServoPosition  = map(HorizontalPotValue, 0, 1023, ServoH_Min , ServoH_Max);
  VerticalServoPosition  = map(VerticalPotValue, 0, 1023, ServoH_Min , ServoH_Max); 
  HorizontalServo.write(HorizontalServoPosition); 
  VerticalServo.write(VerticalServoPosition); 
  delay(20);
 
// Joystick Button 
boolean Value=digitalRead(JoystickkeyPin);//read the state of the key value
  //and check if the kye is pressed
  //if it is,the state is HIGH
  if(Value ==HIGH )
  {
  digitalWrite(JoystickledPin,HIGH);//turn on the led
  }
  else
  {
  digitalWrite(JoystickledPin,LOW);//turn off the led
  }

/****************LCD********************************************/
 lcd.scrollDisplayLeft();
delay(250);

/****************IRreceiver********************************************/
  if (irrecv.decode(&results)) //if the ir receiver module receiver data
  {
  Serial.print("irCode: "); //print"irCode: "
  Serial.print(results.value, HEX); //print the value in hexdecimal
  Serial.print(", bits: "); //print" , bits: "
  Serial.println(results.bits); //print the bits
  irrecv.resume(); // Receive the next value
  }
  delay(600); //delay 600ms
  if(results.value == 0xE318261B)//if receiver module receive OxE318261B
  {
  digitalWrite(irReceiverledPin,HIGH);//turn on the led
  }
  else
  {
  digitalWrite(irReceiverledPin,LOW);//turn off the led
  }
}




Hi Anthony

With every delay in your script plug your Arduino and doing NOTHING more.

try to replace delay for "blink without delay"

I have a script edited by me attached as an example.

greeting,

vic 

/* Blink without Delay 2 leds en i2c display
 This example code is in the public domain.
 http://www.arduino.cc/en/Tutorial/BlinkWithoutDelay
 edited by DuikerVic
 */
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
 LiquidCrystal_I2C lcd(0x27,16, 2);
 
 
// constants won't change. Used here to set a pin number :
const int ledPin2 =  12;      // the number of the LED pin
const int ledPin3 =  13; 
// Variables will change :
int ledState2 = LOW; 
int ledState3 = LOW;    // ledState used to set the LED
int backlightState = LOW;

// Generally, you should use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
unsigned long previousMillis2 = 0;
unsigned long previousMillis3 = 0;   // will store last time LED was updated
unsigned long previousMillis4 = 0;

// constants won't change :
const long interval2 = 1000;     // interval at which to blink (milliseconds)
const long interval3 = 500;
const long interval4 = 200;

void setup() {
  lcd.begin();
  lcd.backlight();   
  lcd.print("blinkwithout delay");
  lcd.setCursor(0, 1);
  lcd.print("2 leds/i2c display");
  
  // set the digital pin as output:
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);
}

void loop()
{
  // here is where you'd put code that needs to be running all the time.

  // check to see if it's time to blink the LED; that is, if the
  // difference between the current time and last time you blinked
  // the LED is bigger than the interval at which you want to
  // blink the LED.
  unsigned long currentMillis = millis();
 
  if(currentMillis - previousMillis2 >= interval2) {
    // save the last time you blinked the LED 
    previousMillis2 = currentMillis;   

    // if the LED is off turn it on and vice-versa:
    if (ledState2 == LOW)
      ledState2 = HIGH;
    else
      ledState2 = LOW;
      
      unsigned long currentMillis2 = millis();
  }
  if(currentMillis - previousMillis3 >= interval3) {
    // save the last time you blinked the LED 
    previousMillis3 = currentMillis;   

      // if the LED is off turn it on and vice-versa:
    if (ledState3 == LOW)
      ledState3 = HIGH;
    else
      ledState3 = LOW;
 unsigned long currentMillis3 = millis();
    // set the LED with the ledState of the variable:
  }
 //===
if(currentMillis - previousMillis4 >= interval4) {
    // save the last time you blinked the LED 
    previousMillis4 = currentMillis;   

      // if the LED is off turn it on and vice-versa:
    if (backlightState == LOW)
      backlightState = HIGH;
    else
      backlightState = LOW;
 unsigned long currentMillis4 = millis();
    // set the LED with the ledState of the variable:
    digitalWrite(ledPin2, ledState2);
    digitalWrite(ledPin3, ledState3);    
    if(backlightState == HIGH)  lcd.backlight();
    else lcd.noBacklight();
  } 
  }


 Forget the LCD display

and play with only the LEDs.

try to recreate it using only delay

then you see the difference.

I will try this tonight.


Thanks again Vic.

ok so this "mills" method is a bit complex for me so it will be a work in progress. Do you have a very basic sketch with this? no button and one blinking LED or something?

Hi Anthony.

top of my sketch a link:

https://www.arduino.cc/en/Tutorial/BlinkWithoutDelay

This has been the basis of my sketch.

Read it and go play. customize and see what happens.

As you will see are getting some things back with a different value for the LED / backlight

On https://www.arduino.cc you can also find lots of information about variance RTC.

Remember one thing:

you learn from mistakes!!

all doing well, you learn nothing.!

:-)

greeting Vic

Login or Signup to post a comment