softSerial library bug

Hi,

I am doing my first big project. It will be jacket with electronical device.
I have a problem with NewSoftSerial library. There is a project, where is the lcd, rotary encoder and buzzer connected to the arduino (code below). I use a libraries from arduino.cc for lcd and rotary encoder and buzzer , connected to the arduino and write program for dynamic menu which is controlled by encoder. There is also buzzer to make beep. The problem is, when I use NewSoftSerial library from arduiniana.org my program do not work properly. I need this lib for later connect GPS device, the lib is close knit with tinyGPS lib. Normally, when is not the NewSoftSerial library present, the program goes well. When you turn encoder, the display will change and buzzer beeps. But when you put this library to the project there are glimmers when you turn the encoder. The program is delayed max for 1 second, sometime it isn´t delay. The buzzer make some absurd noise.
I have no idea, how to fix it :frowning: :(. By my opinion, the soft library is based on the timing and interupts and that is the main problem. I tried to change baud rate of this, but the incorrect sound of the buzzer only changed to another tone. Do you have some suggestion how to fix it?? Thank you very much.

//---------------------------program dynamical menu-----------------------------


#include <LiquidCrystal.h>                    // library for lcd
#include <SoftwareSerial.h>                   // library for software serial link

SoftwareSerial nss(3,2);

  LiquidCrystal lcd(12, 11, 5, 4, 10, A1);   //  lcd display setting
  int encoder0PinA = 6;                      //  rotary encoder setting 
  int encoder0PinB =7; 
  int encoder0Pos = 0;                         
  int encoder0PinALast = LOW;  
  int n = LOW;             
 
  boolean flagL = 1;                       // flag for rotate to the left
  boolean flagR = 0;                       // flag for rotate to the right
  
 

//---------------------------------------------------setup----------------------------------

void setup() {
 
  pinMode (encoder0PinA,INPUT);                       // rotary encoder            
  pinMode (encoder0PinB,INPUT);                       // rotary encoder                   
  pinMode (A0,   OUTPUT);                             // buzzer 
  lcd.begin(16, 2); 
  lcd.clear();                                           
  nss.begin(9600);                                    //set the baud rate os software serial link
}
//---------------------------------------------------procedures----------------------------

static void buzz(int targetPn, long freq, long leng) {    //function for  beep 
  long delayValu = 1000000/freq/2; 
  long numCycle = freq * leng/ 1000; 
   for (long i=0; i < numCycle; i++){ 
    digitalWrite(targetPn,HIGH); 
    delayMicroseconds(delayValu); 
    digitalWrite(targetPn,LOW); 
    delayMicroseconds(delayValu); 
  }
}


void arrow(){                            //  set the low row of the lcd (arrows)                            
  lcd.setCursor(0, 1);
  lcd.print("<<");
  lcd.setCursor(18, 1);
  lcd.print(">>");
   lcd.setCursor(9, 1);
  lcd.print("o");
  
}

void moveLeft(){                      // rotation to the left                                
   for (int i=0; i <= 18; i++){
      lcd.scrollDisplayLeft(); 
      delay(40);
   } 
}
void moveRight(){                      // rotation to the right                              
    for (int i=0; i <= 18; i++){
      lcd.scrollDisplayRight(); 
        delay(40);
   } 
   }
   
void beep(){                                      //procedure for shor beep after turn rotary encoder
 
  buzz(A0, 3600, 20); 
   
 } 
 

  
  
void enCoder(){                                    //procedure for read rotary encoder
     n = digitalRead(encoder0PinA);                //setting flags and possition to
   if (encoder0PinALast == LOW && n == HIGH) {     //the register encoder0Pos
     if (digitalRead(encoder0PinB) == LOW) {      
{
 if (encoder0Pos == 0)
{
  encoder0Pos = 5;
  flagL = 1;
  beep();
}
else
{
 encoder0Pos--;
 flagL = 1;
 beep();
}
}      
     } else {
        if (encoder0Pos ==5)
{
  encoder0Pos = 0;
  flagR = 1;
  beep();
    
  
}
else  
{
 encoder0Pos++;
 flagR = 1;
 beep();
    
 
}
  }
   } 
   encoder0PinALast = n;
}

//--------------------------------------------------- loop ------------------------------------------   
  
void loop() {
  
  enCoder();                          // procedure to find out the possition  
  
  
                     
  if (( flagR == 1)or(flagL == 1)) {   // rotation to the left or right     
    
    if (flagR == 1) {
    moveRight();  
     
    }
    if (flagL == 1) {
      moveLeft();
       }
       
       
     
  lcd.clear();                          // first set of display
  arrow();
  lcd.setCursor(6, 0); 
 
  switch ( encoder0Pos) {              // selection label by encoder0Pos
      case 0:
      lcd.print("Gps");      
      break;
      case 1:
      lcd.print("Temp");
      break;
      case 2:
      lcd.print("Time");
      break;
      case 3:
      lcd.print("Light");
      break;
      case 4:
      lcd.print("Setting");
      break;
      case 5:
      lcd.print("Access");
      break;
  
  }
  
   flagR = 0;                  // neccesary to clear flags, to prevent program cycle
   flagL = 0;                  // (oparation is finish)
   
 }
 
  
}

Do you have something connected to the pins of the SoftwareSerial object that does constantly send some data? If yes, your code gets interrupted for every byte received (for the whole length of the byte transfer). If you have a device that sends about 800 characters a second to your Arduino, it won't do anything else than receiving characters although you (currently) don't use any of them. If you have timing critical code (a buzzer is an example) you should not use SoftwareSerial. If you need more than one UART, use a Mega which has 4 of them.

pylon: If you have a device that sends about 800 characters a second to your Arduino, it won't do anything else than receiving characters although you (currently) don't use any of them.

I imagine this would be particularly applicable if the Rx pin was left floating.

Thx for your replies.

I managed the problem with commands interrupts() and noInterrupts(). I placed these to the program on the certain places and the program works so thanks :-)

Although this works for you now you haven't solved the issue. Have you tried pulling the Rx pin down (to GND)? Does this solve your issue without changing the code? You probably won't disable interrupts this long...