ProtoThreads Library

Hello everyone, I am doing a project, and I am using the Protothreads library, however the printing on a 16X2 LCD for the I2C module has not worked for me, when I load it it does not work, however if I load it without the library from Protothreads if it works. I thank the person who can help me

I will attach the 2 codes that I use for the tests.

CODE WHIT ProtoThreads ---- void I2CLED ---- NOT WORKS

#include <LiquidCrystal_I2C.h>
#include <pt.h>
#include <Wire.h>
#include <Adafruit_NeoPixel.h>
LiquidCrystal_I2C lcd (0x27, 16, 2);
#define DELAYWS 100               // Tiempo de Respuesta WS2812 
#define DATOS 33                  // Pin Datos WS2812
#define NUMPIXELES 60             // Numero de Pixeles
Adafruit_NeoPixel NEO(NUMPIXELES, DATOS, NEO_GRB + NEO_KHZ800);


int NA = 16;
int NB = 16;
int MQ9= 10;

String T1 =" BIENVENIDOS TUNEL UMNG ";
String T2 ="NIVEL DE CO: " ;
int Inicio, Final = 0;
int Posicion = NA;


const byte S1 = 3;
const byte S2 = 4;
const byte S3 = 5;
const byte S4 = 6;
const byte S5 = 7;
const byte S6 = 8;
const byte S7 = 9;
const byte S8 = 10;

int D1;
int D2;
int D3;
int D4;
int D5;
int D6;
int D7;
int D8;

struct pt PT1;
struct pt PT2;
struct pt PT3;
struct pt PT4;


void setup() {
  
PT_INIT(&PT1);
PT_INIT(&PT2);
PT_INIT(&PT3);
PT_INIT(&PT4);


}

void loop() {

FC51(&PT1);
I2CLED(&PT2);

}
void I2CLED(struct pt * pt){

   
     
   PT_BEGIN(pt)
 static long t = 0;
    lcd.backlight();
    lcd.init();
 
 do{    
    
      MQ9=analogRead(A1);

  
  lcd.setCursor(0,1);
  lcd.print(T2);
  lcd.print(MQ9);

  lcd.setCursor(Posicion,0);
  lcd.print(T1.substring(Inicio,Final));

  t=millis();
  PT_WAIT_UNTIL(pt, ((millis()-t)<200));
  lcd.clear();

  if (Inicio == 0 && Posicion > 0){
      Posicion--;
      Final++;
  }
  
  else if(Inicio == Final){
   Inicio = Final = 0;
   Posicion = NA; 
  }

  else if(Final == T1.length()&& Posicion == 0){
  Inicio++;  
  }

  else {
    
  Inicio++;
  Final++;  
    
  }
  
    }while(true);

    PT_END(pt);

  
}
void FC51(struct pt * pt){
  
      PT_BEGIN(pt);   

  pinMode(S1, INPUT);
  pinMode(S2, INPUT);
  pinMode(S3, INPUT);
  pinMode(S4, INPUT);
  pinMode(S5, INPUT);
  pinMode(S6, INPUT);
  pinMode(S7, INPUT);
  pinMode(S8, INPUT);
  pinMode(DATOS, OUTPUT);
   
  NEO.begin(); // INITIALIZE NeoPixel strip object (REQUIRED)  
  



do{

  NEO.setBrightness(200);      // Brillo WS2812
  NEO.show();
  //delay (DELAYWS);
  NEO.clear();

  D1 = digitalRead(S1);
  if (D1 == LOW) {
    
  NEO.setPixelColor(0, NEO.Color(0, 255, 255));
  NEO.setPixelColor(1, NEO.Color(0, 255, 255));
  NEO.setPixelColor(2, NEO.Color(0, 255, 255));
  NEO.setPixelColor(3, NEO.Color(0, 255, 255));
  NEO.setPixelColor(4, NEO.Color(0, 255, 255));
    
  }

  D2 = digitalRead(S2);
  if (D2 == LOW) {
    
  NEO.setPixelColor(4, NEO.Color(0, 255, 255));
  NEO.setPixelColor(5, NEO.Color(0, 255, 255));
  NEO.setPixelColor(6, NEO.Color(0, 255, 255));
  NEO.setPixelColor(7, NEO.Color(0, 255, 255));
  NEO.setPixelColor(8, NEO.Color(0, 255, 255));
  NEO.setPixelColor(9, NEO.Color(0, 255, 255));
  NEO.setPixelColor(10, NEO.Color(0, 255, 255));
  }

  D3 = digitalRead(S3);
  if (D3 == LOW) {
  NEO.setPixelColor(10, NEO.Color(0, 255, 255));
  NEO.setPixelColor(11, NEO.Color(0, 255, 255));
  NEO.setPixelColor(12, NEO.Color(0, 255, 255));
  NEO.setPixelColor(13, NEO.Color(0, 255, 255));
  NEO.setPixelColor(14, NEO.Color(0, 255, 255));
  NEO.setPixelColor(15, NEO.Color(0, 255, 255));
  NEO.setPixelColor(16, NEO.Color(0, 255, 255));
  }

  D4 = digitalRead(S4);
  if (D4 == LOW) {
  NEO.setPixelColor(16, NEO.Color(0, 255, 255));
  NEO.setPixelColor(17, NEO.Color(0, 255, 255));
  NEO.setPixelColor(18, NEO.Color(0, 255, 255));
  NEO.setPixelColor(19, NEO.Color(0, 255, 255));
  NEO.setPixelColor(20, NEO.Color(0, 255, 255));
  NEO.setPixelColor(21, NEO.Color(0, 255, 255));
  NEO.setPixelColor(22, NEO.Color(0, 255, 255));
  }

  D5 = digitalRead(S5);
  if (D5 == LOW) {
  NEO.setPixelColor(22, NEO.Color(0, 255, 255));
  NEO.setPixelColor(23, NEO.Color(0, 255, 255));
  NEO.setPixelColor(24, NEO.Color(0, 255, 255));
  NEO.setPixelColor(25, NEO.Color(0, 255, 255));
  NEO.setPixelColor(26, NEO.Color(0, 255, 255));
  NEO.setPixelColor(27, NEO.Color(0, 255, 255));
  NEO.setPixelColor(28, NEO.Color(0, 255, 255));
  }

  D6 = digitalRead(S6);
  if (D6 == LOW) {
  NEO.setPixelColor(28, NEO.Color(0, 255, 255));
  NEO.setPixelColor(29, NEO.Color(0, 255, 255));
  NEO.setPixelColor(30, NEO.Color(0, 255, 255));
  NEO.setPixelColor(31, NEO.Color(0, 255, 255));
  NEO.setPixelColor(32, NEO.Color(0, 255, 255));
  NEO.setPixelColor(33, NEO.Color(0, 255, 255));
  NEO.setPixelColor(34, NEO.Color(0, 255, 255));
  }

  D7 = digitalRead(S7);
  if (D7 == LOW) {
  NEO.setPixelColor(34, NEO.Color(0, 255, 255));
  NEO.setPixelColor(35, NEO.Color(0, 255, 255));
  NEO.setPixelColor(36, NEO.Color(0, 255, 255));
  NEO.setPixelColor(37, NEO.Color(0, 255, 255));
  NEO.setPixelColor(38, NEO.Color(0, 255, 255));
  NEO.setPixelColor(39, NEO.Color(0, 255, 255));
  NEO.setPixelColor(40, NEO.Color(0, 255, 255));
  }

  D8 = digitalRead(S8);
  if (D8 == LOW) {
  NEO.setPixelColor(40, NEO.Color(0, 255, 255));
  NEO.setPixelColor(41, NEO.Color(0, 255, 255));
  NEO.setPixelColor(42, NEO.Color(0, 255, 255));
  NEO.setPixelColor(43, NEO.Color(0, 255, 255));
  NEO.setPixelColor(44, NEO.Color(0, 255, 255));
  NEO.setPixelColor(45, NEO.Color(0, 255, 255));
  }
}
  while(true);
  
      PT_END(pt);
}

