Hi!
So I'm trying to test my encoders to see what the returned values are for different speeds. So I have produced this program to receive a serial integer value and set the motor speed to that value. Then i want to continuously output the PPS (pulses per second) of the encoder. However at the moment the program is only sending a PPS value after a serial command has been sent. Any suggestions or help would be very kindly appreciated!
Thanks
unsigned long count = 0;Â Â Â Â //Encoder pulse count
unsigned long timep, time, etime;Â Â //Time tracking
int E1 = 5;Â Â //motor speed and direction pins (i have two motors on the robot)
int E2 = 6;
int M1 = 4;
int M2 = 7;
long SPD = 0;Â Â Â Â Â Â Â Â Â Â Â Â Â Â //Set speed from serial
void setup() {
 Serial.begin(9600);
 pinMode(3,INPUT);
attachInterrupt(0,transition, CHANGE);
timep = micros();
 int i;
 for(i=4;i<=7;i++){
  pinMode(i, OUTPUT);Â
 }
Â
}
void loop() {
if(Serial.available()){
Â
 SPD = Serial.parseInt();
 Serial.println(SPD);
 if (SPD >= 0){
 analogWrite (E1,SPD);       Â
 digitalWrite(M1,LOW); Â
 analogWrite (E2,SPD); Â
 digitalWrite(M2,LOW);
 }else if (SPD < 0){
 SPD = (SPD*-1);
 analogWrite (E1,SPD);       Â
 digitalWrite(M1,HIGH); Â
 analogWrite (E2,SPD); Â
 digitalWrite(M2,HIGH);Â
 }
Â
 time = micros();
 etime = time - timep;
 if (etime > 1000000)
 {
 Serial.println(count);
 count = 0;
 timep = time;
 }
}
}
void transition()
{
 count ++;
}