Vibrating Motors - HELP

Hey guys,

I've just bought an Arduino Mega 2560 to use in a project i'm doing.

Basically, i have 10 vibrating motors that i need to control, in terms of switching them on and off in different patterns and different times.

They're gonna be controlled by a solid state relay at each motor. My inputs will be in the form of 6 or 7 toggle switches, and ideally, each time one of these switches is thrown, a different combination of motors will turn on.

My problem is thus; i'm not sure how to code the arduino so that it can interpret each of the different input switches, and activate different relays.

Any suggestion on any standard pieces of code, or coding techniques?

Can anyone help me?

Big time thanks!

The real key is to use some basic boolean logic.

Computers "make decisions" and you can make your program "conditionally branch" with [u]"If" statments[/u], and [u]"and" (&&00) and "or" (||) statements[/u]. (And, a few others.)

Start-out with [u]this[/u] single-button example.

Then add some swtches . Then, you can experiment with "and", which will require 2 (or more) switches to be pressed to light the LED. A simple "or" operation could light the LED if button-1 or button-2 is pushed.

Of course, you can combine "and" and "or", so that the LED comes on if you push button-1, or if you press button-2 and button-3, etc.

You might need to make yourself some sort of flow-chart to figure-out how your final logic is going to work.

Try to write your sketch in little bits so that you can test & debug one part of your logic at a time. It sounds like this might get complicated, and if you try to do it all in one step, you'll never get it working. :wink:

It would also be easier if you start with LEDs (in place of your motors) and get everything else working before you add your vibration motors and the solid state relays.

Thanks for your help, man.

I'm still a little unsure how i can get the arduino to recognise when a particular switch is pressed - what is the code for this?

http://arduino.cc/en/Reference/DigitalRead

They're gonna be controlled by a solid state relay at each motor.

How big are these motors? A solid state relay is normally only used on mains motors.

12V, is there a better way of switching them on and off rather than a solid state relay?

Yes normally a SSR only works on AC, although there are a few that will work on DC but they are expensive. All you need is a transistor, see:-
http://www.thebox.myzen.co.uk/Workshop/Motors_1.html

Thanks for your help, much appreciated!

Sorry if i'm asking the wrong questions, i think i've finally sussed out what i need to ask:

So i have 6 input pins, in the form of pushbutton switches, and 10 outputs, in the form of relays connected to motors.

Each input button needs to control a different combination of outputs, what's the best way to do this with one sketch? Is this possible?

Certainly possible - don't forget you can use the analog pins as extra digital ones. One way might be to have a 2D array that specifies for each input, what the resulting relay settings should be.

Pretty simple set-up:

Use digitalRead() to get input from switches, assign them to variables - (from John_S)

Simple boolean logic to set the outputs depending on variables - (from DVDdoug - if you wanted to make it neater Arduino may have equivalent of 'switch' statment found in C)

Use a discrete driver circuit chip to power motors - requires a couple of extra components for EMC and protecting the Arduino, see AB-001: Discrete Driver Circuits For Vibration Motors - Precision Microdrives)

@JM3000 If you don't know what boolean logic means, it is simply a way of saying you get the switch states into variables and then use compound if statements to channel the sketch into switching on the different combinations you want.
A compound if is one that contains the normal if this is equal to that but these conditions are joined together with AND statements, like
if( this == that && theOther == theOtherOther) {
// then turn on the relays you need to
}

There can be as many joining ands && as you want, you can also use the OR function to join them, this is the || symbol.

Quality, cheers for your help guys!

Sorry if these are obvious questions!

I've written some code, can anyone tell me if i'm on the right lines? I'm not sure my pin selection is correct. Also, i'm pretty sure that there is a less long-winded way of writing this script, any ideas?

// SETS PUSHBUTTON SWITCHES 
const int pushbutton1 = 2;
const int pushbutton2 = 3;
const int pushbutton3 = 4;
const int pushbutton4 = 5;
const int pushbutton5 = 6;
const int pushbutton6 = 7;

