The project is "completed" and in use. It is a game controller with a bunch of joysticks, for use in space games and other flight simulators and FPV simulators. It works well, except the connection to the pc is shaky. I don't know why, but it failsafes (an aspect of the vjoyserialfeeder app used to connect to the pc) a lot, forcing me to "disconnect" then "reconnect" in the app to bring it back.
Is it possible that the arduino is not running smoothly because I have too many loads on the 5v? If so what can I do? I have a similar project that is smaller, only 4 joysticks, and no additional potentiometers, it never has this problem. (sometimes I bump the usb connector, but it's a different situation, and it's just that I wore out the usb connector by now).
You can view the vjoyserialfeeder project (and it's code) here:
my specific sketch (by editing the one from vjoyserialfeeder) is here:
#include <CD74HC4067.h>
#include "ibus.h"
// //////////////////
// Edit here to customize
// How often to send data?
#define UPDATE_INTERVAL 10 // milliseconds
// 1. Analog channels. Data can be read with the Arduino's 10-bit ADC.
// This gives values from 0 to 1023.
// Specify below the analog pin numbers (as for analogRead) you would like to use.
// Every analog input is sent as a single channel.
#define NUM_ANALOG_INPUTS 16
byte analogPins[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,12,13,14,15}; // element count MUST be == NUM_ANALOG_INPUTS
#define NUM_OTHER_ANALOG 2
// 2. Digital channels. Data can be read from Arduino's digital pins.
// They could be either LOW or HIGH.
// Specify below the digital pin numbers (as for digitalRead) you would like to use.
// Every pin is sent as a single channel. LOW is encoded as 0, HIGH - as 1023
#define NUM_DIGITAL_INPUTS 13
byte digitalPins[] = {2,3,4,5,6, 7,8,9,10,11,12,13,A5}; // element count MUST be == NUM_DIGITAL_INPUTS
// 3. Digital bit-mapped channels. Sending a single binary state as a 16-bit
// channel is pretty wasteful. Instead, we can encode one digital input
// in each of the 16 channel bits.
// Specify below the digital pins (as for digitalRead) you would like to send as
// bitmapped channel data. Data will be automatically organized in channels.
// The first 16 pins will go in one channel (the first pin goes into the LSB of the channel).
// The next 16 pins go in another channel and so on
// LOW pins are encoded as 0 bit, HIGH - as 1.
#define NUM_DIGITAL_BITMAPPED_INPUTS 0
byte digitalBitmappedPins[] = {}; // element count MUST be == NUM_DIGITAL_BITMAPPED_INPUTS
// Define the appropriate analog reference source. See
// https://www.arduino.cc/reference/en/language/functions/analog-io/analogreference/
#define ANALOG_REFERENCE DEFAULT
// Define the baud rate
#define BAUD_RATE 115200
// /////////////////
#define NUM_CHANNELS ( (NUM_ANALOG_INPUTS) + (NUM_DIGITAL_INPUTS) + (NUM_OTHER_ANALOG) + (15 + (NUM_DIGITAL_BITMAPPED_INPUTS))/16 )
CD74HC4067 my_mux(A0, A1, A2, A3);
const int signal_pin = A4; // Pin A4 - Connected to Sig pin of CD74HC4067
IBus ibus(NUM_CHANNELS);
void setup()
{
analogReference(ANALOG_REFERENCE); // use the defined ADC reference voltage source
Serial.begin(BAUD_RATE); // setup serial
pinMode(A0, OUTPUT);
pinMode(A1, OUTPUT);
pinMode(A2, OUTPUT);
pinMode(A3, OUTPUT);
}
void loop()
{
int i, bm_ch = 0;
unsigned long time = millis();
ibus.begin();
// read analog pins - one per channel
for(i=0; i < NUM_ANALOG_INPUTS; i++) {
//my_mux.channel(6);
//Serial.println("-" + String(i));
my_mux.channel(analogPins[i]);
//int val = analogRead(signal_pin);
//Serial.println("Channel "+String(i)+": "+String(val));
ibus.write(analogRead(signal_pin));
// Serial.println(String(analogPins[i]) + " - " + String(analogRead(signal_pin)));
// Serial.println(analogPins[i]);
// read digital pins - one per channel
}
ibus.write(analogRead(A6));
ibus.write(analogRead(A7));
for(i=0; i < NUM_DIGITAL_INPUTS; i++)
ibus.write(digitalRead(digitalPins[i]) == HIGH ? 1023 : 0);
//Serial.println("--" + String(i));
// read digital bit-mapped pins - 16 pins go in one channel
for(i=0; i < NUM_DIGITAL_BITMAPPED_INPUTS; i++) {
int bit = i%16;
if(digitalRead(digitalBitmappedPins[i]) == HIGH)
bm_ch |= 1 << bit;
if(bit == 15 || i == NUM_DIGITAL_BITMAPPED_INPUTS-1) {
// data for one channel ready
ibus.write(bm_ch);
//Serial.println("--");
bm_ch = 0;
}
}
ibus.end();
//Serial.println("end");
time = millis() - time; // time elapsed in reading the inputs
if(time < UPDATE_INTERVAL)
// sleep till it is time for the next update
delay(UPDATE_INTERVAL - time);
}



