Go Down

Topic: Pot --> Mux --> MCU --> Servos??? (Read 542 times) previous topic - next topic

tt_tony

I'm not sure how to write the code to get the MCU to look at each of the "channels", convert that info to a PWM and send it out to the servos.

I was going to combine the analog input mux code with the "servo knob" example code. 

Here is the mux code I'm using:

Code: [Select]
//Mux control pins
int s0 = 10;
int s1 = 11;
int s2 = 13;
int s3 = 14;

//Mux in "SIG" pin
int SIG_pin = 0;

void setup(){
  pinMode(s0, OUTPUT);
  pinMode(s1, OUTPUT);
  pinMode(s2, OUTPUT);
  pinMode(s3, OUTPUT);

  digitalWrite(s0, LOW);
  digitalWrite(s1, LOW);
  digitalWrite(s2, LOW);
  digitalWrite(s3, LOW);

  Serial.begin(9600);
}

void loop(){

  //Loop through and read all 16 values
  //Reports back Value at channel 6 is: 346
  for(int i = 0; i < 16; i ++){
    Serial.print("Value at channel ");
    Serial.print(i);
    Serial.print("is : ");
    Serial.println(readMux(i));
    delay(100);
  }

}

int readMux(int channel){
  int controlPin[] = {s0, s1, s2, s3};

  int muxChannel[16][4]={
    {0,0,0,0}, //channel 0
    {1,0,0,0}, //channel 1
    {0,1,0,0}, //channel 2
    {1,1,0,0}, //channel 3
    {0,0,1,0}, //channel 4
    {1,0,1,0}, //channel 5
    {0,1,1,0}, //channel 6
    {1,1,1,0}, //channel 7
    {0,0,0,1}, //channel 8
    {1,0,0,1}, //channel 9
    {0,1,0,1}, //channel 10
    {1,1,0,1}, //channel 11
    {0,0,1,1}, //channel 12
    {1,0,1,1}, //channel 13
    {0,1,1,1}, //channel 14
    {1,1,1,1}  //channel 15
  };

  //loop through the 4 sig
  for(int i = 0; i < 4; i ++){
    digitalWrite(controlPin[i], muxChannel[channel][i]);
  }

  //read the value at the SIG pin
  int val = analogRead(SIG_pin);

  //return the value
  return val;
}


...and here is the "servo knob" example code that everyone has:

Code: [Select]
// Controlling a servo position using a potentiometer (variable resistor)
// by Michal Rinott <http://people.interaction-ivrea.it/m.rinott>

// NOTE: UART will be disabled when servo is attached to pin 0 or 1.


#include <Servo.h>


Servo myservo;  // create servo object to control a servo

int potpin = 0;  // analog pin used to connect the potentiometer
int val;    // variable to read the value from the analog pin

void setup()
{
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
}

void loop()
{
  val = analogRead(potpin);            // reads the value of the potentiometer (value between 0 and 1023)
  val = map(val, 0, 1023, 0, 179);     // scale it to use it with the servo (value between 0 and 180)
  myservo.write(val);                  // sets the servo position according to the scaled value
  delay(15);                           // waits for the servo to get there
}


Also, I plan on using 6 muxes, how do I get it to look at the different "sig" pins?

Thanks, I'm new.  :)


johnwasser

So you have 6 16-channel MUX's connected to the 6 analog input pins?  That's 96 input values.

How do you connect the 96 servos?!?
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

tt_tony

Right, I'm not going to be able to use all of the muxes, any help with the actual question?

johnwasser


Right, I'm not going to be able to use all of the muxes, any help with the actual question?


Change readMux() to take both the current mux address (0-15) and an analog input pin (0-5).

Use readMux(muxChannel, analogPin) in place of analogRead(potpin) in the Servo Knob example.  For each combination of muxChannel and analogPin you'll need a Servo object.  I'd put them in an array for easy access.

Code: [Select]

Servo servoTable[96];
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

tt_tony

thanks for that John, I'll try it tonight.  When I was buying the multiplexers I forgot that I wasnt going to be able to use all 83 of my pins.  I guess if i ever get to that point that I would need as many servos as possible I could use one board as a transmitter and two as receivers.

Go Up