Floppy disc controlled by Pure Pata

Hi all,
I am trying to build a floppy disk, controlled by Pure Data. I'm new to Arduino and quite experienced in Pure Data.

Based on code fragments found on this forum, I have created working code:

#define Dir 3
#define Step 6
#define Sel 4
int incomingByte = 0;
void setup() {
  Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
  pinMode(Dir, OUTPUT); 
  pinMode(Step, OUTPUT);
  pinMode(Sel, OUTPUT);
  digitalWrite(Sel, HIGH);
  initHead();  // move header half way
}

void initHead() {
  digitalWrite(Step, HIGH);
  digitalWrite(Dir, HIGH); 
  doSteps(80, 1911);  // move header to start position wherever it is, 80 steps at ~261 step/s
  digitalWrite(Dir, LOW);
  doSteps(40, 1911);
  delay(1000);
}

void doSteps(int steps, int stepDelay) {
  digitalWrite(Sel, LOW);
  for(int i=0;i<steps; i++) {
    digitalWrite(Step,LOW); 
    delayMicroseconds(stepDelay);
    digitalWrite(Step,HIGH); 
    delayMicroseconds(stepDelay);

  }
  digitalWrite(Sel, HIGH);
}

void vibe(int count, int period) {
  for(int l=0;l<count; l++) {
    digitalWrite(Dir, HIGH);
    doSteps(1, period);
    digitalWrite(Dir, LOW);
    doSteps(1, period);
  }
}

void loop() {
  if (Serial.available()>0) {
    incomingByte=Serial.read();
    digitalWrite(Step, incomingByte);
    
   
    
  vibe(30, 3823); //D
  vibe(35, 3214);
  vibe(40, 2552);
  
  }
  
  }

By sending any number from PD, it runs a sequence of three sounds.

vibe (30, 3823);
vibe (35, 3214);
vibe (40, 2552);

(d minor, BTW)

I just need to find a way to send a specific number from PD to run a specific line of code, rather than all at once.

Thanks in advance for your help!

I just need to find a way to send a specific number from PD to run a specific line of code, rather than all at once.

You read what is being sent, and then ignore it. You then unconditionally execute 3 statements. Look at the reference page and learn how to use an if statement to conditionally execute code.

ok, I will begin study "if" :slight_smile:

The problem is that I don't know what message sends PD and how to combine it with Arduino code. I made a clumsy code based on two examples:

#define Dir 3
#define Step 6
#define Sel 4
int incomingByte = 0;
void setup() {
  Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
  pinMode(Dir, OUTPUT); 
  pinMode(Step, OUTPUT);
  pinMode(Sel, OUTPUT);
  digitalWrite(Sel, HIGH);
  initHead();  // move header half way
}

void initHead() {
  digitalWrite(Step, HIGH);
  digitalWrite(Dir, HIGH); 
  doSteps(80, 1911);  // move header to start position wherever it is, 80 steps at ~261 step/s
  digitalWrite(Dir, LOW);
  doSteps(40, 1911);
  delay(1000);
}

void doSteps(int steps, int stepDelay) {
  digitalWrite(Sel, LOW);
  for(int i=0;i<steps; i++) {
    digitalWrite(Step,LOW); 
    delayMicroseconds(stepDelay);
    digitalWrite(Step,HIGH); 
    delayMicroseconds(stepDelay);

  }
  digitalWrite(Sel, HIGH);
}

void vibe(int count, int period) {
  for(int l=0;l<count; l++) {
    digitalWrite(Dir, HIGH);
    doSteps(1, period);
    digitalWrite(Dir, LOW);
    doSteps(1, period);
  }
}

void loop() {
  if (Serial.available()>0) {
    incomingByte=Serial.read();
    analogWrite(Dir, incomingByte);
    
 if (incomingByte == 50);
    
 { vibe(30, 3823); }
  
  }
  
  }

At this point, it works in such way, that clicking anything in the PD runs what is written there. I'd like to send a single messages which will run for example:

some message: vibe (30, 3823);
another message: vibe (30, 6542);
yet another message: vibe (50, 4279);

and so on...

 if (incomingByte == 50);

The ; at the end forms the body of your if statement. So, what you are saying here is if incomingByte is 50, do nothing. Otherwise, do nothing.

Then, you unconditionally call vibe().

Reread the page on the if statement.

OK, two hours RTFM'ing (good reading during the trip by train), I wrote the code and it works as it should.
Thank you PaulS for your wise help, I learned a lot.

Here is the code. For some time I will post a video here :slight_smile:

#define Dir 3
#define Step 6
#define Sel 4
int incomingByte = 0;

 
void setup() {
  Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
  pinMode(Dir, OUTPUT); 
  pinMode(Step, OUTPUT);
  pinMode(Sel, OUTPUT);
  digitalWrite(Sel, HIGH);
  initHead();  // move header half way
}

void initHead() {
  digitalWrite(Step, HIGH);
  digitalWrite(Dir, HIGH); 
  doSteps(80, 1911);  // move header to start position wherever it is, 80 steps at ~261 step/s
  digitalWrite(Dir, LOW);
  doSteps(40, 1911);
  delay(1000);
}

void doSteps(int steps, int stepDelay) {
  digitalWrite(Sel, LOW);
  for(int i=0;i<steps; i++) {
    digitalWrite(Step,LOW); 
    delayMicroseconds(stepDelay);
    digitalWrite(Step,HIGH); 
    delayMicroseconds(stepDelay);

  }
  digitalWrite(Sel, HIGH);
}

void vibe(int count, int period) {
  for(int l=0;l<count; l++) {
    digitalWrite(Dir, HIGH);
    doSteps(1, period);
    digitalWrite(Dir, LOW);
    doSteps(1, period);
  }
}

void loop() {
  if (Serial.available()>0) {
    incomingByte=Serial.read();

    
 if (incomingByte == 1) vibe(30, 3823);
 if (incomingByte == 2) vibe(40, 1823);
 if (incomingByte == 3) vibe(43, 1223);
 if (incomingByte == 4) vibe(50, 823);
 if (incomingByte == 5) vibe(50, 423);

/*    
and so on..
   */
  
  }
  
  }