Two way comms

Hi all,

I have a question that involves a lot of inputs and outputs on the Arduino. My question is how do I go about selecting the right information for feeding into a program and sending the right information to the right output on the Arduino? My project has:

10 Linear Pots
8 buttons,
a joy stick like device (basically 2 pots)
RGB LEDS,

Heres a block diagram:

How do i go about making the outputs usable to a program being written in Python and the inputs usable too? This is the code I have so far and due to a lack of funds I cannot test to see if it works in any sense!

#include <avr/pgmspace.h>
 
int up_button = 0;
int down_button = 0;
int currentColorValueRed;
int currentColorValueGreen;
int currentColorValueBlue;

 // Init the Pins used for PWM
const int redPin = 5;
const int greenPin = 6;
const int bluePin = 7;

//Joystcik stuff
const int selectPin = 2;  
const int joystick_xPin = A0;  
const int joystick_yPin = A5;  
int oldX = 0;  
int oldY = 0;  
int oldSelect = 0; 

//Mux control pins
int s0 = 8;
int s1 = 9;
int s2 = 10;
int s3 = 11;

//Mux in "SIG" pin
int SIG_pin = 0;
    
void setup()  {
  Serial.begin(9600);
  pinMode(5, INPUT);
  pinMode(6, INPUT); 
  pinMode(7, INPUT);
  pinMode(8, INPUT);
  pinMode(9, INPUT); 
  pinMode(10, INPUT);
  pinMode(11, INPUT);
  pinMode(12, INPUT); 
  
  pinMode(s0, OUTPUT);
  pinMode(s1, OUTPUT);
  pinMode(s2, OUTPUT);
  pinMode(s3, OUTPUT); 

  digitalWrite(s0, LOW);
  digitalWrite(s1, LOW);
  digitalWrite(s2, LOW);
  digitalWrite(s3, LOW);
  
  
 // 
  digitalWrite(10, HIGH);
  
 //RBG LED Pins
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);
  
}

void loop()  {
  int joystick_x;  
  int joystick_y;  
  int select;
  
int fader0 = map(analogRead(0), 0, 1023, 0, 255);
int fader1 = map(analogRead(1), 0, 1023, 0, 255);
int fader2 = map(analogRead(2), 0, 1023, 0, 255);
int fader3 = map(analogRead(3), 0, 1023, 0, 255);
int fader4 = map(analogRead(4), 0, 1023, 0, 255);
int fader5 = map(analogRead(5), 0, 1023, 0, 255);
int fader6 = map(analogRead(6), 0, 1023, 0, 255);
int fader7 = map(analogRead(7), 0, 1023, 0, 255);
int fader8 = map(analogRead(8), 0, 1023, 0, 255);
int fader9 = map(analogRead(9), 0, 1023, 0, 255);

 down_button = digitalRead(7);  // read input value
  if (down_button == LOW) {         // check if the input is LOW (button pressed)
  Serial.print(1);
  } else {
  Serial.print(0);
  }
  
   up_button = digitalRead(8);  // read input value
  if (up_button == LOW) {         // check if the input is LOW (button pressed)
  Serial.print(1);
  } else {
  Serial.print(0);
  }
  
 joystick_x = map(analogRead(joystick_xPin), 0, 1023, 1, 20);
 joystick_y = map(analogRead(joystick_yPin), 0, 1023, 1, 20);

 select = !digitalRead(selectPin);

  if((oldX != joystick_x) || (oldY != joystick_y) || (oldSelect != select)){  
       Serial.print("joystick X: ");  
       Serial.print(joystick_x);  
       Serial.print(" joystick Y: ");  
       Serial.print(joystick_y);
       
       if(select){  
          Serial.print("select");  
       }  
       
       Serial.println("");  
       oldX = joystick_x;  
       oldY = joystick_y;  
       oldSelect = select;  
  }  
 delay(10);  
   
// Write the color to each pin using PWM and the value gathered above
  analogWrite(redPin, currentColorValueRed);
  analogWrite(bluePin, currentColorValueBlue);
  analogWrite(greenPin, currentColorValueGreen);
 
 //Loop through and read all 16 values
  //Reports back Value at channel 6 is: 346
  for(int i = 0; i < 16; i ++){
    Serial.print("Fader");
    Serial.print(i);
    Serial.print(": ");
    Serial.println(map(readMux(i), 0, 1023, 0, 255));
    delay(1000);
  }

}

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;
}