// SETS RELAY PINS
const int relayCT = 10;
const int relayL1 = 11;
const int relayL2 = 12;
const int relayL3 = 13;
const int relayL4 = 14;
const int relayCB = 15;
const int relayR1 = 16;
const int relayR2 = 17;
const int relayR3 = 18;
const int relayR4 = 19;

// CURRENT STATE OF PUSHBUTTONS
int pushbuttonState1 = 0;
int pushbuttonState2 = 0;
int pushbuttonState3 = 0;
int pushbuttonState4 = 0;
int pushbuttonState5 = 0;
int pushbuttonState6 = 0;

// CURRENT STATE OF RELAYS
int relayStateCT = 0;
int relayStateL1 = 0;
int relayStateL2 = 0;
int relayStateL3 = 0;
int relayStateL4 = 0;
int relayStateCB = 0;
int relayStateR1 = 0;
int relayStateR2 = 0;
int relayStateR3= 0;
int relayStateR4 = 0;


void setup() {
  
   // pushbuttons as inputs
   
   pinMode(pushbutton1, INPUT);
   pinMode(pushbutton2, INPUT);
   pinMode(pushbutton3, INPUT);
   pinMode(pushbutton4, INPUT);
   pinMode(pushbutton5, INPUT);
   pinMode(pushbutton6, INPUT);
   
   // relays as outputs
   
   pinMode(relayCT, OUTPUT);
   pinMode(relayL1, OUTPUT);
   pinMode(relayL2, OUTPUT);
   pinMode(relayL3, OUTPUT);
   pinMode(relayL4, OUTPUT);
   pinMode(relayCB, OUTPUT);
   pinMode(relayR1, OUTPUT);
   pinMode(relayR2, OUTPUT);
   pinMode(relayR3, OUTPUT);
   pinMode(relayR4, OUTPUT);
                
}

void loop() {
  
  // reads state of each push button
  
  pushbuttonState1 = digitalRead(pushbutton1);
  pushbuttonState2 = digitalRead(pushbutton2);
  pushbuttonState3 = digitalRead(pushbutton3);
  pushbuttonState4 = digitalRead(pushbutton4);
  pushbuttonState5 = digitalRead(pushbutton5);
  pushbuttonState6 = digitalRead(pushbutton6);
  
  // ----------RIGHT TURN------------
  
  if (  pushbuttonState1 == true     ){
  
  
      digitalWrite(relayR2, HIGH);     
      digitalWrite(relayR3, HIGH);   
      digitalWrite(relayR4, HIGH);
      delay(250);
      digitalWrite(relayR2, LOW);     
      digitalWrite(relayR3, LOW) ;    
      digitalWrite(relayR4, LOW);
      delay(250);
      digitalWrite(relayR2, HIGH);     
      digitalWrite(relayR3, HIGH) ;    
      digitalWrite(relayR4, HIGH);
      delay(250);
      digitalWrite(relayR2, LOW);     
      digitalWrite(relayR3, LOW) ;     
      digitalWrite(relayR4, LOW);
      delay(500);
      
  }
 // -----------LEFT TURN--------------
 
 if (     pushbuttonState2 == true    ){
 
      digitalWrite(relayL2, HIGH)  ;   
      digitalWrite(relayL3, HIGH)  ;  
      digitalWrite(relayL4, HIGH);
      delay(250);
      digitalWrite(relayL2, LOW) ;    
      digitalWrite(relayL3, LOW)  ;    
      digitalWrite(relayL4, LOW);
      delay(250);
      digitalWrite(relayL2, HIGH);     
      digitalWrite(relayL3, HIGH) ;    
      digitalWrite(relayL4, HIGH);
      delay(250);
      digitalWrite(relayL2, LOW) ;    
      digitalWrite(relayL3, LOW) ;    
      digitalWrite(relayL4, LOW);
      delay(500);
      
 }
}

Also, as far as transistors vs. relays goes, can my motors really be switched on with a simple 5V DC transistor, if i have a 12v supply on my Arduino?

Thanks again!

can anyone tell me if i'm on the right lines?

