fusing two codes

Need help,

I've got one code that works as a state machine, and another that works with the serial receive send thru max msp.

The latter one has the pinout's of my h-bridge. But I want the former one use as a state machine.

Pin 13 and Pin 10 should be HIGH al the time.

Pin 12 & 11 and Pin 9 & 8 should switch according to the state machine loop.

As example.

Pin 12 is High for 100 ms (Pin 11 must be LOW) and Pin 9 is High for 100 ms (Pin 8 must be LOW) . The states of the pin should switch random on their own.

This is used with magnets hence when 12 is HIGH the magnetic field will be 'positive' of it. IF Pin 8 is HIGH that magnetic field will be 'negative'. I want the states of the magnets to change randomly with the state sequencing.

I've got it to work with the max msp serial but I lack the knowledge to build it in the state machine. It may or maynot be a correct way of claiming it's a state machine. But if you read the code I think you'll get what I'm achieving at.

Hope somebody can help me!!

Max/msp serial code

#define ENABLE1 13
#define POLARITY1a 12
#define POLARITY1b 11
#define ENABLE2 10
#define POLARITY2a 9
#define POLARITY2b 8

#define analogPin1 0     
#define analogPin2 1
#define analogPin3 2




void setup()
{
  Serial.begin(9600);          //  setup serial
  
  pinMode(ENABLE1,OUTPUT);
  pinMode(ENABLE2,OUTPUT);
  pinMode(POLARITY1a,OUTPUT);
  pinMode(POLARITY1b,OUTPUT);
  pinMode(POLARITY2a,OUTPUT);
  pinMode(POLARITY2b,OUTPUT);

 
}

void loop()
{
  
  
  // WRITE
  static int v = 0;

    char ch = Serial.read();
    switch(ch) {
      case '0'...'9':
        v = v * 10 + ch - '0';
        break;
      case 'x':                            //integer for recognition in max in ascii
        digitalWrite(ENABLE1, v); // off 
        v = 0;
        break;
      case 'w':
        digitalWrite(POLARITY1a, LOW);
        digitalWrite(POLARITY1b, HIGH); // + or -
        //v = 0;
        break;
       case 'y':
        digitalWrite(POLARITY1a, HIGH); // - or +
        digitalWrite(POLARITY1b, LOW);
        //v = 0;
        break;
      case 't':
        digitalWrite(ENABLE2, v);
        v = 0;
        break;
      case 's':
        digitalWrite(POLARITY2a, HIGH);
        digitalWrite(POLARITY2b, LOW);
        //v = 0;
        break;
      case 'u':
        digitalWrite(POLARITY2a, LOW);
        digitalWrite(POLARITY2b, HIGH);
        //v = 0;
        break;
    }
    }

state sequencer code

// pins
const int pin1 = 8;
const int pin2 = 10;

// timing variables
unsigned long stap;
unsigned long currentMillis;
long previousMillis = 0;
const long interval = 100; // interval om de 1/10 seconde of 100 millisecs

// variable states of pin1 & pin2
int state1 = 0;
int state2 = 0;

// SETUP _______________________________________________________________
void setup() {
 Serial.begin(9600);
 pinMode(pin1,OUTPUT);
 pinMode(pin2,OUTPUT);
}

// ACTIONS ______________________________________________________________
void een(int state) {
 digitalWrite(pin1,state);
}
void twee(int state) {
 digitalWrite(pin2,state);
}

void eenBlink() {
 if (state1 == LOW)
     state1 = HIGH;
   else
     state1 = LOW;
 digitalWrite(pin1,state1);
}
void tweeBlink() {
 if (state2 == LOW)
     state2 = HIGH;
   else
     state2 = LOW;
 digitalWrite(pin2,state2);
}

//LOOPS_______________________________________________________________
void LOOP1(){
 een(1);
 twee(0);
}

void LOOP2(){
 een(0);
 twee(1);
}

void LOOP3(){
 if (stap % 2 == 0){ // 1 stap op 2 aan
   eenBlink();
 }
 if (stap % 8 == 0){ // 1 stap op 4 aan, 3 stappen op 4 uit
   tweeBlink();
 }
}

//MAIN LOOP____________________________________________________________
void loop() {
 currentMillis = millis();  // sample the time

 if (currentMillis - previousMillis >= interval) { 
   previousMillis = currentMillis; 
   stap = stap+1;
   //Serial.println(stap);
 }

 if (stap == 120) { // totaal aantal stappen per volledige loop
   Serial.println("------- time reset --------");
   stap = 0;  
 }

//deel 1
 if (stap >=1 && stap<=10) {
   Serial.println("LOOP1");
   LOOP1();
 }
//deel 2
 if (stap >=11 && stap<=20) {
   Serial.println("LOOP2");
   LOOP2();
 }
//deel 3
 if (stap >=21 && stap<=120) {
   Serial.println("LOOP3");
   LOOP3();
 }

}

Looky here.

http://www.thebox.myzen.co.uk/Tutorial/Merging_Code.html