Pages: [1]   Go Down
Author Topic: metro liberay + Serial.read + 7 motors  (Read 286 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am trying to run multiple 12V motors from my arduino through the serialport - and would, as a start, like to be able to turn them on individually.
i have no problem with writing messages to the arduino through the serialport ( using max/msp), but Im having trouble with the metro.
here´s my code: (from the example on the website)

#include <Metro.h>
/*
instead of blinking an led I wil use it to turn RELAYS off and ON to control the motors speed
 */

// Instanciate a metro object and set the interval to 250 milliseconds (0.25 seconds).
int val = 0; // reading data from the seral port
Metro ledPin02_metro = Metro(250);
int ledPin02 = 2;                // here i attached my RELAY 01
Metro ledPin03_metro = Metro(250);
int ledPin03 = 3;                // here i attached my RELAY 02
void setup()
{
  pinMode(ledPin02,OUTPUT);
  pinMode(ledPin03,OUTPUT);
  Serial.begin(9600);
}
void loop()
{
  val = Serial.read();
if(val == '1'){ // if the number 1 is recieved from the serial port
    if (ledPin02_metro.check() == 1) { // check if the metro has passed it's interval .
      digitalWrite(ledPin02,!digitalRead(ledPin02)); // change the state of pin 3.
      if (digitalRead(ledPin02)==HIGH)  {
        ledPin02_metro.interval(250); // if the pin is HIGH, set the interval to 0.25 seconds.
      }
    }
}
if(val == '2'){ // if the number 1 is recieved from the serial port
    
    if (ledPin03_metro.check() == 1) { // check if the metro has passed it's interval .
      digitalWrite(ledPin03,!digitalRead(ledPin03)); // change the state of pin 3.
      if (digitalRead(ledPin03)==HIGH)  {
        ledPin02_metro.interval(250); // if the pin is HIGH, set the interval to 0.25 seconds.
      }
    }
}
  
}

Im running version 0006 on my arduino is that an issue ?

before i tried the metro solution my code looked like this:

int count01 = 0;  
int count02 = 0;              
int ledPin02 = 2;                 // RELAY connected to digital pin 2
int ledPin03 = 3;                 // RELAY connected to digital pin 3

int val = 0;                     // variable to recieve data from the serialport


void setup()
{
  pinMode(ledPin02, OUTPUT);      // sets the digital pin as output
  pinMode(ledPin03,OUTPUT);
 
  Serial.begin(9600);
}
void loop(){
  val = Serial.read();
  switch (val) {
  case 1:
    for(int count01 = 0; count01 <= 10; count01++){
      digitalWrite(debugFix,HIGH);
      digitalWrite(ledPin02,HIGH);
      delay(20);
    
      digitalWrite(ledPin02,LOW);
      delay(20);
    }
  break;
  case 2:
    for(int count02 = 0; count02 <= 10; count02++){
      digitalWrite(debugFix,HIGH);
      digitalWrite(ledPin03,HIGH);
      delay(100);
    
      digitalWrite(ledPin03,LOW);
      delay(100);
    }
  break;
  default:
    digitalWrite(ledPin02,LOW);
    digitalWrite(ledPin03,LOW);
    

  }
}

but in this exhample i think i get trouble with the delay()

my aim is to control a PWM-like pulse on  7 relays from the serialport -- if there is anyone on the forum that knows how to solve my problem then i would be wery greatfull to hear from you
best regards
martin






Logged

Pages: [1]   Go Up
Jump to: