Pages: [1]   Go Down
Author Topic: tone() and reading data from serial  (Read 602 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 0
Posts: 56
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

In my sketch main loop I used SoftwareSerial in order to read data from gps.

void loop()  
{
  while (nss.available())
  {
    if (gps.encode(nss.read()))
    {
     ...
Also I have timer ISR, which is called every second or so, where I engage buzzer

ISR(TIMER1_COMPA_vect)
{
  tone(8, 1000, 200);
  // digitalWrite(9, 1); delay(200); digitalWrite(9, 0);

}
The problem is, when I hear the tone and at the same time there is an incoming gps data, the tone is corrupted. When data is finished the tone is clear. So somehow serial input and tone are interfering with each other. In other words, if I just comment nss.begin(9600); then tone is clear.

Update:

Below is the full code:
Code:
#include <avr/io.h>
#include <avr/interrupt.h>
#include <SoftwareSerial.h>
#include <TinyGPS.h>

TinyGPS gps;
SoftwareSerial nss(3, 4);

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

  // initialize Timer1
  cli();          // disable global interrupts
  TCCR1A = 0;     // set entire TCCR1A register to 0
  TCCR1B = 0;     // same for TCCR1B

  // set compare match register to desired timer count:
  OCR1A = 15624;

  // turn on CTC mode:
  TCCR1B |= (1 << WGM12);

  // Set CS10 and CS12 bits for 1024 prescaler:
  TCCR1B |= (1 << CS10);
  TCCR1B |= (1 << CS12);

  // enable timer compare interrupt:
  TIMSK1 |= (1 << OCIE1A);

  // enable global interrupts:
  sei();
}

ISR(TIMER1_COMPA_vect)
{
  tone(8, 1000, 500);
}


void loop() 
{
  float flat, flon, speedkmh;
  unsigned long age;
  unsigned short sat;
 
  while (nss.available())
  {
    if (gps.encode(nss.read()))
    {
        gps.f_get_position(&flat, &flon, &age);
        sat = gps.satellites();
        speedkmh = gps.f_speed_kmph();
        if(flat != TinyGPS::GPS_INVALID_F_ANGLE && flon != TinyGPS::GPS_INVALID_F_ANGLE && age != TinyGPS::GPS_INVALID_AGE
            && sat != TinyGPS::GPS_INVALID_SATELLITES && speedkmh != TinyGPS::GPS_INVALID_SPEED)
        {
         
        }
        else
          Serial.println("Something is wrong...");
       
    }
     
  }
}


How can I solve this?
« Last Edit: November 03, 2012, 09:19:15 am by acid » Logged

Poole, Dorset, UK
Offline Offline
Edison Member
*
Karma: 50
Posts: 2207
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You need to post all your code not just a small part ( and post it within code tags)

Mark
Logged

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12568
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

When you pass a duration to tone() it becomes a blocking call, meaning it does not return until the duration has passed. Since you're calling it within an ISR, this prevents anything else from happening during that time.

I suggest you completely abandon use of an ISR to trigger this, and use the technique demonstrated in 'blink without delay' to start and stop the tone at regular intervals from within loop().
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Pages: [1]   Go Up
Jump to: