I am trying to create a claw machine using an arduino. So far the biggest issue is getting the DC motor to move with a microswitch joystick. I am using is a MultiMoto to control the motors and claw. Whenever I use an IF statement with a SWITCH, only one of the motors will barley move with the joystick. I know the code is working, because I made it light an LED whenever a motor should move. I did try a plain IF/ELSE IF but nothing worked. Does anyone have any ideas that I could try?
Original:
/*
Robot Power MultiMoto v1.0 demo
This software is released into the Public Domain
This demo shows the setup of the L9958 chips and runs the 4 motor outputs
independently. Also Timer0 and Timer1 are set to higher frequency than standard.
This may affect the millisecond timer function.
v1.0 - initial release
*/
// include the SPI library:
#include <SPI.h>
// L9958 slave select pins for SPI
#define SS_M4 14
#define SS_M3 13
#define SS_M2 12
#define SS_M1 11
// L9958 DIRection pins
#define DIR_M1 2
#define DIR_M2 3
#define DIR_M3 4
#define DIR_M4 7
// L9958 PWM pins
#define PWM_M1 9
#define PWM_M2 10 // Timer1
#define PWM_M3 5
#define PWM_M4 6 // Timer0
// L9958 Enable for all 4 motors
#define ENABLE_MOTORS 8
int pwm1, pwm2, pwm3, pwm4;
int dir1, dir2, dir3, dir4;
//Crane variables
//Arduino used pins
const int joyup = 15;
const int joydown = 16;
const int joyleft = 17;
const int joyright = 18;
const int joypress = 19;
const int coin = 20;
const int front = 21;
const int left = 22;
const int clawup = 23;
const int clawdown = 24;
const int ledPin = 13;
//Mics Variables
int moveLeft = 0;
int moveRight = 0;
int moveBack = 0;
int moveFront = 0;
int pressButton = 0;
int coinIn = 0;
int limitFront = 0;
int limitLeft = 0;
int limitUp = 0;
int limtiDown = 0;
int opt = 0;
void setup() {
unsigned int configWord;
// put your setup code here, to run once:
pinMode(SS_M4, OUTPUT); digitalWrite(SS_M4, HIGH); // HIGH = not selected
pinMode(SS_M3, OUTPUT); digitalWrite(SS_M3, HIGH);
pinMode(SS_M2, OUTPUT); digitalWrite(SS_M2, HIGH);
pinMode(SS_M1, OUTPUT); digitalWrite(SS_M1, HIGH);
// L9958 DIRection pins
pinMode(DIR_M1, OUTPUT);
pinMode(DIR_M2, OUTPUT);
pinMode(DIR_M3, OUTPUT);
pinMode(DIR_M4, OUTPUT);
// L9958 PWM pins
pinMode(PWM_M1, OUTPUT); digitalWrite(PWM_M1, LOW);
pinMode(PWM_M2, OUTPUT); digitalWrite(PWM_M2, LOW); // Timer1
pinMode(PWM_M3, OUTPUT); digitalWrite(PWM_M3, LOW);
pinMode(PWM_M4, OUTPUT); digitalWrite(PWM_M4, LOW); // Timer0
// L9958 Enable for all 4 motors
pinMode(ENABLE_MOTORS, OUTPUT); digitalWrite(ENABLE_MOTORS, HIGH); // HIGH = disabled
/******* Set up L9958 chips *********
' L9958 Config Register
' Bit
'0 - RES
'1 - DR - reset
'2 - CL_1 - curr limit
'3 - CL_2 - curr_limit
'4 - RES
'5 - RES
'6 - RES
'7 - RES
'8 - VSR - voltage slew rate
'9 - ISR - current slew rate
'10 - ISR_DIS - current slew disable
'11 - OL_ON - open load enable
'12 - RES
'13 - RES
'14 - 0 - always zero
'15 - 0 - always zero
*/
// set to max current limit and disable ISR slew limiting
configWord = 0b0000010000001100;
SPI.begin();
SPI.setBitOrder(LSBFIRST);
SPI.setDataMode(SPI_MODE1); // clock pol = low, phase = high
// Motor 1
digitalWrite(SS_M1, LOW);
SPI.transfer(lowByte(configWord));
SPI.transfer(highByte(configWord));
digitalWrite(SS_M1, HIGH);
// Motor 2
digitalWrite(SS_M2, LOW);
SPI.transfer(lowByte(configWord));
SPI.transfer(highByte(configWord));
digitalWrite(SS_M2, HIGH);
// Motor 3
digitalWrite(SS_M3, LOW);
SPI.transfer(lowByte(configWord));
SPI.transfer(highByte(configWord));
digitalWrite(SS_M3, HIGH);
// Motor 4
digitalWrite(SS_M4, LOW);
SPI.transfer(lowByte(configWord));
SPI.transfer(highByte(configWord));
digitalWrite(SS_M4, HIGH);
// Reduce the PWM frequency to about 8kHz
// Note, this will screw up the timer functions that use Timer0 such as millis()
setPwmFrequency(PWM_M1, 8);
setPwmFrequency(PWM_M3, 8);
pinMode(joyup, INPUT);
pinMode(joydown, INPUT);
pinMode(joyleft, INPUT);
pinMode(joyright, INPUT);
pinMode(joypress, INPUT);
pinMode(coin, INPUT);
pinMode(front, INPUT);
pinMode(left, INPUT);
pinMode(clawup, INPUT);
pinMode(clawdown, INPUT);
pinMode(ledPin, OUTPUT);
}
// *******************************************
// ************** Main Loop ******************
// *******************************************
void loop() {
moveLeft = digitalRead(joyleft);
moveRight = digitalRead(joyright);
moveBack = digitalRead(joyup);
moveFront = digitalRead(joydown);
pressButton = digitalRead(joypress);
coinIn = digitalRead(coin);
limitFront = digitalRead(front);
limitLeft = digitalRead(left);
limitUp = digitalRead(clawup);
limtiDown = digitalRead(clawdown);
if (moveLeft == HIGH)
{
opt = 1;
}
else if (moveRight == HIGH)
{
opt = 2;
}
else if (moveBack == HIGH)
{
opt = 3;
}
else if (moveFront == HIGH)
{
opt = 4;
}
else
{
opt = 0;
}
switch (opt)
{
case 0:
analogWrite(PWM_M1, 0); digitalWrite(DIR_M1, 0);
analogWrite(PWM_M2, 0); digitalWrite(DIR_M2, 0);
analogWrite(PWM_M3, 0); digitalWrite(DIR_M3, 0);
digitalWrite(ledPin, LOW);
break;
case 1:
digitalWrite(ledPin, HIGH);
analogWrite(PWM_M1, 255); digitalWrite(DIR_M1, 0);
break;
case 2:
digitalWrite(ledPin, HIGH);
analogWrite(PWM_M1, 255); digitalWrite(DIR_M1, 1);
break;
case 3:
digitalWrite(ledPin, HIGH);
analogWrite(PWM_M3, 255); digitalWrite(DIR_M3, 0);
break;
case 4:
digitalWrite(ledPin, HIGH);
analogWrite(PWM_M3, 255); digitalWrite(DIR_M3, 1);
break;
}
digitalWrite(ENABLE_MOTORS, LOW); // enable = LOW
}
// ***********************************************
// ************** End Main Loop ******************
// ***********************************************
/*
* Divides a given PWM pin frequency by a divisor.
*
* The resulting frequency is equal to the base frequency divided by
* the given divisor:
* - Base frequencies:
* o The base frequency for pins 3, 9, 10, and 11 is 31250 Hz.
* o The base frequency for pins 5 and 6 is 62500 Hz.
* - Divisors:
* o The divisors available on pins 5, 6, 9 and 10 are: 1, 8, 64,
* 256, and 1024.
* o The divisors available on pins 3 and 11 are: 1, 8, 32, 64,
* 128, 256, and 1024.
*
* PWM frequencies are tied together in pairs of pins. If one in a
* pair is changed, the other is also changed to match:
* - Pins 5 and 6 are paired (Timer0)
* - Pins 9 and 10 are paired (Timer1)
* - Pins 3 and 11 are paired (Timer2)
*
* Note that this function will have side effects on anything else
* that uses timers:
* - Changes on pins 5, 6 may cause the delay() and
* millis() functions to stop working. Other timing-related
* functions may also be affected.
* - Changes on pins 9 or 10 will cause the Servo library to function
* incorrectly.
*
* Thanks to macegr of the Arduino forums for his documentation of the
* PWM frequency divisors. His post can be viewed at:
* http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1235060559/0#4
*/
void setPwmFrequency(int pin, int divisor) {
byte mode;
if(pin == 5 || pin == 6 || pin == 9 || pin == 10) { // Timer0 or Timer1
switch(divisor) {
case 1: mode = 0x01; break;
case 8: mode = 0x02; break;
case 64: mode = 0x03; break;
case 256: mode = 0x04; break;
case 1024: mode = 0x05; break;
default: return;
}
if(pin == 5 || pin == 6) {
TCCR0B = TCCR0B & 0b11111000 | mode; // Timer0
} else {
TCCR1B = TCCR1B & 0b11111000 | mode; // Timer1
}
} else if(pin == 3 || pin == 11) {
switch(divisor) {
case 1: mode = 0x01; break;
case 8: mode = 0x02; break;
case 32: mode = 0x03; break;
case 64: mode = 0x04; break;
case 128: mode = 0x05; break;
case 256: mode = 0x06; break;
case 1024: mode = 0x7; break;
default: return;
}
TCCR2B = TCCR2B & 0b11111000 | mode; // Timer2
}
}
If i use this line in place of the IF statement the motors will work:
analogWrite(PWM_M1, 255); digitalWrite(DIR_M1, 0);
analogWrite(PWM_M3, 255); digitalWrite(DIR_M3, 0);
digitalWrite(ENABLE_MOTORS, LOW); // enable = LOW
Thanks,
Draco