Show Posts
Pages: [1] 2 3
1  Using Arduino / Interfacing w/ Software on the Computer / Re: Two way serial communication with raspberry Pi (radio project) on: December 06, 2012, 04:32:05 pm
Problem solved:

I skipped Arduino Uno and work with Raspberry's gpio pins only. No serial communication needed any more ...  smiley-lol

The following declares  gpio pin 7 to be an input pin:

Code:
echo "7" > /sys/class/gpio/export
echo "in" > /sys/class/gpio/gpio7/direction


Whenever I press a button, variable BUTTON gets value 1, otherwise 0

Code:
BUTTON=$(cat /sys/class/gpio/gpio7/value)

So after booting my radio starts playing with station 1; pressing the button produces a switch to the next station.
When the end of the list is reached, we start again with station 1.


2  Using Arduino / Interfacing w/ Software on the Computer / Re: Two way serial communication with raspberry Pi (radio project) on: December 02, 2012, 01:43:00 pm
Just come over to Europe ... the beer is on me :-)


I found a c Program "ardunio-serial.c"

Code:
pi@raspberrypi ~/scripts $ ./arduino-serial -b 9600 -p /dev/ttyACM0 -d 1600 -s 30 -r
read: 30

With that I can transfer a value like "30" in this example from the raspberry to arduino.
Unfortunately my script does not catch that value.

Any idea, why?


3  Using Arduino / Interfacing w/ Software on the Computer / Re: Two way serial communication with raspberry Pi (radio project) on: December 02, 2012, 11:00:41 am
This is how I read on the pi what arduino is sending:

Code:
#!/bin/bash
set -x
ALTWERT=1
while true
do
WERT=$(head -n 1 < /dev/ttyACM0)
WERT=$(echo  $WERT | sed "s:\r::")
NUMBSTATIONS=$(mpc playlist | wc -l)

if [ $ALTWERT -ne $WERT ]; then
mpc play "$WERT"
ALTWERT=$WERT
#sleep 1
fi

#echo  "$NUMBSTATIONS\n"  > /dev/ttyACM0
done

Variable WERT is filled with what is coming from arduino. So by turning the pot I can "tune" through my playlist.

Variable NUMBSTATIONS hold the number of radio stations in my playlist.  This is the value that I want to echo to the pot.

Echo is commented # in the code above;

But as I leared in this thread in the last two hours "echo" does not work in this case.

4  Using Arduino / Interfacing w/ Software on the Computer / Re: Two way serial communication with raspberry Pi (radio project) on: December 02, 2012, 10:08:09 am
Here is my scenario:

Arduino is connected to my Windows Desktop PC. This is where I write the Arduino code.
If I use the Serial Monitor and run the sketch, everything works fine. In the serial monitor I can fill variable "maxNumb" and the arduino sketch does what I want.

When I connect arduino to my Raspberry pi computer (the target system for my Internet radio):

- Raspberry can receive tuner values between 1 and 26 (according to the pot position)
- I cannot use "echo 40 > /dev/ttyACM0" to change the value of variable "maxNumb".

So the serial communication works from arduino to pi, but not the other way :-(





5  Using Arduino / Interfacing w/ Software on the Computer / Re: Two way serial communication with raspberry Pi (radio project) on: December 02, 2012, 09:27:30 am
I removed some lines that are not used at the moment anyway:


Code:
/*
 *
 * Siehe Arduino Cookbook: safari books online
 * Sends station numbers to Raspberri: ok
 * Receives max Numb of stations from Raspberry: nok
 */
const int MaxChars = 2;
char strValue[MaxChars+1];
int index = 0;
int maxNumb = 26;
int upperValue;
int lowerValue;
char ch;
int poti = 0;
int sensorValue = 10;
int tunerValue = 1;

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
{
 sensorValue = analogRead(poti);

 tunerValue = map(sensorValue, 0, 1023, 1, maxNumb);
 
  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;
    }
  }

}

At least at the built in serial console it does what I want it to do:

- Starting with an initial value for the variable "maxNumb" (26) depending on the poti's position a number between 1 and 26 ist transfered via serial;
- If I enter a different value in the console, maxNumb is updated; if for instance I enter 50, by turning the knob I can produce numbers between 1 and 50:

But I still have the problem, that I cannot use "echo" from my computer to fill this variable.
-
6  Using Arduino / Interfacing w/ Software on the Computer / Re: Two way serial communication with raspberry Pi (radio project) on: December 02, 2012, 08:37:11 am
Thanks to everyone who is helping here ..:-)

Ardunio and computer are connected via USB.

Tried to take all variables out of the loop. Resulted in code that compiles ... but if I run it in the serial console nothing happens any more. The  WHILE condition will never be true, so I will remove that.

