So, finally making progress on my project, getting things to move in the right direction, buttons are no longer backwards etc.
But, in putting the pieces together I'm definitely missing something basic...
I've got an accelerometer on a digital sensor and it println()s nicely when moved around. But I'm trying to have the hbridge stop pushing the motor when the tilt reaches a set point.
Initially I had a button which called out to a function (outside of the loop) doing the motor driving etc. But as soon as I call the function, the sensor stops responding. Maybe that makes sense. So then I put the motor driving into the loop, but the sensor still stops updating as soon as the button is pushed to initiate travel to the setpoint.
Seems like this should work, but maybe I need some kind of interrupt approach. Any pointers?
void loop() {
StateA = digitalRead(BtnA);
//Serial.println(StateA);
int TiltTemp, PulseTilt, TiltSet;
PulseTilt = pulseIn(TiltPin,HIGH);
TiltTemp = ((PulseTilt/10)-500)*8/4; //divide by 4 to take values from 1024 to fit into a byte
//Serial.print(TiltTemp);
Serial.println(TiltTemp);
if (StateA==HIGH && PrevA==LOW && millis() - DnTimeA > Bounce) {
DnTimeA = millis();
//Serial.println("Down");
}
if (StateA==LOW && PrevA==HIGH && millis() - DnTimeA < Interval && millis() - DnTimeA > Bounce) {
//Serial.println("SHORT");
//*****
if (TiltTemp!=TiltSet){
digitalWrite(Disconnect,HIGH);
delay(20);
if (TiltSet < TiltTemp){
while (TiltSet < TiltTemp) {
digitalWrite(SwitchHBridge,HIGH);
Serial.print("Driving HIGH to TiltSet: ");
Serial.print(TiltSet);
Serial.print("\t");
Serial.print(" From: ");
Serial.print(TiltTemp);
Serial.println();
}
}
else {
while (TiltSet > TiltTemp) {
digitalWrite(SwitchHBridge,LOW);
Serial.print("Driving LOW to TiltSet: ");
Serial.print(TiltSet);
Serial.print("\t");
Serial.print(" From: ");
Serial.print(TiltTemp);
Serial.println();
}
}
digitalWrite(Disconnect,LOW);
}
//*****
}
if (StateA==LOW && PrevA==HIGH && millis() - DnTimeA > Interval && millis() - DnTimeA > Bounce) {
//Serial.println("LONG");
TiltSet = TiltTemp;
// EEPROM.write(1,TiltSet);
// Serial.print("TiltSet = ");
// Serial.print(TiltSet);
// Serial.print("stored");
// Serial.println();
}
PrevA = StateA;
}