Hi all this is my first time posting on here i hope that you can help me with this project it is doing my head in. I'm sure its something simple i just cant see it.
So I am doing a project where i need to control a servo motor with a 50% square sign wave with a frequency from 166Hz to 3000Hz and also control a DC motor with a simple on off signal.
When a start signal is received both motors turn on and when a stop signal is received they both stop.
I know that the hardware is all working as it should as i have previously had it running correctly but then i lost my original sketch and had to start again. I have attached my code below.
For some reason when i start the program the variable Pinchstart goes to true and will not react to my interrupts.
Any ideas as to why this may be happening would be greatly appreciated.
//============================MC Pin Variables============================
int FrtPin = 2 ; //Pin for Fruit Sensor input
int LblPin = 3 ; //Label Sensor input
int PotPin = A0 ; //Potentiometer input
int PinchPin = 5 ; //Pinch output
// int AlarmSignalPin = 4; //Alarm output
int PullPin = 11 ; //Stepper Motor pulse
// int ControlSignalPin = 10; //global/local speed selection
// int ControlSignalValuePin = A2; //speed value
//const int SignalPin = 7 ; //Pull motor signal
//=======================Variables for main motor Speed==================
unsigned long StartTime;
unsigned long CheckTime;
volatile boolean Pinchstart=false;
volatile boolean SignalState=false;
int PotReading = 0;
int SwitchTime = 167;
//===========================================================================
void StartSignal(){
Pinchstart=true;
}
void StopSignal(){
Pinchstart=false;
}
//===========================================================================
void setup() {
pinMode(FrtPin, INPUT);
pinMode(LblPin,INPUT);
// pinMode(ControlSignalPin,INPUT);
// pinMode(ControlSignalValuePin,INPUT);
pinMode(PinchPin, OUTPUT);
// pinMode(AlarmSignal, OUTPUT);
pinMode(PullPin, OUTPUT);
attachInterrupt(digitalPinToInterrupt(FrtPin),StartSignal,RISING);
attachInterrupt(digitalPinToInterrupt(LblPin),StopSignal,RISING);
}
void loop(){
if(Pinchstart=true){
StartTime=micros(); //Takes start time
digitalWrite(PullPin,HIGH); //Starts DC motor
PotReading=analogRead(PotPin); //Reads potentiometer reading //This can be moved into above loop but will increase time for cycle
SwitchTime=map(PotReading,0,1023,166,3000); //To run Pinch motor from 50rpm to 900rpm sets the speed range
StartTime=StartTime+SwitchTime; //Determines next time that pulse signal needs to change state.
while(Pinchstart=true){
CheckTime=micros(); //Checkstime
if(CheckTime>StartTime){
digitalWrite(PinchPin,SignalState); //Swaps signal sign
SignalState=!SignalState;
StartTime=CheckTime+SwitchTime; //Determins next time that pulse signal needs to change state.
}
}
}
if(Pinchstart=false){
digitalWrite(PullPin,LOW);
}
}