I made this code to control multiple motors at the same time :
#define MAXTAILLE 50
int valMoteur1; // Status of the motor
class Timer{
public:
void start() {
t0 = millis();
t1 = t0;
}
void reset() { start(); }
int get_ms() {
t1 = millis();
return t1-t0;
}
float get_sec() {
return float(get_ms())/1000.0f;
}
private:
int t0, t1;
};
class Sequencer{
public:
Timer timer;
float durations[MAXTAILLE];
int states[MAXTAILLE];
int index = 0;
int taille = 0;
void add(int stat, float durms) {
states[taille] = stat;
durations[taille] = durms;
taille++;
}
void start() {
timer.start();
}
bool check() {
if (timer.get_ms() >= durations[index]) {
index+=1;
if (index >= taille) index = 0;
timer.reset();
return true;
}
return false;
}
};
class Continu{
public:
String type = "continu";
int pin;
int state = 0; // arrêt
Sequencer seq;
String name;
Continu(String _name, int npin) {
name = _name;
pin = npin;
init();
}
void display() {
int newstate = seq.states[seq.index];
float dursec = seq.durations[seq.index]/1000.0f;
Serial.println(name);
Serial.println(int(newstate));
Serial.println(float(dursec));
//std::cout << "set state of " << name << " to " << state << " for " << dursec << " seconds" << std::endl;
}
void start() {
seq.start();
display();
}
void check() {
if (seq.check()) {
set_state(seq.states[seq.index]);
display();
}
}
void init() {
pinMode(pin, OUTPUT);
}
void add(int stat, float dursec) {
seq.add(stat, dursec*1000.0f);
}
void set_state(int _state) {
state = _state;
Serial.println(state);
if (state == 0) digitalWrite(pin, LOW);
if (state == 1) digitalWrite(pin, HIGH);
}
};
Continu p1 = Continu("p1", 2);
Continu p2 = Continu("p2", 3);
Continu p3 = Continu("p3", 4);
void setup() {
Serial.begin(9600);
p1.add( valMoteur1 = 0, 2 );
p1.add( valMoteur1 = 1, 2 );
p1.start();
p2.add( 0, 2);
p2.add( 1, 2);
p2.start();
p3.add( 0, 2);
p3.add( 1, 2);
p3.start();
}
void loop() {
p1.check();
p2.check();
p3.check();
}
Everything works but now I would like to read the state of the motor in real time from Supercollider. To be clear I want to ask SuperCollider to do something if the motor is running and to stop when the motor stops.
I wrote this on SC to read from the Arduino USB port :
Well I'll be in SC controlling arguments from multiple Synths depending of the state of each motor. The thing is I don't really know how to read and keep actualised those values from Arduino. Should I use digitalWrite & digitalRead at some point ? How do I read the three different values (from the three motors) independently ? Should I complete the code inside my 'check' function or in the loop ?
The result is quite simple, I'm in this case controlling 3 DC motors wired with mosfets. I wrote this code a few years ago for an installation driving 9 motors, I haven't code in Arduino or C since.
The void setup is just filled with values in seconds to give informations to the motors :
p1.add( valMoteur1 = 0, 2 ); // stop the motor for 2 sec
p1.add( valMoteur1 = 1, 10 ); // start the motor for 10 sec
p1.add( valMoteur1 = 0, 10 ); // stop the motor for 10 sec etcetcetc....
p1.start();
p2.add( 0, 2);
p2.add( 1, 2);
p2.start();
p3.add( 0, 2);
p3.add( 1, 2);
p3.start();
I wrote this to be able to easely control the multiple motors at the same time. The check function just verify the state of the motor accordingly to the timer set when the board gets switch on.
I assume this wasn't the prettiest way to do it but this is how I dit it at the time...
you should do what you feel comfortable with. when code is reviewed professionally, it needs to use approaches that are familiar to those reviewing the code and who may need to maintain it in the future
for such a small program, i question whether explicit classes makes it easier to understand. (is there need for more than one)? just using classes doesn't make it OOD.
i copied the code i posted from another thread, cutting the OP's code size in half and showing how the code could be simpler.
where each object requires different parameters, i typically define a struct and statically initialize an array of structs. each line specifies the parameter. adding an additional object is just a single line in the code.
of course there are functions to operate on the struct (i.e. methods).
you now need to add code to read serial input, interpret it and generate a response