I am working on a vehicle controller for a new design project for freshman engineering students. I have added a simple optosensor to measure counts of a rotating wheel and another optosensor to read whether or not the vehicle is still on the track (One is reading counts that the students will download after runs, the other is a safety cutoff that will shut down the motors if it comes off the track and the sensor doesn't read anything)
I am confused on the use of Interrupt timers. I am using Timer 2 to gather info at particular time intervals (other data is recorded as well and the students will download using EEPROM after each trial but I don't have that in my code). The safety shut off switch is a new development and I am not quite sure if I can use port 3 as an interrupt for this added optosensor? Also the development team wants to use port 11 for a 3rd (optional) motor control pin, is the pwm function disabled for 3 and 11?
I wrote simple code to accomplish what I wanted but I haven't written the code to add the optosensor to pin 3 because I didn't know if that would still work. To advanced programmers this code is probably horrible but this is branching outside of my skill set and I am learning as I go. If anyone has suggestions on how to write this code better please feel free to comment.
Thanks for your time
Andrew
#include <MsTimer2.h>
byte Counter[500]; // Counter array capable of 500 Entries
byte i=0; // i should be between 0 and 255
int k=0; // index variable
int EncoderPin=2;
void setup()
{
pinMode(EncoderPin, INPUT);
attachInterrupt(0, ShaftEncoder, CHANGE); // Interrupt 0 is Pin 2
MsTimer2::set(500, InterruptTimer); // Take data every .5 seconds
MsTimer2::start();
}
void loop()
{
}
void ShaftEncoder()
{
i++;
}
void InterruptTimer()
{
Counter[k]=i; // Deposits Counts in array
k++; // Advance index
i=0; // Resets counter
}
The first thing that strikes me is that there is nothing to limit 'k' to the range 0..499, so it could very quickly start trashing memory.
The second thing that strikes me is 'why are you exposing freshmen to interrupts at all?'
Another question is, where come the values of i from and what's happening with your Counter array. Possibly you may have to declare some as volatile, possibly not. If things behave as you expect, go on until they don't or your project is done.
Also be aware, that EEPROM won't survive infinite writing to them. They go bust.
And I agree with Groove, what's the point of exposing your student to interrupts specially if you don't master the subject yourself yet. Or did we just misunderstand you and you're building a black box for them to control?
I am building a black box that the students control. I have done the design construction and also I was made to program the device.
The students will have simple commands that they input to make the vehicle go. The course time limit is 2 minutes. So 2 min X 60 seconds X readings every 1/2 second means about 240 readings. I choose 500 to give some extra time.
EEPROM won't survive infinite writing? I think we are using 256K EEPROM and I am trying to take into account the limit, is that what you mean? What did you mean that they go bust?
vaules of i are coming from a digital optosensor that only gives 1's and 0's, do I still need to declare some as volatile.
I know that I am very ignorant with programming but I am learning as I go. So the programming help is for me as I make a workable device that the students can use.
Now are there any answers to the interrupt timers in conjunction with motors and interrupts?
No. The manufacturer provides a guarenteed life (look to the datasheet for the number). Most survive much longer. But, past that point, the manufacturer is not going to replace the device if it fails.
I think we are using 256K EEPROM and I am trying to take into account the limit, is that what you mean?
Check the datasheet for the write cycles.
What did you mean that they go bust?
They stop working. Stuck bits. Toggled bits.
Bear in mind that the failure applies to WRITES. READS do not cause wear.
CB,
Thanks for directing me to that post. I have read all the entries and also some of the other posts that are associated with it. I understand much more about interrupts and declaring something volatile.
I am looking into the EEPROM issue and I'm trying to switch to some kind of flash memory but I am not sure what my department is capable of. I will post later if I have questions regarding that.
Finally, I was asking to see if Timer2 will disable using ports 3 or 11? I want to use 3 for another digital sensor and I wanted to use 11 to control a motor. Thanks for all the help