tmd3: Thank you for the quick reply. Your explanation was great!
I actually had I.S.R.s but had them commented out. I uncommented them and the program millis() began working again. Sorry for not including everything (like my newt) definition). I was trying to simplify things as to not confuse others but I think I got the opposite result. I will try to be more complete.
My code is actually a bit longer and I'm trying to be modular with it in an attempt to assist in the debugging process. I added parts of my code back in and I'm not getting another problem - one that I get often in fact.
The output to the serial monitor shows that newt is continually progressing - like it should - with the following code.
#define pwmOut 9
int buttonIn=12, potIn=A1, potVal=0, motorRun=0, state, oldState, count=0;
double angularRate, jointAngle=0, oldAngle=1.98, gain=0.01, regA, regB,OrigRegA,OrigRegB, percent, targAngRate;
const double pi=3.1416;
unsigned long newt, oldt=0;
void setup()
{
Serial.begin(9600);
pinMode(pwmOut, OUTPUT);
// initialize timer1
noInterrupts(); // disable all interrupts
TCCR1A = 0;
TCCR1B = 0;
TCNT1 = 0;
// Compare B has to be before compare A because the timer TCNT1
// resets when it matches OCR1A
OCR1A = 64000;
OCR1B = 63000; // compare match register
TCCR1B |= (1 << WGM12)|(1 << CS10);
TIMSK1 |= (1 << OCIE1A)|(1 << OCIE1B); // enable timer compare interrupt
interrupts(); // enable all interrupts
}
ISR(TIMER1_COMPB_vect) // timer compare interrupt service routine
{
digitalWrite(pwmOut, HIGH);
}
ISR(TIMER1_COMPA_vect) // timer compare interrupt service routine
{
digitalWrite(pwmOut, LOW);
}
void loop()
{
oldState=state;
if (digitalRead(buttonIn)==LOW)
{
state=0;
}
else
{
state=1;
}
if (state!=oldState)
{
motorRun=1;
regA=OrigRegA;
regB=OrigRegB;
}
// Reads the voltage from the Goniometer/potentiometer
// and maps 0-5V to 10 bit integer value (0-1023)
potVal = analogRead(potIn);
// Reads the voltage from the from Hall sensors on the
// motor and maps 0-5V to 10 bit integer value (0-1023)
// currentVal = analogRead(currentPin);
jointAngle = (0.3333*potVal-170)*pi/180;
newt=micros();
Serial.print((newt));
Serial.print("\t");
}
But when I add the last bit of code after serial.print() (code in full shown below) I only get two values in the serial monitor. Any thoughts?
#define pwmOut 9
int buttonIn=12, potIn=A1, potVal=0, motorRun=0, state, oldState, count=0;
double angularRate, jointAngle=0, oldAngle=1.98, gain=0.01, regA, regB,OrigRegA,OrigRegB, percent, targAngRate;
const double pi=3.1416;
unsigned long newt, oldt=0;
void setup()
{
Serial.begin(9600);
pinMode(pwmOut, OUTPUT);
// initialize timer1
noInterrupts(); // disable all interrupts
TCCR1A = 0;
TCCR1B = 0;
TCNT1 = 0;
// Compare B has to be before compare A because the timer TCNT1
// resets when it matches OCR1A
OCR1A = 64000;
OCR1B = 63000; // compare match register
TCCR1B |= (1 << WGM12)|(1 << CS10);
TIMSK1 |= (1 << OCIE1A)|(1 << OCIE1B); // enable timer compare interrupt
interrupts(); // enable all interrupts
}
ISR(TIMER1_COMPB_vect) // timer compare interrupt service routine
{
digitalWrite(pwmOut, HIGH);
}
ISR(TIMER1_COMPA_vect) // timer compare interrupt service routine
{
digitalWrite(pwmOut, LOW);
}
void loop()
{
oldState=state;
if (digitalRead(buttonIn)==LOW)
{
state=0;
}
else
{
state=1;
}
if (state!=oldState)
{
motorRun=1;
regA=OrigRegA;
regB=OrigRegB;
}
// Reads the voltage from the Goniometer/potentiometer
// and maps 0-5V to 10 bit integer value (0-1023)
potVal = analogRead(potIn);
// Reads the voltage from the from Hall sensors on the
// motor and maps 0-5V to 10 bit integer value (0-1023)
// currentVal = analogRead(currentPin);
jointAngle = (0.3333*potVal-170)*pi/180;
newt=micros();
Serial.print((newt));
Serial.print("\t");
angularRate=abs(jointAngle-oldAngle)/((newt-oldt)/1000000);
oldAngle=jointAngle;
oldt=newt;
percent=1-(jointAngle - 0.3)/1.68;
targAngRate=(-242.2086*pow(percent,3)+172.8958*pow(percent,2)+58.4458*percent+52.1349)*pi/180;
//On off transient logic
if (jointAngle<0.3)
{
motorRun = 0;
}
if (motorRun==1)
{
OCR1A=regA;
OCR1B=regB;
regB=regB+(angularRate-targAngRate)*gain;
OCR1B = regB;
}
else
{
OCR1A = 0;
OCR1B = 0;
}
}