I created this code years ago but then needed to resurrect it to recreate a previous project. Basically what it does is talk to a Sparkfun TB6612FNG breakout to control a motor for a cats laser toy. I had more code in this sketch for a pause type function but tore a lot of code out trying to figure out whats going wrong. Everything seems to work except in between function within the main VOID LOOP I am getting a weird 60+ second delay when there should be none.
I build the entire circuit on a protoboard with the bare Atmel chip and a crystal so I wasnt sure if I did something wrong. I bought a couple Arduino Pros and wired them into my circuit using on the Sparkfun breakout board and I am getting the same exact thing so it must be the code.
I then went to a website to simulate and Arduino with my code and on the motor output pins I just connected a couple virtual voltometers and everything appears to work correctly.
Here is the stripped down code
#include <avr/sleep.h>
#define RST 0 //reset output
#define SWIN1 2 //switch input
#define PWML 3 //PWM Laser driver signal
#define PWMA 12 //PWM motor driver signal
#define AIN2 11 //Direction control 2
#define AIN1 10 //Direction control 1
#define STBY 9 //Standby signal
#define RDY 13 //Onboard LED
int delaytimerun = 0; //Length of time motor will run
int delaytimepause = 0; //Length of time between actions
int drivespeed = 0; //Analog drive speed 0-255
int loopcount = 0; //How many times a mode will run
void setup() {
pinMode(SWIN1, INPUT);
pinMode(RST, OUTPUT);
pinMode(PWMA, OUTPUT);
pinMode(PWML, OUTPUT);
pinMode(AIN1, OUTPUT);
pinMode(AIN2, OUTPUT);
pinMode(STBY, OUTPUT);
pinMode(RDY, OUTPUT);
digitalWrite(RST, HIGH);
digitalWrite(RDY, HIGH);
digitalWrite(PWML, HIGH);
digitalWrite(STBY, HIGH);
mc_off();
mc_on();
spintest();
}
void loop() {
loopcount = 15;
fast_twitch();
loopcount = 15;
sweep();
loopcount = 15;
crazy();
loopcount = 15;
slow_sweep();
}
void fast_twitch() {
for (int i = 0; i < loopcount; i++) {
delaytimepause = random(10, 350);
delaytimerun = random(1, 30);
drivespeed = random(125, 254);
forward();
delaytimepause = random(10, 350);
delaytimerun = random(1, 30);
drivespeed = random(125, 255);
reverse();
}
}
void sweep() {
for (int i = 0; i < loopcount; i++) {
delaytimepause = random(100, 3000);
delaytimerun = random(5, 750);
drivespeed = random(60, 90);
forward();
delaytimepause = random(100, 3000);
delaytimerun = random(5, 750);
drivespeed = random(60, 90);
reverse();
}
}
void crazy() {
for (int i = 0; i < loopcount; i++) {
delaytimepause = random(1, 2500);
delaytimerun = random(1, 150);
drivespeed = random(60, 254);
forward();
delaytimepause = random(1, 2500);
delaytimerun = random(1, 150);
drivespeed = random(60, 254);
reverse();
}
}
void slow_sweep() {
for (int i = 0; i < loopcount; i++) {
delaytimepause = random(100, 2000);
delaytimerun = random(100, 1150);
drivespeed = random(5, 80);
forward();
delaytimepause = random(100, 2000);
delaytimerun = random(100, 1150);
drivespeed = random(5, 80);
reverse();
}
}
void mc_off() {
digitalWrite(STBY, LOW);
}
void mc_on() {
digitalWrite(STBY, HIGH);
}
void spintest() {
drivespeed = 255;
delaytimepause = (50);
for (int fiveoo = 0; fiveoo < 300; fiveoo = fiveoo + 50) {
delaytimerun = (fiveoo);
forward();
reverse();
}
delay(2000);
}
void forward() {
digitalWrite(AIN1, HIGH);
digitalWrite(AIN2, LOW);
analogWrite(PWMA, drivespeed);
delay(delaytimerun);
coast();
delay(125);
brake();
delay(delaytimepause);
}
void reverse() {
digitalWrite(AIN1, LOW);
digitalWrite(AIN2, HIGH);
analogWrite(PWMA, drivespeed);
delay(delaytimerun);
coast();
delay(125);
brake();
delay(delaytimepause);
}
void brake() {
digitalWrite(AIN1, HIGH);
digitalWrite(AIN2, HIGH);
}
void coast() {
digitalWrite(AIN1, LOW);
digitalWrite(AIN2, LOW);
}