Two things:-

  1. Always post code between code markers, highlight the code and click the # icon, you can go back and modify that post.

  2. Yes that code is very turgid, you need to learn how to use arrays, look here for some help:-
    http://www.thebox.myzen.co.uk/Tutorial/Arrays.html

Due to time constraints, i'm gonna leave my code like that, as long as it will still work.

Looking at the pin numbers i've assigned for inputs and outputs, are these the right kind of thing? Would anybody else pick other pins for inputs or outputs? I'm using a Mega 2560

// SETS PUSHBUTTON SWITCHES 
const int pushbutton1 = 2;
const int pushbutton2 = 3;
const int pushbutton3 = 4;
const int pushbutton4 = 5;
const int pushbutton5 = 6;
const int pushbutton6 = 7;

// SETS RELAY PINS
const int relayCT = 10;
const int relayL1 = 11;
const int relayL2 = 12;
const int relayL3 = 13;
const int relayL4 = 14;
const int relayCB = 15;
const int relayR1 = 16;
const int relayR2 = 17;
const int relayR3 = 18;
const int relayR4 = 19;

Or does it not really matter, and i'm making something out of nothing here?

Or does it not really matter,

It doesn't matter.

Due to time constraints

So have we been doing your homework for you. I hope we get a good mark.

No, just giving me a nudge or two in the right direction.

Having uploaded my code and made a circuit, my board is doing something a bit weird.

I've programmed the board so that it only gives an output when one of the buttons is pressed and does nothing when no buttons are pressed, but at the moment it seems to ignore any of the button presses, and just cycles through each of the things that the buttons would do, but without any of them being pressed. Any ideas why?

Any ideas why?

Yes, your code is wrong.

However in order to tell you exactly exactly what bit of it is wrong you will have to post it as my crystal ball is on the blink at the moment.

Here it is. I know it could do with arrays instead.

// SETS PUSHBUTTON SWITCHES 
const int pushbutton1 = 2;
const int pushbutton2 = 3;
const int pushbutton3 = 4;
const int pushbutton4 = 5;
const int pushbutton5 = 6;
const int pushbutton6 = 7;

// SETS RELAY PINS
const int relayCT = 10;
const int relayL1 = 11;
const int relayL2 = 12;
const int relayL3 = 13;
const int relayL4 = 14;
const int relayCB = 15;
const int relayR1 = 16;
const int relayR2 = 17;
const int relayR3 = 18;
const int relayR4 = 19;

// CURRENT STATE OF PUSHBUTTONS
int pushbuttonState1 = 0;
int pushbuttonState2 = 0;
int pushbuttonState3 = 0;
int pushbuttonState4 = 0;
int pushbuttonState5 = 0;
int pushbuttonState6 = 0;

// CURRENT STATE OF RELAYS
int relayStateCT = 0;
int relayStateL1 = 0;
int relayStateL2 = 0;
int relayStateL3 = 0;
int relayStateL4 = 0;
int relayStateCB = 0;
int relayStateR1 = 0;
int relayStateR2 = 0;
int relayStateR3= 0;
int relayStateR4 = 0;


void setup() {
  
   // pushbuttons as inputs
   
   pinMode(pushbutton1, INPUT);
   pinMode(pushbutton2, INPUT);
   pinMode(pushbutton3, INPUT);
   pinMode(pushbutton4, INPUT);
   pinMode(pushbutton5, INPUT);
   pinMode(pushbutton6, INPUT);
   
   // relays as outputs
   
   pinMode(relayCT, OUTPUT);
   pinMode(relayL1, OUTPUT);
   pinMode(relayL2, OUTPUT);
   pinMode(relayL3, OUTPUT);
   pinMode(relayL4, OUTPUT);
   pinMode(relayCB, OUTPUT);
   pinMode(relayR1, OUTPUT);
   pinMode(relayR2, OUTPUT);
   pinMode(relayR3, OUTPUT);
   pinMode(relayR4, OUTPUT);
                
}

