Motor Driving Problem

Hi Guys,

I've been working on a project for eight months now, and i'm stuck right at the end of it, 2 months trying to work it out has failed.
What I have, is a motor driving in two directions and receiving pulses (count) as it turns, via an interrupt service routine, all working well.
For testing purposes, I'm using a count from zero to 600,(but I need it to stop at a preset value in between without overrunning) .I need to start the motor slow, via PWM and preferably, PWM starting at zero and ramping up to PWM 255(delay 10),so that it slowly gains speed and doesnt miss a count pulse, but I also need it to slow when approaching the stopping point (PresetCount value).I have two functions,void forward and void backward each having their own ISR.Stopping is working ok via dynamic braking, but i need it to be a lot slower than full speed , so it doesnt overrun (which it does at full speed).
So, in a nutshell,
SensorCount-PresetCount <=30, FwdPWM,0-50, ramp up slowly for one rev then brake.
SensorCount-PresetCount >30, FwdPWM,0-255, ramp up to full speed to save time.
Then as approaching preset value, SensorCount-PresetCount <=30, PWM,255-50, ramp down to slow for last rev.Dynamic braking will stop it from there. ie void stopping().

I also need to show my display as it drives ( Display ( ) ) .

I have also tried switch case, but I cant get the case to be <=30, only 30 works.

If you are able to help, only one function, void Forward(), will be required, i will modify for myself the backward function.

Thanks in advance for any help.

volatile unsigned int SensorCount  ; //SensorCount stored to eeprom then read from eeprom on next start up from power off.
// Count is the value shown on 7 segment display, while driving
//PresetCount is shown on 7 sement display while pressetting
// SensorCount is from an IR tx/rx sensor.


void Forward()
{
  digitalWrite (DriveBwdN,LOW);  // Safety precaution so as not to create dead short with Fwd and Bwd driving together
  digitalWrite (DriveBwdP,LOW);  // Safety precaution so as not to create dead short with Fwd and Bwd driving together
  attachInterrupt (SensorPin,CounterPulseFwd,FALLING);   // this is my count value
  digitalWrite (DriveFwdP,HIGH);  //H Bridge PNP is now on
  digitalWrite (FwdLED,HIGH);  // LED direction indicator  



/* fix here down, this doesnt work
  {
    for (int f=0; f <= 255; f++)
    {
      analogWrite (DriveFwdN,f);   // H Bridge NPN PWM
      if ((SensorCount - PresetCount) <=30 )  // as I need to slow

      {
        analogWrite (DriveFwdN,50);
        Count = (SensorCount*Divisor);      
  Display ();
      } 
      Count = (SensorCount*Divisor);  // modified for future purposes, currently divisor is 1, must be incorporated
      Display ();
      delay(10);  // speed ramping time
    }
  }
}
*/

// This is working ok
void CounterPulseFwd ()      
{
  SensorCount = SensorCount --; 

  if (SensorCount == PresetCount)
  {
    Stopping();
  } 
}


// This will be the opposite to Forward
void Backward()
{
  digitalWrite (DriveFwdN,LOW);  // Safety precaution so as not to create dead short with Fwd and Bwd driving together
  digitalWrite (DriveFwdP,LOW);  // Safety precaution so as not to create dead short with Fwd and Bwd driving together
  attachInterrupt (SensorPin,CounterPulseBwd,FALLING); 
  digitalWrite (DriveBwdP,HIGH);
  digitalWrite (BwdLED,HIGH);



}

void Stopping()
{
  digitalWrite (DriveFwdP,LOW);
  digitalWrite (DriveBwdP,LOW);
  digitalWrite (DriveBwdN,HIGH);  //Dynamic braking
  digitalWrite (DriveFwdN,HIGH);  //Dynamic braking
  delay (3000);
  digitalWrite (DriveBwdN,LOW);
  digitalWrite (DriveFwdN,LOW);
  //digitalWrite (Sensor,LOW); 
  digitalWrite (BwdLED,LOW);
  digitalWrite (FwdLED,LOW);
  detachInterrupt (SensorPin);
  Ewrite ();  // write
}



void Ewrite () // Write SensorCount to eeprom for next startup
{
  byte HiByte = highByte(SensorCount); //splitting 2 bytes of count into 2 separate bytes
  byte LoByte = lowByte (SensorCount); //splitting 2 bytes of count into 2 separate bytes
  EEPROM.write (Address,HiByte);  // writing the byte as 1 byte high in 1st address
  EEPROM.write (Address+1,LoByte); // writing the byte as 1 byte low in 2nd address
}

Read this before posting a programming question

Please edit your post, select the code, and put it between [code] ... [/code] tags.

You can do that by hitting the # button above the posting area.

Plus post all of it. For example how is SensorCount declared?

I have two voids,void forward and void backward ...

May as well get the terminology right. You have two functions. They each are declared to return void (that is, no return value).