Putting it all together, but having issues with button 2.
I tested motor forwards & backwards, and it was fine. Tested LED swap interrupt, worked fine.
Incorporating buttons, button 1 works (& reverses direction of motor), button 2 doesn’t. Code got me going all googly-eyed.
Can’t see, what is probably very obvious.
// Include the Bricktronics libraries
#include <BricktronicsShield.h>
#include <BricktronicsMotor.h>
#include <BricktronicsButton.h>
#define ledPin1 12
#define ledPin2 13
int timerCounter= 0;
unsigned long previousMillis = 0; // will store last time clock was updated
unsigned long currentMillis = 0;
// Attach motor and sensors to Bricktronics sheild
BricktronicsButton b1(BricktronicsShield::SENSOR_1);
BricktronicsButton b2(BricktronicsShield::SENSOR_2);
BricktronicsMotor m(BricktronicsShield::MOTOR_1);
void setup() {
pinMode(12, OUTPUT);
pinMode(13, OUTPUT);
digitalWrite(12, HIGH);
// Be sure to set your serial console to 115200 baud
Serial.begin(115200);
// Initialize the Bricktronics Shield
BricktronicsShield::begin();
// Initialize the button connection
b1.begin();
b2.begin();
m.begin();
// initialize timer1
noInterrupts(); // disable all interrupts
TCCR1A = 0;
TCCR1B = 0;
// Set timer1_counter to the correct value for our interrupt interval
timerCounter = 0; // preload timer 65536-16MHz/256/2Hz
TCNT1 = timerCounter; // preload timer
TCCR1B |= (1 << CS12); // 256 prescaler
TIMSK1 |= (1 << TOIE1); // enable timer overflow interrupt
interrupts(); // enable all interrupts
}
// interrupt service routine
ISR(TIMER1_OVF_vect){
if (timerCounter == 10) { // preload timer
digitalWrite(ledPin1, digitalRead(ledPin1) ^ 1);
digitalWrite(ledPin2, digitalRead(ledPin2) ^ 1);
previousMillis = currentMillis;
timerCounter = 0;
}
timerCounter++;
}
int motorSpeed = -170;
void loop() {
m.setFixedDrive(motorSpeed);
currentMillis = millis();
// Wait until button 1 is pressed
while (b1.isReleased()) { // Nothing to do here
}
// To get here, the button was pushed!
// In order to debounce the button, we transmit a message on the serial
// port and then wait a little bit longer here.
Serial.println(“b1 pressed”);
delay(100);
// Reverse direction
motorSpeed *= -1;
Serial.println(motorSpeed);
m.setFixedDrive(motorSpeed);
// Wait until button 1 is released
while (b1.isPressed()) { // Nothing to do here
}
// In order to debounce the button, we transmit a message on the serial
// port and then wait a little bit longer here.
Serial.println(“b1 released”);
delay(100);
// Wait until button 2 is pressed
while (b2.isReleased()) { // Nothing to do here
}
// To get here, the button was pushed!
// In order to debounce the button, we transmit a message on the serial
// port and then wait a little bit longer here.
Serial.println(“b2 pressed”);
delay(100);
// Reverse direction
motorSpeed *= -1;
m.setFixedDrive(motorSpeed);
Serial.println(motorSpeed);
// Wait until button 2 is released
while (b2.isPressed()) { // Nothing to do here
}
// In order to debounce the button, we transmit a message on the serial
// port and then wait a little bit longer here.
Serial.println(“b2 released”);
delay(100);
}