Code:
*
 *
 * Siehe Arduino Cookbook: safari books online
 * Sends station numbers to Raspberri: ok
 * Receives max Numb of stations from Raspberry: nok
 */
const int MaxChars = 2;
char strValue[MaxChars+1];
int index = 0;
int maxNumb = 26;
int upperValue;
int lowerValue;
char ch;
int poti = 0;
int sensorValue = 1;
int tunerValue = 1;


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
{
 
do
  {  
sensorValue = analogRead(poti);
    upperValue = sensorValue + 2;
    lowerValue = sensorValue - 2;
  } while (sensorValue < upperValue && sensorValue > lowerValue);
  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;
    }
  }

}
7  Using Arduino / Interfacing w/ Software on the Computer / Re: Two way serial communication with raspberry Pi (radio project) on: December 02, 2012, 07:46:05 am
Tried to change as advised - problem still exists ...:

Code:
/*
 *
 * Siehe Arduino Cookbook: safari books online
 * Sends station numbers to Raspberri: ok
 * Receives max Numb of stations from Raspberry: nok
 */
const int MaxChars = 2;
char strValue[MaxChars+1];
int index = 0;
int maxNumb = 26 ;
int value;
int upperValue;
int lowerValue;
char ch;
int poti = 0;
int sensorValue;


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
{
 
do
  { 
sensorValue = analogRead(poti);
    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;
    }
  }

}

