PWM with MUX Shield II

Status
Not open for further replies.
Hey everyone!

So, I'm working on a project where I use 4 muxs, one for voltage, one for temperature, one for amps and another for PWM. I have 30 Servos turning and a sensor in each one to record their PWM, the AMS AS5048B. The voltage, temperature and amps, I got them easily. But the PWM I'm not finding the way to get it properly.

I leave here my code of what I have tried so far.

Code:
#include "PWM.hpp"

#define CONTROL0 17
#define CONTROL1 16
#define CONTROL2 15
#define CONTROL3 14

PWM ch1(A9);
int mux0array[16] = {0};
int mux0array1[14] = {0};

void setup() 
{
 pinMode(CONTROL0, OUTPUT);
 pinMode(CONTROL1, OUTPUT);
 pinMode(CONTROL2, OUTPUT);
 pinMode(CONTROL3, OUTPUT);
 pinMode(A9, INPUT);
 ch1.begin(true);
 Serial.begin(9600);
}

void loop() 
{
  pwm_read();
}

void pwm_read()
{
  for (int i = 0; i<16; i++)
  {
    digitalWrite(CONTROL0, (i&15)>>3); 
    digitalWrite(CONTROL1, (i&7)>>2);  
    digitalWrite(CONTROL2, (i&3)>>1);  
    digitalWrite(CONTROL3, (i&1));  
    mux0array[i] = analogRead(A9);
    //mux0array1[i] = analogRead(A8);
  }
 
  for (int i = 0; i<2; i++)
  {
   mux0array[0] = ch1.getValue();
   Serial.print(mux0array[0]); 
   Serial.print("\t");
   mux0array[1] = ch1.getValue();
   Serial.print(mux0array[1]);
   Serial.println("\t");
   delay(250);
  }

}

I'm only triyng with two servos to see if it works. I was hoping if any of you could helpm, tell me what I'm doing wrong and guidem, please

Thank you,
Jonathan
 
You appear to be reading PWM with a library that uses pin change interrupts. Your analog read on pin A9 changes the interrupt pin from digital to analog and it may no longer respond to pin changes( depending upon the underlying hardware ). The sensor runs at 1khz, so you need to keep in mind that it will take at least two milliseconds after changing the mux select pins before you will have a valid reading from the pin changes.

Also of note in your example, mux0array1[14] defined as 14 in size instead of 16 is a buffer overflow if you uncomment the code in your loop. And your example leaves the mux select pins selecting 15 where it appears you wish to be reading the 0 and 1 paths through the mux.
 
Status
Not open for further replies.
Back
Top