0

Trying to send commands from an Arduino to the RPi through the GPIO PCM. I can't do it through the serial port as the port is connected to a computer and commanding the Arduino. I was planning to send single hex codes (0011,1010,1101,etc), through the GPIO but it doesn't appear to be working as intended. I wrote the following code in the Arduino to send the signal.

const int RPiPins[3][3] = {{1,2,4},
                           {2,5,6},
                           {3,7,8}
                          };

/* Define input pins from the RPI Army */
const int RPiInPins[3][3] = {{1,A0,A1},
                             {2,A2,A3},
                             {3,A4,A5}
                            };

int ComControlValue = -1; // switch control
int signalDelay = 10; // delay for output

void setup() {
  Serial.begin(115200);           // Set serial port to receive input

  /* Set pins to output*/
  pinMode(RPiPins[0][1],OUTPUT); 
  pinMode(RPiPins[0][2],OUTPUT);   
  pinMode(RPiPins[1][1],OUTPUT);        
  pinMode(RPiPins[1][2],OUTPUT);         
  pinMode(RPiPins[2][1],OUTPUT);
  pinMode(RPiPins[2][2],OUTPUT);   
  /* Make sure all pins are set to 0 volts*/
  CallPins(0,0,0);   
  CallPins(1,0,0);  
  CallPins(2,0,0);
}

void loop() {
  switch(ComControlValue){
    case -1:
       Serial.println("Waiting for Command");
       while (Serial.available()==0){}
       ComControlValue=Serial.parseInt();
       break;     
    case 1:
       InputRequest();
       SendBinaryInput(1010);   // Sending  Input To RPi
       ComControlValue= -1; 
       break;
  }
}

void SendBinaryInput(int args) {
    int counter = 3;
    int I1;
    int ii;
    int PinsOut;

    while (counter > -1) {
      I1 = args / (int)pow(10,counter) % 10 ;                                  
    // Pulls out the digits individually
      delay(signalDelay);

      if (I1 == 1) {
        CallPins(0,1,1);
        CallPins(1,1,1);
        CallPins(2,1,1);
        counter--;
      }
      else if (I1 == 0) {
        CallPins(0,0,1);
        CallPins(1,0,1);
        CallPins(2,0,1);
       counter--;
      }
    delay(signalDelay);
    CallPins(0,0,0);
    CallPins(1,0,0);
    CallPins(2,0,0);
    delay(signalDelay);
  }
}

/* Simple function for calling Pins based on RPi */
void CallPins(int args,int PinA, int PinB) {
   digitalWrite(RPiPins[args][1],PinA);
   digitalWrite(RPiPins[args][2],PinB); 
}

void InputRequest(){
    CallPins(0,1,1);
    CallPins(1,1,1);
    CallPins(2,1,1);
    delay(signalDelay*2);
    CallPins(0,0,0);
    CallPins(1,0,0);
    CallPins(2,0,0);
    delay(signalDelay*2);
}

code is a bit long. I pretty much setup so the output from the Arduino is 2 pins (for 1) or 1 pin (for 0). I even have delays set to allow for the RPi to receive the command. The code works well when I attach the output pins to LEDS.

As for the RPi, Here is the code I wrote to receive the inputs. The code I wrote though is in Python which might be part of the problem.

import RPi.GPIO as GPIO
from RPi.GPIO import LOW, OUT, HIGH, BCM
from threading import Thread
import time

Listen = True        # Value for loop to run
ComArd = [0,0,0,0]       # Command from Arduino to Follow
OutPins = [22,27]
InPins = [6, 12]

GPIO.setmode(BCM)        # Set GPIO to BCM pin layout
GPIO.setup(InPins[0],IN)
GPIO.setup(InPins[1],IN)

def Listening(Listen):
    while Listen:
        if GPIO.input(InPins[0]) and GPIO.input(InPins[1]):
            ComArd = RetrieveCommand()
            print(ComArd)
        elif GPIO.input(InPins[0]) and not GPIO.input(InPins[1]):
            pass
        elif GPIO.input(InPins[1]) and not GPIO.input(InPins[0]):
            pass
def RetrieveCommand():
    EventListen = True         # For loop to receive input
    BinColl = 0
    while EventListen:
        if GPIO.input(InPins[0]) and not GPIO.input(InPins[1]):
            ComArd[BinColl] = 0
            BinColl = BinColl + 1
            time.sleep(0.01)
        elif GPIO.input(InPins[1]) and GPIO.input(InPins[0]):
        ComArd[BinColl] = 1
        BinColl = BinColl + 1
        time.sleep(0.01)
        if BinColl == 3:
            EventListen = False
    EventListen = True
    return ComArd
  • Can you send a serial data bit stream at something like 19k2 baud? If so you could use pigio to receive the data. Try sudo pigpiod then pigs slro 23 19200 8 where 23 is the GPIO to use and 8 is the bits per byte. Then send some data and try pigs slr 23 100 where 23 is the GPIO and 100 means read up to a 100 bytes. If that works you can do the same in C and Python. – joan May 30 '18 at 7:27
  • I think it would be fine. I want to send a 4 or 8 bit code to notify the RPi to run function X or Y. – Hojo.Timberwolf May 30 '18 at 7:38
  • I meant to say I didn't look at your current code but in principal the idea behind it should work. – joan May 30 '18 at 7:43
  • Why not simply use either the I2C or SPI busses to send the bytes across? – stevieb May 30 '18 at 13:19
  • Having abandoned the Arduino for the Raspberry Pi myself, I am really curious about your application. What is the Arduino doing? My Pi does I2C, W1, closes relays, etc. – OyaMist May 30 '18 at 13:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.