Parts of the code can only be finished when I get my Arduino. (When ever that will happen!)

Thanks in advanced,

Callum

  pinMode(8, INPUT);

pinMode(9, INPUT);
  pinMode(10, INPUT);
  pinMode(11, INPUT);
  pinMode(12, INPUT);
 
  pinMode(s0, OUTPUT);
  pinMode(s1, OUTPUT);
  pinMode(s2, OUTPUT);
  pinMode(s3, OUTPUT);

This doesn't make sense. You set s0-s3 to 8-11. So you first set pins 8-11 to INPUTs and then immediately to OUTPUTs.

In order to communicate between your python script and the Arduino, you'll probably have to create yourself a little protocol. Something where you send a character (or two) to tell the Arduino what input you want to know about and vice versa. (Or alternatively, you could simply stream the data non-stop and let the python script only pay attention to what it cares about.) It's up to you.

The Arduino code needs to be sorted that I know.

Do you have any links to where this is being discussed or posts that show how its done?

Do you really have ten analogue inputs, or did you mean to map a load of readMuxes?
Arrays. I really like arrays. And for loops.

AWOL:
Do you really have ten analogue inputs, or did you mean to map a load of readMuxes?
Arrays. I really like arrays. And for loops.

No. :blush: I changed something and thats no longer needed. This is the real code:

#include <avr/pgmspace.h>

// Init buttons
int up_button = 0;
int down_button = 0;

// Init vars for LEDs
int currentColorValueRed;
int currentColorValueGreen;
int currentColorValueBlue;

 // Init the Pins used for PWM
const int redPin = 5;
const int greenPin = 6;
const int bluePin = 7;

//Joystick stuff
const int selectPin = 2;  
const int joystick_xPin = A0;  
const int joystick_yPin = A5;  
int oldX = 0;  
int oldY = 0;  
int oldSelect = 0; 

//Mux control pins
int s0 = 8;
int s1 = 9;
int s2 = 10;
int s3 = 11;

//Mux in "SIG" pin
int SIG_pin = 0;
    
void setup()  {
  Serial.begin(9600);
  pinMode(5, INPUT);
  pinMode(6, INPUT); 
  pinMode(7, INPUT);
  pinMode(8, INPUT);
  pinMode(9, INPUT); 
  pinMode(10, INPUT);
  pinMode(11, INPUT);
  pinMode(12, INPUT); 
  
  pinMode(s0, OUTPUT);
  pinMode(s1, OUTPUT);
  pinMode(s2, OUTPUT);
  pinMode(s3, OUTPUT); 

  digitalWrite(s0, LOW);
  digitalWrite(s1, LOW);
  digitalWrite(s2, LOW);
  digitalWrite(s3, LOW);
  
  
 // 
  digitalWrite(10, HIGH);
  
 //RBG LED Pins
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);
  
}

