Problem with Serial.readBytes

Status
Not open for further replies.

Racia

Member
Hi,
I'm trying to call different frequencies from different case using the serial monitor. I understand that Serial.read only allows to read up to one byte. Therefore, I have a problem using Serial.readBytes for the program to read the integer that has more than one byte i've input in the serial monitor. When i input the case number in the serial monitor, nothing came out from my speakers. Do i have to add any more things into the program for it to work?
Code:
/*
c
  - released
b
- Use FIR filters with fast_fft option

The audio board uses the following pins.
 6 - MEMCS
 7 - MOSI
 9 - BCLK
10 - SDCS
11 - MCLK
12 - MISO
13 - RX
14 - SCLK
15 - VOL
18 - SDA
19 - SCL
22 - TX
23 - LRCLK
*/

//#include <arm_math.h>
#include <Audio.h>
#include <Wire.h>
#include <SD.h>
#include <SPI.h>
#include <Bounce.h>
#include "filters.h"

// If this pin is grounded the FIR filter is turned off
// which just passes the audio sraight through
// Don't use any of the pins listed above
#define PASSTHRU_PIN 1
// If this pin goes low the next FIR filter in the list
// is switched in.
#define FILTER_PIN 0

// debounce the passthru and filter switching pins
Bounce b_passthru = Bounce(PASSTHRU_PIN,15);
Bounce b_filter   = Bounce(FILTER_PIN,15);

//const int myInput = AUDIO_INPUT_MIC;
const int myInput = AUDIO_INPUT_LINEIN;


AudioInputI2S       audioInput;         // audio shield: mic or line-in
AudioFilterFIR      myFilterL;
AudioFilterFIR      myFilterR;
AudioOutputI2S      audioOutput;        // audio shield: headphones & line-out

// Create Audio connections between the components
// Route audio into the left and right filters
AudioConnection c1(audioInput, 0, myFilterL, 0);
AudioConnection c2(audioInput, 1, myFilterR, 0);
// Route the output of the filters to their respective channels
AudioConnection c3(myFilterL, 0, audioOutput, 0);
AudioConnection c4(myFilterR, 0, audioOutput, 1);
AudioControlSGTL5000 audioShield;


struct fir_filter {
  short *coeffs;
  short num_coeffs;
};

// index of current filter. Start with the low pass.
int fir_idx = 0;
struct fir_filter fir_list[] = {
  {low_pass , 100},    // low pass with cutoff at 1kHz and -60dB at 2kHz
  {band_pass, 100},    // bandpass 1200Hz - 1700Hz
  {NULL,      0}
};


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

  pinMode(PASSTHRU_PIN, INPUT_PULLUP);
  pinMode(FILTER_PIN, INPUT_PULLUP);

  // allocate memory for the audio library
  AudioMemory(8);

  audioShield.enable();
  audioShield.inputSelect(myInput);
  audioShield.volume(0.5);
  
  // Warn that the passthru pin is grounded
  if(!digitalRead(PASSTHRU_PIN)) {
    Serial.print("PASSTHRU_PIN (");
    Serial.print(PASSTHRU_PIN);
    Serial.println(") is grounded");
  }

  // Warn that the filter pin is grounded
  if(!digitalRead(FILTER_PIN)) {
    Serial.print("FILTER_PIN (");
    Serial.print(FILTER_PIN);
    Serial.println(") is grounded");
  }  
  // Initialize the filter
  myFilterL.begin(fir_list[0].coeffs, fir_list[0].num_coeffs);
  myFilterR.begin(fir_list[0].coeffs, fir_list[0].num_coeffs);
  Serial.println("setup done");
}

// index of current filter when passthrough is selected
int old_idx = -1;
char buffer[] = {' ',' ',' ',' ',' ',' ',' '}; // Receive up to 7 bytes
// audio volume
int volume = 0;
unsigned long last_time = millis();

void loop()
{
  // Volume control
  int n = analogRead(15);
  if (n != volume) {
    volume = n;
    //uncomment this line if your audio shield has the volume pot
    //audioShield.volume((float)n / 1023);
  }
  if (Serial.available()>0) // not grounded
    {  
        Serial.readBytes(buffer, 7);
       int incomingValue = atoi(buffer);
       switch (incomingValue)
       {
         case'0':
         sine1.amplitude(0.1);
         sine1.frequency(0.000);
         break;
         
         case'100':
         sine1.amplitude(0.05); 
         sine1.frequency(100.000);
         break;
         
         case'200':
         sine1.amplitude(0.05);
         sine1.frequency(200.000);

         case'1000':
         sine1.amplitude(0.05);
         sine1.frequency(1000.000);
         break;
                }
    }
 
      
 }
 
Others may have more info, but my quick look here:
Not sure from your code if this is keyboard input from something like terminal monitor? I am guessing yes.

If so, I would probably use readBytesUntil an probably search for CR. Why because readBytes(buffer, 7) will wait for 7 characters or a timeout, which is probably not what you want.

Note default the timeout is something like 1 second, might be fine, but you can set using Serial.setTimeout.

Also not sure when it was added (maybe awhile?) there is also Serial.parseInt...

Kurt
 
don't try to send binary integers data - a user input would be one or more ASCII characters - hopefully the ASCII digits 0 to 9.
You use sscanf() or atoi() or your own code to convert the ASCII digits coming in on serial into an integer. This is a very common problem.
 
Status
Not open for further replies.
Back
Top