void loop() {
  
  // reads state of each push button
  
  pushbuttonState1 = digitalRead(pushbutton1);
  pushbuttonState2 = digitalRead(pushbutton2);
  pushbuttonState3 = digitalRead(pushbutton3);
  pushbuttonState4 = digitalRead(pushbutton4);
  pushbuttonState5 = digitalRead(pushbutton5);
  pushbuttonState6 = digitalRead(pushbutton6);
  
  // ----------RIGHT TURN------------
  
  if (  pushbuttonState1 == true     ){
  
  
      digitalWrite(relayR2, HIGH);     
      digitalWrite(relayR3, HIGH);   
      digitalWrite(relayR4, HIGH);
      delay(250);
      digitalWrite(relayR2, LOW);     
      digitalWrite(relayR3, LOW) ;    
      digitalWrite(relayR4, LOW);
      delay(250);
      digitalWrite(relayR2, HIGH);     
      digitalWrite(relayR3, HIGH) ;    
      digitalWrite(relayR4, HIGH);
      delay(250);
      digitalWrite(relayR2, LOW);     
      digitalWrite(relayR3, LOW) ;     
      digitalWrite(relayR4, LOW);
      delay(500);
      
  }
 // -----------LEFT TURN--------------
 
 if (     pushbuttonState2 == true    ){
 
      digitalWrite(relayL2, HIGH)  ;   
      digitalWrite(relayL3, HIGH)  ;  
      digitalWrite(relayL4, HIGH);
      delay(250);
      digitalWrite(relayL2, LOW) ;    
      digitalWrite(relayL3, LOW)  ;    
      digitalWrite(relayL4, LOW);
      delay(250);
      digitalWrite(relayL2, HIGH);     
      digitalWrite(relayL3, HIGH) ;    
      digitalWrite(relayL4, HIGH);
      delay(250);
      digitalWrite(relayL2, LOW) ;    
      digitalWrite(relayL3, LOW) ;    
      digitalWrite(relayL4, LOW);
      delay(500);
      
 }
  // ----------ROUNDABOUT 1st EXIT---------
  
  if (     pushbuttonState3 == true    ){
  
      //circle1 = start
      digitalWrite(relayCB, HIGH);
      delay(80);
      digitalWrite(relayCB, LOW);
      digitalWrite(relayL4, HIGH);
      delay(80);
      digitalWrite(relayL4, LOW);
      digitalWrite(relayL3, HIGH);
      delay(80);
      digitalWrite(relayL3, LOW);
      digitalWrite(relayL2, HIGH);
      delay(80);
      digitalWrite(relayL2, LOW);
      digitalWrite(relayL1, HIGH);
      delay(80);
      digitalWrite(relayL1, LOW);
      digitalWrite(relayCT, HIGH);
      delay(80);
      digitalWrite(relayCT, LOW);
      digitalWrite(relayR1, HIGH);
      delay(80);
      digitalWrite(relayR1, LOW);
      digitalWrite(relayR2, HIGH);
      delay(80);
      digitalWrite(relayR2, LOW);
      digitalWrite(relayR3, HIGH);
      delay(80);
      digitalWrite(relayR3, LOW);
      digitalWrite(relayR4, HIGH);
      delay(80);
      digitalWrite(relayR4, LOW);
      delay(200);
      //circle1 = end
      digitalWrite(relayCB, HIGH);
      delay(80);
      digitalWrite(relayCB, LOW);
      digitalWrite(relayL4, HIGH);
      delay(80);
      digitalWrite(relayL4, LOW);
      digitalWrite(relayL3, HIGH);
      delay(80);
      digitalWrite(relayL3, LOW);
      digitalWrite(relayL2, HIGH);
      delay(80);
      digitalWrite(relayL2, LOW);
      digitalWrite(relayL1, HIGH);
      delay(80);
      digitalWrite(relayL1, LOW);
      digitalWrite(relayCT, HIGH);
      delay(80);
      digitalWrite(relayCT, LOW);
      digitalWrite(relayR1, HIGH);
      delay(80);
      digitalWrite(relayR1, LOW);
      digitalWrite(relayR2, HIGH);
      delay(80);
      digitalWrite(relayR2, LOW);
      digitalWrite(relayR3, HIGH);
      delay(80);
      digitalWrite(relayR3, LOW);
      digitalWrite(relayR4, HIGH);
      delay(80);
      digitalWrite(relayR4, LOW);
      delay(200);
      //circle2 = end                                
      digitalWrite(relayL2, HIGH);     
      digitalWrite(relayL3, HIGH) ;     
      digitalWrite(relayL4, HIGH);
      delay(125);
      digitalWrite(relayL2, LOW);     
      digitalWrite(relayL3, LOW) ;     
      digitalWrite(relayL4, LOW);
      delay(125);
      digitalWrite(relayL2, HIGH);     
      digitalWrite(relayL3, HIGH) ;    
      digitalWrite(relayL4, HIGH);
      delay(125);
      digitalWrite(relayL2, LOW);     
      digitalWrite(relayL3, LOW) ;     
      digitalWrite(relayL4, LOW);
      delay(500);
      
  }    
      
// ----------ROUNDABOUT 2nd EXIT---------

  if (     pushbuttonState4 == true    ){
  
      //circle1 = start
     digitalWrite(relayCB, HIGH);
      delay(80);
      digitalWrite(relayCB, LOW);
      digitalWrite(relayL4, HIGH);
      delay(80);
      digitalWrite(relayL4, LOW);
      digitalWrite(relayL3, HIGH);
      delay(80);
      digitalWrite(relayL3, LOW);
      digitalWrite(relayL2, HIGH);
      delay(80);
      digitalWrite(relayL2, LOW);
      digitalWrite(relayL1, HIGH);
      delay(80);
      digitalWrite(relayL1, LOW);
      digitalWrite(relayCT, HIGH);
      delay(80);
      digitalWrite(relayCT, LOW);
      digitalWrite(relayR1, HIGH);
      delay(80);
      digitalWrite(relayR1, LOW);
      digitalWrite(relayR2, HIGH);
      delay(80);
      digitalWrite(relayR2, LOW);
      digitalWrite(relayR3, HIGH);
      delay(80);
      digitalWrite(relayR3, LOW);
      digitalWrite(relayR4, HIGH);
      delay(80);
      digitalWrite(relayR4, LOW);
      delay(200);
      //circle1 = end
      digitalWrite(relayCB, HIGH);
      delay(80);
      digitalWrite(relayCB, LOW);
      digitalWrite(relayL4, HIGH);
      delay(80);
      digitalWrite(relayL4, LOW);
      digitalWrite(relayL3, HIGH);
      delay(80);
      digitalWrite(relayL3, LOW);
      digitalWrite(relayL2, HIGH);
      delay(80);
      digitalWrite(relayL2, LOW);
      digitalWrite(relayL1, HIGH);
      delay(80);
      digitalWrite(relayL1, LOW);
      digitalWrite(relayCT, HIGH);
      delay(80);
      digitalWrite(relayCT, LOW);
      digitalWrite(relayR1, HIGH);
      delay(80);
      digitalWrite(relayR1, LOW);
      digitalWrite(relayR2, HIGH);
      delay(80);
      digitalWrite(relayR2, LOW);
      digitalWrite(relayR3, HIGH);
      delay(80);
      digitalWrite(relayR3, LOW);
      digitalWrite(relayR4, HIGH);
      delay(80);
      digitalWrite(relayR4, LOW);
      delay(200);
      //circle2 = end 
      //2nd exit signal
      digitalWrite(relayL1, HIGH);    
      digitalWrite(relayCT, HIGH) ;     
      digitalWrite(relayR1, HIGH);
      delay(125);
      digitalWrite(relayL1, LOW);     
      digitalWrite(relayCT, LOW) ;     
      digitalWrite(relayR1, LOW);
      delay(125);
      digitalWrite(relayL1, HIGH);     
      digitalWrite(relayCT, HIGH) ;    
      digitalWrite(relayR1, HIGH);
      delay(125);
      digitalWrite(relayL1, LOW);     
      digitalWrite(relayCT, LOW) ;     
      digitalWrite(relayR1, LOW);
      delay(500);
  }

}