void loop()  {
  int joystick_x;  
  int joystick_y;  
  int select;
  
 down_button = digitalRead(7);  // read input value
  if (down_button == LOW) {         // check if the input is LOW (button pressed)
  Serial.print(1);
  } else {
  Serial.print(0);
  }
  
   up_button = digitalRead(8);  // read input value
  if (up_button == LOW) {         // check if the input is LOW (button pressed)
  Serial.print(1);
  } else {
  Serial.print(0);
  }
  
 joystick_x = map(analogRead(joystick_xPin), 0, 1023, 1, 20);
 joystick_y = map(analogRead(joystick_yPin), 0, 1023, 1, 20);

 select = !digitalRead(selectPin);

  if((oldX != joystick_x) || (oldY != joystick_y) || (oldSelect != select)){  
       Serial.print("joystick X: ");  
       Serial.print(joystick_x);  
       Serial.print(" joystick Y: ");  
       Serial.print(joystick_y);
       
       if(select){  
          Serial.print("select");  
       }  
       
       Serial.println("");  
       oldX = joystick_x;  
       oldY = joystick_y;  
       oldSelect = select;  
  }  
 delay(10);  
   
// Write the color to each pin using PWM and the value gathered above
  analogWrite(redPin, currentColorValueRed);
  analogWrite(bluePin, currentColorValueBlue);
  analogWrite(greenPin, currentColorValueGreen);
 
 //Loop through and read all 16 values
  //Reports back Value at channel 6 is: 346
  for(int i = 0; i < 16; i ++){
    Serial.print("Fader");
    Serial.print(i);
    Serial.print(": ");
    Serial.println(map(readMux(i), 0, 1023, 0, 255));
    delay(1000);
  }

}

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;
}

:smiley:

Anyone got any help? :frowning:

Anyone? =\ Don't like to bump my posts.

To send many pieces of data you need a protocol. Both the receiver and transmitter need to recognize the format. This sketch will show you how (receive and parse) to read a group of data starting with SOP '<' and ending with EOP '>' .

An example output from your Arduino would be . The computer my send back .

I have saved this sketch that PaulS has created for serial comms. I call it serial_comms_perfect because for me it is perfect.

You may need to change your SOP and EOP...packet markers. You might need other changes to fit your situation. Look the code over and maybe you will see how to transmit serial in a manner that will work with this receiving code.

#define SOP '<'
#define EOP '>'

bool started = false;
bool ended = false;

char inData[80];
byte index;

void setup()
{
   Serial.begin(9600);
   // Other stuff...
}

void loop()
{
  // Read all serial data available, as fast as possible
  while(Serial.available() > 0)
  {
    char inChar = Serial.read();
    if(inChar == SOP)
    {
       index = 0;
       inData[index] = '\0';
       started = true;
       ended = false;
    }
    else if(inChar == EOP)
    {
       ended = true;
       break;
    }
    else
    {
      if(index < 79)
      {
        inData[index] = inChar;
        index++;
        inData[index] = '\0';
      }
    }
  }

  // We are here either because all pending serial
  // data has been read OR because an end of
  // packet marker arrived. Which is it?
  if(started && ended)
  {
    // The end of packet marker arrived. Process the packet
    Serial.print(inData);
    // Reset for the next packet
    started = false;
    ended = false;
    index = 0;
    inData[index] = '\0';
  }
}
int s0 = 8;
int s1 = 9;
int s2 = 10;
int s3 = 11;
...
pinMode(8, INPUT);
pinMode(9, INPUT); 
pinMode(10, INPUT);
pinMode(11, INPUT);
  
pinMode(s0, OUTPUT);
pinMode(s1, OUTPUT);
pinMode(s2, OUTPUT);
pinMode(s3, OUTPUT);

WTF?

It was mentioned above but you seem to have ignored it.


Rob

Graynomad:

int s0 = 8;

int s1 = 9;
int s2 = 10;
int s3 = 11;
...
pinMode(8, INPUT);
pinMode(9, INPUT);
pinMode(10, INPUT);
pinMode(11, INPUT);
 
pinMode(s0, OUTPUT);
pinMode(s1, OUTPUT);
pinMode(s2, OUTPUT);
pinMode(s3, OUTPUT);



WTF? 

It was mentioned above but you seem to have ignored it.

______
Rob

Hi Rob,

As I said until I get my Ardiuno I cant sort out the pin allocation fully. I know that just now its a real mess.

@cyclegadget

I will look at that and see if I can decipher it... May be back.

OK, then follow what cyclegadget said and get back to us when you get stuck.


Rob