Go Down

Topic: Two way serial communication with raspberry Pi (radio project) (Read 4 times) previous topic - next topic

Schwabinger

Hi everybody,

I installed "music player deamon" (mpd, mpc) on my new rasberry pi. I created a playlist that contains the urls of some internet radio stations. I "tune" the stations by turning a potentiometer's knob, connected to my Arduino.

This is done using the "map" function: the pot's range is divided by the number of stations which are in my playlist. (The number of stations is initially set in variable "maxNumb". So the pot delivers values between 1 and 26)

The station number that mpc has to play is transfered via serial communication; on raspberry this value is read by a bash script that controls mpc. That works fine and stable:

Code: [Select]

int sensorValue = analogRead(A0);
    delay(100);
    int upperValue = sensorValue + 2;
    int lowerValue = sensorValue - 2;
  } while (sensorValue < upperValue && sensorValue > lowerValue);
  int tunerValue = map(sensorValue, 0, 1023, 1, maxNumb);
 
  if (tunerValue < 10)
  {
    Serial.print(0);
  }
 
  Serial.println(tunerValue,DEC);


Now to my problem:

If I add more stations I want to automatically change the value of variable "maxNumb", so that all stations can be addressed using the potentiometer.

Rasperry sends the number of stations that are on the playlist to Arduino via the serial interface:

Code: [Select]
echo "$NUMBEROFSTATIONS" > /dev/ttyACM0

The transfered number is between 20 and 99. If arduino receives this new value, variable "maxNumb" should change and the new stations should be selectable with the pod.

I try to receive this value with the following code (works well on arduino's serial monitor, but not together with raspberry):

Code: [Select]
if (Serial.available())
{
  char ch = Serial.read();
    if(index <  MaxChars && ch >= '0' && ch <= '9'){
      strValue[index++] = ch; // add the ASCII character to the string;
    }
    else
    {
      // here when buffer full or on the first non digit
      strValue[index] = 0;        // terminate the string with a 0
      maxNumb = atoi(strValue);  // use atoi to convert the string to an int
      index = 0;
    }
  }


Any ideas, why this works on the serial console but not when the data comes from the computer?

Schwabinger





Grumpy_Mike

Is the computer sending a terminating character after the numbers in the string?

Schwabinger

Hi,

I tried the following to transmit the value "40" with no success:

echo 40 > /dev/ttyACM0
echo "40" > /dev/ttyACM0
echo 40\n > /dev/ttyACM0
echo "40\n" > /dev/ttyACM0
echo 40\\n > /dev/ttyACM0
echo "40\\n" > /dev/ttyACM0

Kind regards,

Schwabinger


Grumpy_Mike

So what do you recieve from the Pi? Print out the ASCII of what you recieve.

Schwabinger

I turn the pot all the way to the right side ... so after sending the value 40, variable maxNumb should get that value and arduino should send this value back to the computer - but this does not happen:


Code: [Select]
root@raspberrypi:/home/pi# cat /dev/ttyACM0
26

26

26

26


26 is the value that was initially set to the varable "maxNumb" in my sketch.

Here is the code of whole sketch:

Code: [Select]
/*
*
* Siehe Arduino Cookbook: safari books onlin
* Uebertraegt Sendernummer zum Radio
*/
const int MaxChars = 2;
char strValue[MaxChars+1];
int index = 0;
int maxNumb = 26 ;
int value;
int upperValue;
int lowerValue;
char ch;

void setup()                    // run once, when the sketch starts
{
  Serial.begin(9600);           // set up Serial library at 9600 bps
}

void loop()                       // run over and over again
{
  int sensorValue = analogRead(A0);

do
  { 
int sensorValue = analogRead(A0);
    delay(100);
    int upperValue = sensorValue + 2;
    int lowerValue = sensorValue - 2;
  } while (sensorValue < upperValue && sensorValue > lowerValue);
  int tunerValue = map(sensorValue, 0, 1023, 1, maxNumb);
 
  if (tunerValue < 10)
  {
    Serial.print(0);
  }
 
  Serial.println(tunerValue,DEC);

delay(250);

if (Serial.available())
{
  char ch = Serial.read();
    if(index <  MaxChars && ch >= '0' && ch <= '9'){
      strValue[index++] = ch; // add the ASCII character to the string;
    }
    else
    {
      // here when buffer full or on the first non digit
      strValue[index] = 0;        // terminate the string with a 0
      maxNumb = atoi(strValue);  // use atoi to convert the string to an int
      index = 0;
    }
  }

}




Go Up