Reading Multiple Voltages Through Serial [Arduino]
I am attempting to create a motorized rotary potentiometer connected to max/MSP for recall of parameters. I have gotten one potentiometer to work, but when I try it with two one of the pots controls the other. Max is not reading the information for both Voltages separately. Attached are my patches and circuit.
The Alps pot pins are labeled, left to right: Dummy|A-read|GND|5v|A-read|GND|5v|Dummy
This is the code for ONE potentiometer:
const int servoPos=7;
const int servoNeg=8;
float potVolt = A1;
void setup(){
Serial.begin(9600);
pinMode(servoPos, OUTPUT);
pinMode(servoNeg, OUTPUT);
}
void loop(){
byte positive;
float potVolt = analogRead(A1)/ 4;
if (Serial.available()) {
positive = Serial.read();
//threshold of +/- 1.5 so pot doesn't bounce around
if (potVolt > (positive + 1.5)) {
digitalWrite(servoPos, LOW);
digitalWrite(servoNeg, HIGH);
}
else if (potVolt < (positive - 1.5)) {
digitalWrite(servoPos, HIGH);
digitalWrite(servoNeg, LOW);
}
if (potVolt == positive) {
digitalWrite(servoNeg, LOW);
digitalWrite(servoPos, LOW);
}
}
Serial.print(positive);
Serial.print(" ");
Serial.println(potVolt);
}
and this is the code I attempted for two. You can view the voltages in the IDE Serial Monitor, but in Max/MSP it is sending them both to one integer object and interpolating the voltages.
const int servoPos1=2;
const int servoNeg1=3;
const int servoPos2=7;
const int servoNeg2=8;
byte pot1,pot2;
float potVolt1 = A5;
float potVolt2 = A1;
void setup(){
Serial.begin(9600);
pinMode(servoPos1, OUTPUT);
pinMode(servoNeg1, OUTPUT);
pinMode(servoPos2, OUTPUT);
pinMode(servoNeg2, OUTPUT);
}
void loop(){
float potVolt1 = analogRead(A1)/ 4;
float potVolt2 = analogRead(A5)/ 4;
while (Serial.available()) {
pot1 = Serial.read();
pot2 = Serial.read();
if (pot1 > (potVolt1 + 1.5)) {
digitalWrite(servoPos1, LOW);
digitalWrite(servoNeg1, HIGH);
}
else if (pot1 < (potVolt1 - 1.5)) {
digitalWrite(servoPos1, HIGH);
digitalWrite(servoNeg1, LOW);
}
if (pot1 == potVolt1) {
digitalWrite(servoNeg1, LOW);
digitalWrite(servoPos1, LOW);
}
if (pot2 > (potVolt2 + 1.5)) {
digitalWrite(servoPos2, LOW);
digitalWrite(servoNeg2, HIGH);
}
else if (pot2 < (potVolt2 - 1.5)) {
digitalWrite(servoPos2, HIGH);
digitalWrite(servoNeg2, LOW);
}
if (pot2 == potVolt2) {
digitalWrite(servoNeg2, LOW);
digitalWrite(servoPos2, LOW);
}
}
Serial.print(pot1);
Serial.print(" ");
Serial.println(potVolt1);
Serial.print(pot2);
Serial.print(" ");
Serial.println(potVolt2);
}
*also note that in my diagram, 123dcircuit doesn't have a pot and motor joined, so I had to use them as separate pieces in the image, but they are ONE UNIT in real life.*Arduino-Breadboard connections
If you're willing to re-visit the code, then I recommend looking at the arduivis system. It's really easy to work with.
I will check it out when I get a chance! I was under the impression that I didn't really need anything much more than the Serial object in Max and if Arduino is coded right it will send the integers (ASCII) to the proper outlet of the [unpack]. I'm not good at troubleshooting code yet becuase I don't know a whole lot. Would changing it to Serial.parseInt() be the way to go in my code? Like I said, with one potentiometer everything works perfectly how I want. With two, the number is being sent to the same output (middle) of the [unpack] :( lol. This is a part of my thesis and I've been doing a lot of research, but what I'm attempting has absolutely no documentation, and I don't think I'm searching with the proper syntax to receive the right results in google.
...that's why I'm recommending arduivis instead of reinventing the wheel.
Whoops, sorry I should have stated it better. I meant that if Arduino was coded right in terms of just having your outputs set and whatever you're sending to Serial.read(). From what Isaw with Arduivis it pretty much looks like what I did, but it's sending it back and forth through the arduino. I'll test it out when I am back at home ^_^