8  Using Arduino / Interfacing w/ Software on the Computer / Re: Two way serial communication with raspberry Pi (radio project) on: December 02, 2012, 05:59:04 am
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:
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:
/*
 *
 * 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;
    }
  }

}


9  Using Arduino / Interfacing w/ Software on the Computer / Re: Two way serial communication with raspberry Pi (radio project) on: December 02, 2012, 04:53:00 am
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

10  Using Arduino / Interfacing w/ Software on the Computer / Two way serial communication with raspberry Pi (radio project) on: December 02, 2012, 03:48:02 am
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:
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:
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:
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




11  Using Arduino / Displays / Re: shiftreg alternate IC on: March 12, 2011, 05:38:23 am
Any progress?
What kind of diode are you using?

Greetings,

Schwabinger
12  Community / Exhibition / Gallery / Re: SlugRadio: Arduino controls NSLU2-based internet radio player on: March 05, 2011, 04:49:31 am
Pushbutton for favourite stations
============================

Turning the station dial is lots of fun, but I wanted to have fast access to some favourite stations ... so I added some push-buttons.


8 Pushbottons by RigasW, on Flickr


I used a shiftIn registered as described in the tutorial here in arduino.cc to address the buttons.

If you want to save a station number to a button, you have to do the following:

- Select the sation by turning the tuning dial.
- Go into freeze mode
- Press the button, that you want to use for that station
- The station number is written in eeprom
- Exit freeze mode.

If you now press the station button, SlugRadio switches to the saved station. In addition, a led lights up, indicating, that you are listening to a programmed station.

Here is part of the code:

Code:
buttonState = digitalRead(buttonPin);

switch(StationKeyState) {

  case NonStationKeyMode:
 
  // you have to be in freeze mode;  then pressing the station button will save the station value im eeprom.
   
   if (((switchVar1 & B10000000) == B10000000) & programState == FreezeMode) {
   digitalWrite(ledPin, HIGH);
   delay(300);
   digitalWrite(ledPin, LOW);
   EEPROM.write(1, tunerValue);
   }
   
   if (((switchVar1 & B00000100) == B00000100) & programState == FreezeMode){
      digitalWrite(ledPin, HIGH);
      delay(300);
      digitalWrite(ledPin, LOW);
      EEPROM.write(2, tunerValue);
   }
     
    if (((switchVar1 & B00000010) == B00000010) & programState == FreezeMode){
      digitalWrite(ledPin, HIGH);
      delay(300);
      digitalWrite(ledPin, LOW);
      EEPROM.write(3, tunerValue);
   }
   
      if (((switchVar1 & B00001000) == B00001000) & programState == FreezeMode){
      digitalWrite(ledPin, HIGH);
      delay(300);
      digitalWrite(ledPin, LOW);
      EEPROM.write(4, tunerValue);
   }
   
   
   if (((switchVar1 & B00000001) == B00000001) & programState == FreezeMode){
      digitalWrite(ledPin, HIGH);
      delay(300);
      digitalWrite(ledPin, LOW);
      EEPROM.write(5, tunerValue);
   }
   
     if (((switchVar1 & B00010000) == B00010000) & programState == FreezeMode){
      digitalWrite(ledPin, HIGH);
      delay(300);
      digitalWrite(ledPin, LOW);
      EEPROM.write(6, tunerValue);
   }
   
   
   // pressing the station botton in regular operation will get the station value from eeprom.
   
 
if ((switchVar1 & B10000000) == B10000000){
   tunerValue = EEPROM.read(1);
   StationKeyState = StationKeyMode;
    digitalWrite(ledPin, HIGH);
  }

 if ((switchVar1 & B00000100) == B00000100){   
   tunerValue = EEPROM.read(2);
   StationKeyState = StationKeyMode;
    digitalWrite(ledPin, HIGH);
  }
   
    if ((switchVar1 & B00000010) == B00000010){   
   tunerValue = EEPROM.read(3);
   StationKeyState = StationKeyMode;
    digitalWrite(ledPin, HIGH);
  }
 
    if ((switchVar1 & B00001000) == B00001000){   
   tunerValue = EEPROM.read(4);
   StationKeyState = StationKeyMode;
    digitalWrite(ledPin, HIGH);
  }
   
   
       if ((switchVar1 & B00000001) == B00000001){   
   tunerValue = EEPROM.read(5);
   StationKeyState = StationKeyMode;
    digitalWrite(ledPin, HIGH);
  }
 
     if ((switchVar1 & B00010000) == B00010000){   
   tunerValue = EEPROM.read(6);
   StationKeyState = StationKeyMode;
    digitalWrite(ledPin, HIGH);
  }
   
   break;

case StationKeyMode

if ((switchVar1 & B10000000) == B10000000){ 
  StationKeyState = NonStationKeyMode;
     digitalWrite(ledPin, LOW);
  }
 
   if ((switchVar1 & B00000100) == B00000100){   
      StationKeyState = NonStationKeyMode;
      digitalWrite(ledPin, LOW);
  }
 
   if ((switchVar1 & B00000010) == B00000010){   
      StationKeyState = NonStationKeyMode;
      digitalWrite(ledPin, LOW);
  }


   if ((switchVar1 & B00001000) == B00001000){   
      StationKeyState = NonStationKeyMode;
      digitalWrite(ledPin, LOW);
  }

   if ((switchVar1 & B00000001) == B00000001){   
      StationKeyState = NonStationKeyMode;
      digitalWrite(ledPin, LOW);
  }


if ((switchVar1 & B00010000) == B00010000){   
      StationKeyState = NonStationKeyMode;
      digitalWrite(ledPin, LOW);
  }
 
     break;
     
  default:
  programState = NonStationKeyMode;
  break;
}

Enjoy!

Schwabinger





13  Using Arduino / Programming Questions / Re: Comparing ShiftIn Values on: February 28, 2011, 02:45:39 pm
Hi Robtillaart,

thanks for your help. I did not know about the B for "binary" ....

Now my SlugRadio has eight more station buttons.

Greetings,

Schwabinger
14  Using Arduino / Programming Questions / Re: Comparing ShiftIn Values on: February 27, 2011, 11:27:37 am
ok, let me be more precise:

If I do understand correctly, pressing the first button sends a 00000001 to the variable.
Pressing the second sends a 00000010, the third a 00000100 and so on.

I try to evaluate that using the following code:


Code:

  if (switchVar1 == 00000001){
      Serial.println("treffer: 1");
   
  }
 
if (switchVar1 == 0000010){
      Serial.println("treffer: 2");
   
  }
 
   
if (switchVar1 == 0000100){
      Serial.println("treffer: 3");

if (switchVar1 == 00001000){
      Serial.println("treffer: 4");
   
  }

if (switchVar1 == 00010000){
      Serial.println("treffer: 5");
   
  }
  if (switchVar1 == 00100000){
      Serial.println("treffer: 6");
   
  }
  if (switchVar1 == 01000000){
      Serial.println("treffer: 7");
   
  }

  if (switchVar1 == 10000000){
      Serial.println("treffer: 8");
   
  }


So when I press button 1, i get the result

"treffer 1" and the if-statement does what I expect.

But pressing the second, third ... button, never leads to a result like "treffer 2" or "treffer3".

Any ideas?

Schwabinger







15  Using Arduino / Programming Questions / Comparing ShiftIn Values on: February 27, 2011, 09:37:00 am
Hi,

I managed  successfully to built a shift in register with 8 push buttons.

The state of the buttons goes into a variable switchVar1:

Code:
byte switchVar1 = 72;  //01001000
..
switchVar1 = shiftIn(dataPin, clockPin);

I can show the value of the pressed button doing the following:

Code:
  Serial.println(switchVar1, BIN);


This results in an output between 1 and 10000000.

How can I use the variable switchVar1 in an If- or case-statement?

Thanks in advance,

Schwabinger




Pages: [1] 2 3