Hello:
I'm developing a device meant to diagnose serial communication. It's supposed to send a known frame, read it back via serial1 and do the comparison. I need to use a I2C 20x4 liquid cristal display to show results. I made a simple test sketch, using Serial1, Serial2 and the display, uploaded it to the Pico and it worked just fine. However, when I uploaded a more advanced sketch, including a test routine, everything went south: The device does nothing, LCD shows nothing, and Windows complaints that it does not recognize the USB device.
The following sketch works fine:
#include <Arduino.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,20,4); // Define la direccion del LCD a 0x27 para una pantalla de 20 caracteres y 4 lineas
#ifndef LED_BUILTIN
#define LED_BUILTIN 13
#endif
#define P_VELOCIDAD 15
#define P_MODO 28
#define P_FUNCION 27
#define P_DURACION 26
#define P_PRUEBA 22
#define OUT01 16
#define RTS1 2
#define CTS1 3
#define RTS2 6
#define CTS2 7
#define LED_V1 8
#define LED_V2 9
#define LED_M1 10
#define LED_M2 11
#define LED_F 12
#define LED_D1 13
#define LED_D2 14
#define LED_TX1 18
#define LED_RX1 19
#define LED_TX2 20
#define LED_RX2 21
volatile bool seq1[10] = {1,0,0,0,0,0,0,0,0,0};
volatile bool seq2[10] = {1,1,1,1,1,0,0,0,0,0};
volatile bool seq3[10] = {1,0,1,0,1,0,1,0,1,0};
volatile bool seq4[10] = {1,1,1,0,0,0,0,0,0,0};
volatile bool flag_test = false;
volatile int seq_ctr = 0;
volatile int tick_ctr = 0;
volatile bool flag_1_s = false, flag_3_s = false;
volatile int i = 0;
char line[64];
unsigned long previousMillis = 0; // will store last time LED was updated
const long interval = 50; // interval at which to blink (milliseconds)
/////////////////////////////////////////////////
///// Manejador de interupcion pin P_PRUEBA /////
/////////////////////////////////////////////////
void PRUEBA_ISR()
{
if(flag_test == false)
flag_test = true;
else if(flag_test == true)
flag_test = false;
}
void setup()
{
// initialize digital pin LED_BUILTIN as an output.
pinMode(LED_BUILTIN, OUTPUT);
pinMode(P_VELOCIDAD,INPUT);
pinMode(P_MODO,INPUT);
pinMode(P_FUNCION,INPUT);
pinMode(P_DURACION,INPUT);
pinMode(P_PRUEBA,INPUT);
pinMode(RTS1, OUTPUT);
pinMode(CTS1,INPUT);
pinMode(RTS2, OUTPUT);
pinMode(CTS2,INPUT);
pinMode(LED_V1,OUTPUT);
pinMode(LED_V2,OUTPUT);
pinMode(LED_M1,OUTPUT);
pinMode(LED_M2,OUTPUT);
pinMode(LED_F,INPUT); // LEDS función apagados al principio (MODO = 0)
pinMode(LED_D1,OUTPUT);
pinMode(LED_D2,INPUT); // LEDS 120 y 180 [s] apagados al principio
pinMode(LED_TX1,OUTPUT);
pinMode(LED_RX1,OUTPUT);
pinMode(LED_TX2,OUTPUT);
pinMode(LED_RX2,OUTPUT);
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, LOW);
flag_test = false;
lcd.init(); // initialize the lcd
lcd.backlight();
// Configurar interrupciones de pulsación
attachInterrupt(digitalPinToInterrupt(P_PRUEBA), PRUEBA_ISR, FALLING);
Serial.begin(9600);
Serial1.begin(1200);
Serial2.begin(1200);
digitalWrite(LED_F,false);
digitalWrite(LED_D1,false);
digitalWrite(RTS1, true);
digitalWrite(RTS2, true);
}
void loop()
{
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval)
{
// save the last time you blinked the LED
previousMillis = currentMillis;
if(!flag_test)
digitalWrite(LED_BUILTIN,seq1[seq_ctr]);
else
digitalWrite(LED_BUILTIN,seq3[seq_ctr]);
seq_ctr++;
if(seq_ctr>10)
{
flag_1_s = true;
seq_ctr = 0;
}
}
//lcd.setCursor(col, row);
lcd.setCursor(0,0);
lcd.print("TEST");
lcd.setCursor(0,1);
lcd.print("RPI Pico");
lcd.setCursor(0,2);
if(flag_test == true)
sprintf(line,"flag_test = true ");
else if(flag_test == false)
sprintf(line,"flag_test = false ");
////////////////01234567890123456789
lcd.print(line);
lcd.setCursor(0,3);
lcd.print("Counter: ");
lcd.setCursor(10,3);
lcd.print(" ");
lcd.setCursor(10,3);
lcd.print(i,DEC);
if(flag_1_s == true)
{
sprintf(line, "i = %02d", i);
Serial.println(line);
digitalWrite(LED_TX1, true);
digitalWrite(LED_TX2, true);
Serial1.println(line);
Serial2.println(line);
digitalWrite(LED_TX1, false);
digitalWrite(LED_TX2, false);
i++;
if(i==30)
i = 0;
flag_1_s = false;
}
}
I have the code which breaks stuff, but I'll put it in another post, so it doesn't appear as if i'm spamming the forum.
This is driving me crazy, as i don't understand why does it fail...
Best regards,
Fenhasan