SoftwareSerial and Interrupts

Hello,

I have a small problem.
I need to use in my UNO 2 serial port's (Uart + softwareserial) and i lso need to use some interrupts (more then 2 pins).
I tryied to use softwareserial library and PinChangeInt i think that it's not possible to use them together (it give's me an error compiling).

That's interesting.

I assume that you haven't looked at any other threads on the forum, because if you had, you would see that people that post the code they are having problems with often receive help, but people that don't just get told to post the code they are having problems with.

So, post the code you are having problems with.

I need to use in my UNO 2 serial port's (Uart + softwareserial) and i lso need to use some interrupts (more then 2 pins).
I tryied to use softwareserial library and PinChangeInt i think that it's not possible to use them together (it give's me an error compiling).

It really sounds like you are using the wrong hardware, then. A Mega is only a few dollars more, and solves all of your problems.

Sorry, i forgot the code :slight_smile:

#include <SoftwareSerial.h>
#include “PinChangeInt.h”
#define MAX_PIN_CHANGE_PINS (2)

unsigned long last_interrupt_time = 0;
unsigned long interrupt_time = millis();

char meu_numero="+35191xxxxxxx";
char outro_numero="+35191yyyyyyy";

SoftwareSerial gsm(3, 2); // TX, RX

void setup()
{
Serial.begin(9600);

PCintPort::attachInterrupt(5, a, RISING);
PCintPort::attachInterrupt(6, b, RISING);

delay(3500); //tempo para iniciar o gsm

gsm.begin(9600);
gsm.println(“AT”);

}

void loop()
{
//just wait a interrupt
}

void envio_sms(char numero, char sms)
{
Serial.println(“SMS a ser enviado”);
gsm.println(“AT+CMGF=1”); // set SMS mode to text
gsm.print(“AT+CMGS=”); // now send message…
gsm.write(34); // ASCII equivalent of "
gsm.print(numero);
gsm.write(34); // ASCII equivalent of "
gsm.write(13); //CR
delay(500); // give the module some thinking time
gsm.print(sms); // our message to send
gsm.write(26); // ASCII equivalent of Ctrl-Z
gsm.write(13); //CR
delay(5000); // the SMS module needs time to return to OK status
Serial.println(“SMS Enviado”);

}

void a()
{
static unsigned long last_interrupt_time = 0;
unsigned long interrupt_time = millis();
// If interrupts come faster than 200ms, assume it’s a bounce and ignore
if (interrupt_time - last_interrupt_time > 200)
{
state = !state;
envio_sms(meu_numero, “sms a”);
}
last_interrupt_time = interrupt_time;
}

void b()
{
static unsigned long last_interrupt_time = 0;
unsigned long interrupt_time = millis();
// If interrupts come faster than 200ms, assume it’s a bounce and ignore
if (interrupt_time - last_interrupt_time > 200)
{
state = !state;
envio_sms(outro_numero, “sms b”);
}
last_interrupt_time = interrupt_time;
}

Thanks in advance

(I know that with a mega i solve my problem but, i don’t want to buy a new mega having 3 UNO’s.) :wink:

I think that your biggest problem is that you are trying to do far to much in your interrupt handlers. a() and b() should simply set a flag indicating that a message needs to be sent (and, if necessary, which message). Then, loop() should periodically check that flag, and call the function to send the message, if the flag is true.

You absolutely CAN NOT call delay() from inside an ISR or any function that it calls.

SoftwareSerial is #2 in my list of things not to use with Arduino Five things I never use in Arduino projects | David Crocker's Solutions blog precisely because it has to disable interrupts for the entire duration of sending or receiving a character. Use a Mega instead, or find a way of needing only one serial port.

I am experiencing similar problems. The following will not compile:

#include <SoftwareSerial.h>
#include <PinChangeInt.h>
#include <AdaEncoder.h>

void setup() {
}

void loop() {
}

If you comment out the #include for SoftwareSerial.h it compiles. If you only comment out the #include for PinChangeInt.h it compiles. If you try to compile as-is, I get the following using an UNO:

SoftwareSerial\SoftwareSerial.cpp.o: In function __vector_3': C:\arduino-1.0-windows\arduino-1.0\libraries\SoftwareSerial/SoftwareSerial.cpp:302: multiple definition of __vector_3’
Blink.cpp.o:C:\arduino-1.0-windows\arduino-1.0\libraries\PinChangeInt/PinChangeInt.h:485: first defined here
SoftwareSerial\SoftwareSerial.cpp.o: In function __vector_4': C:\arduino-1.0-windows\arduino-1.0\libraries\SoftwareSerial/SoftwareSerial.cpp:309: multiple definition of __vector_4’
Blink.cpp.o:C:\arduino-1.0-windows\arduino-1.0\libraries\PinChangeInt/PinChangeInt.h:492: first defined here
SoftwareSerial\SoftwareSerial.cpp.o: In function __vector_5': C:\arduino-1.0-windows\arduino-1.0\libraries\SoftwareSerial/SoftwareSerial.cpp:316: multiple definition of __vector_5’
Blink.cpp.o:C:\arduino-1.0-windows\arduino-1.0\libraries\PinChangeInt/PinChangeInt.h:499: first defined here

I forgot to mention that I am using Arduino 1.0.

In what way is it similar? This belongs in a new thread.

The thread author's code also begins by including the SoftwareSerial and PinChangeInt libraries. I have since heard back from the author of PinchangeInt who indicated there is indeed a conflict between the two in that they both use pin change interrupts. So regardless of whatever other issues the thread author had with his code, it will never compile because apparently a person can't use both libraries at the same time without customizing one to specify which one gets to control which ports.

I don't know if this helps at all but I am using Softwareserial on pins 4 and 5 to talk to a GLCD. I also use hardware serial for wireless IO. I also use an interrupt to wake from sleep mode. All of this and I have no issues whatsoever.

I use the pin 2 interrupt INT0. So instead of using the pinchangeint library, use the included hardware interrupts on Pins 2 and 3. This should solve your problem.

Search for External Interrupt and you will find lots of examples on how to implement it.

..myabe also interesting: Arduino Forum