Im toggling a motor on a button press and reading the current via ACS712.
After the motor is on for more than ~3 seconds, the code gets stuck somewhere. In the window, it just stops. Any suggestions?
Thank you!
#include <TimerOne.h>
#define motorButtonPin 2
#define ledPin 6
#define currentSensorPin A8
byte manipulation = 64;
byte high = false;
volatile byte motorState = LOW;
byte ledState = LOW;
float currentValue;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Serial1.begin(9600);
pinMode(ledPin, OUTPUT);
pinMode(motorButtonPin, INPUT);
attachInterrupt(digitalPinToInterrupt(motorButtonPin), motorToggle, RISING);
//Timer 1 setup at 1000 Hz
Timer1.initialize(1000);
Timer1.attachInterrupt(motorControl);
Timer1.stop();
}
//Timer 1 interrupt at 1000 Hz, executes PD motor control
void motorControl(){
// //1 is full reverse,, +127 is full forward, 64 is stop
// if (high == false ){manipulation++;}
// if (high == true){manipulation--;}
// if(manipulation > 127){manipulation = 127; high = true;}
// if(manipulation < 1){manipulation = 1; high = false;}
Serial1.write(100);
}
void loop() {
// put your main code here, to run repeatedly:
if(motorState == HIGH){Timer1.resume(); ledState = HIGH;}
if(motorState == LOW){Timer1.stop(); Serial1.write(64); ledState = LOW;}
digitalWrite(ledPin, ledState);
currentValue = (510 - analogRead(currentSensorPin))*75.78/1023;
Serial.println(currentValue);
}
void motorToggle(){
motorState = !motorState;
}