Code WHITOUT ProtoThreads Library ITS WORKS

#include <Wire.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27,16,2);
int NA = 16;
int NB = 16;
int MQ9= 10;

String T1 =" BIENVENIDOS TUNEL UMNG ";
String T2 ="NIVEL DE CO: " ;
int Inicio, Final = 0;
int Posicion = NA;

void setup() {
 // put your setup code here, to run once:
   lcd.backlight();
   lcd.init();
   
}

void loop() {
 // put your main code here, to run repeatedly:
 
 MQ9=analogRead(A1);

 
 lcd.setCursor(0,1);
 lcd.print(T2);
 lcd.print(MQ9);

 lcd.setCursor(Posicion,0);
 lcd.print(T1.substring(Inicio,Final));

 delay(200);
 lcd.clear();

 if (Inicio == 0 && Posicion > 0){
     Posicion--;
     Final++;
 }
 
 else if(Inicio == Final){
  Inicio = Final = 0;
  Posicion = NA; 
 }

 else if(Final == T1.length()&& Posicion == 0){
 Inicio++;  
 }

 else {
   
 Inicio++;
 Final++;  
   
 }

 
 }

It would help if you could describe what "not works" means.

Hello
What do you expect by using the PT-lib?

I am using 8 infrared sensors to light various segments of a WS2812B strip, that part is already working. The objective with the PT is that it can execute another function in parallel, and don't get involved with each other. what I want now is that through I2C the LCD works, and that when I upload the code it does not print anything.

Hi

What I want with the PT is that I can do several functions independently, so that I don't have to depend on the times of each function to have synchrony.

I have no idea if because it is I2C, it can affect the function. :thinking::thinking:

For one thing, are you sure the condition in this statement is correct?

I would think it should be ((millis()-t) >= 200).