yjl
Posts: 10
Joined: Sun Aug 06, 2017 7:53 pm

programe multiple GPIO control [arduino>Rpi]

Sun Aug 06, 2017 8:16 pm

Hi,

I am making the circuit to control 6 relays in different on/off period and duty cycle depends on a real time data below..

5 5 2 1 1 1
3 4 2 1 0 0
4 4 2 1 0 0
4 5 4 2 1 2
4 5 4 2 0 1
...

Those are originally arrays[0-5], I printed for checking. Anyway I was supposed to use Arduino to control relays while Rpi feed those data by serial.
But now I raised question why I don't to use GPiO pins directly. The problem is that I know very basic of Python/Rpi. I tried to figure out how to control multiple LED(I say LED for convenience, I can replace them into relay later) in different time, but failed. Basic I idea is I want to control each LED in different On, Off time. Below is my proto-type arduino code.

Basic idea is making function for flickering LED with variables. Could anyone help me? Any advice would be appreciated. Thank you!

Code: Select all

const byte TestPins[]={8, 9, 10, 11, 12, 13};

unsigned long BlankTimes[] = {30,  100,    500,   50,    700,   200}; // off time
unsigned long CycleTimes[] = {80, 1000, 1000, 1000, 3000, 1000};  // total cycle time (on+off)

unsigned long startMillis[] = {0,-100, -30, 0, 0, 0}; //initialize pulse beginning point to shift from other channel 

void setup() {              
  for (byte i = 0 ; i < sizeof(TestPins) ; i++){
    pinMode(TestPins[i] , OUTPUT);
  }
  Serial.begin(9600);
}

void loop() {
 for(byte i = 0; i < sizeof(TestPins); i++){
 updateLed(TestPins[i], startMillis[i], BlankTimes[i], CycleTimes[i]);
}

void updateLed(const byte LedPin, unsigned long &timer, unsigned long BTime, unsigned long CTime) {
  const bool LedState = digitalRead(LedPin);
  if(!LedState && (millis() - timer >= BTime)){
    digitalWrite(LedPin, HIGH);
  }
  //if on we wait till the cycle is over
  else if(LedState && (millis() - timer) >= CTime){
    timer = millis();  //restart cycle
    digitalWrite(LedPin, LOW);
  }
}

yjl
Posts: 10
Joined: Sun Aug 06, 2017 7:53 pm

Re: programe multiple GPIO control [arduino>Rpi]

Mon Aug 07, 2017 6:14 am

Still looking for solutions. I found piGPIO, I guess I can make variety pulse signal with multi channel. Am I going proper way?

User avatar
DougieLawson
Posts: 30501
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website

Re: programe multiple GPIO control [arduino>Rpi]

Mon Aug 07, 2017 7:10 am

You realise that this forum isn't here to do your homework/coursework for you. Do your own work, do your own research.

https://www.raspberrypi.org/blog/gpio-zero-update/
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

Since 2012: 1B*5, 2B*2, B+, A+, Zero*2, 3B*3

Please post ALL technical questions on the forum. Do not send private messages.

scotty101
Posts: 2389
Joined: Fri Jun 08, 2012 6:03 pm

Re: programe multiple GPIO control [arduino>Rpi]

Mon Aug 07, 2017 10:22 am

Code: Select all

unsigned long startMillis[] = {0,-100, -30, 0, 0, 0};
You are putting negative number in to an unsigned variable type. This is asking for trouble.
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

yjl
Posts: 10
Joined: Sun Aug 06, 2017 7:53 pm

Re: programe multiple GPIO control [arduino>Rpi]

Fri Aug 11, 2017 10:23 am

Just for someone who is looking for same solution.

I've tried pigpio & piscope library. It is fantastic but if you want to control PWM in unsual freq (much slower than usual..etc), go for gpio zero.

Gpio zero is also very simple to use. I had some troubles with writing code as very beginner. but as you know python is very intuitive, so it seems going okay.

In the end, I control the GPIOs by GPIO Zero but when I monitor them I used PISCOPE. it was very useful to analyse without circuit for simulation.

Return to “Python”

Who is online

Users browsing this forum: No registered users and 13 guests