Go Down

Topic: Interrupting the loop (Read 989 times) previous topic - next topic

eckaus15

#15
Jun 10, 2015, 09:18 pm Last Edit: Jun 10, 2015, 09:21 pm by eckaus15
So what I currently have set up runs it forward for a determined amount of time and then back for a determined amount of time. There is something messing up with the millis because instead of switching at around 1100 millis it switches at 2500-3000 millis. Any thoughts?

Code: [Select]


// -----LIBRARIES

#include <Servo.h>

// ----CONSTANTS (won't change)

const int servoPin = 8; // the pin number for the servo signal
const int servoMinDegrees = 20; // the limits to servo movement
const int servoMaxDegrees = 150;


int a = 2;
// 1 = Fast forward
// 2 = medium forward
// 3 = slow forward
// 4 == slow back
// 5 == medium back
// 6 == fast back
int power_mapping [] = { 0, 160, 135, 110, 70, 50, 30 }; //Speed Settings
int power_mappingReversed [] = { 0, 30, 50, 70, 110, 135, 160 }; //Speed Settings
float speed_mapping [] = { 0, 6.10501, 2.92654, 0.80321, 0.88417, 2.56805, 4.83092 }; //Speed in Feet Per Second
float speed_mappingReversed [] = { 0, 4.83092, 2.56805, 0.88417, 0.80321, 2.92654, 6.10501 }; //Speed in Feet Per Second
int b = 3; //This is distance in feet forward
int c = 5; //distance for U
int motor1 = 7;// defines pin 5 as connected to the motor FRONT MOTOR STEERING
int motor2 = 8;// defines pin 8 as connected to the motor REAR MOTOR
int channel1 = 9; // defines the channels that are connected
int channel2 = 10;// to pins 9 and 10 of arduino respectively
int analogOutputF ; //Used later to
int analogOutputB ; // store values
int Channel1 ; // Used later to
int Channel2 ; // store values
int athome = 0; //homing for straight wheels
int switchingServo = 0;
int servoInt;



//------- VARIABLES (will change)

Servo ST1;
Servo ST2; 

int servoPosition = 90;     // the current angle of the servo - starting at 90.
int servoInterval = servoInt; // initial millisecs between servo moves
                              //    will be changed to negative value for movement in the other direction

unsigned long currentMillis = 0;    // stores the value of millis() in each iteration of loop()
unsigned long previousServoMillis = 0; // the time when the servo was last moved

//========

void setup() {

   pinMode (motor1, OUTPUT);// initialises the motor pins
   pinMode (motor2, OUTPUT);
   pinMode (channel1, INPUT);// initialises the channels
   pinMode (channel2, INPUT);// as inputs
 Serial.begin(9600);
 Serial.println("Starting SeveralThingsAtTheSameTimeRev1.ino");  // so we know what sketch is running
 
 ST1.write(servoPosition); // sets the initial position
 ST1.attach(8);
 ST2.attach(7);
}

//=======

void loop() {

     // Notice that none of the action happens in loop() apart from reading millis()
     //   it just calls the functions that have the action code
 
  Channel1 = (pulseIn (channel1, HIGH)); // Checks the value of channel1
  //Serial.println (Channel1); //Prints the channels value on the serial monitor
 
  Channel2 = (pulseIn (channel2, HIGH)); // Checks the value of channel1
  //Serial.println (Channel2); //Prints the channels value on the serial monitor
   
   
   Serial.println (currentMillis - previousServoMillis);
   Serial.println (servoInt);

 currentMillis = millis();   // capture the latest value of millis()


  if (Channel1 == 0 && Channel2 == 0){
   servoSweep();
  }else{
   RC();
  }
}

//========

void servoSweep() {

     // this is similar to the servo sweep example except that it uses millis() rather than delay()

     // nothing happens unless the interval has expired
     // the value of currentMillis was set in loop()
     
     // Serial.println (servoInt);
 

 if (currentMillis - previousServoMillis >= servoInt) {
       // its time for another move
       

      previousServoMillis = currentMillis;

   if (switchingServo == 0){
     ST1.write(power_mapping [a]);
     servoInt = ((b/speed_mapping [a]) * 1000);
     switchingServo = 1;
   }else{
     ST1.write(power_mappingReversed [a]);     
     servoInt = ((b/speed_mappingReversed [a]) * 1000);
     switchingServo = 0;
   }
   

     
  }
 
 
}

void RC(){
     //BACK MOTOR
   analogOutputF = map(Channel2, 990, 1900, 0, 180);
   ST1.write(analogOutputF);
     Serial.println (Channel2);
   
     //STEERING
   analogOutputB = map(Channel1, 1260, 1660, 0, 180);
   if (Channel1 > 1500 && Channel1 < 1700){
     ST2.write(180);
        //Serial.println (180);

   }else if (Channel1 < 1400 && Channel1 < 1300){
     ST2.write(0);
        //Serial.println (0);

   }else if (Channel1 > 1400 && Channel1 < 1500){
     ST2.write(90);
        //Serial.println (110);

 
}
}


//=====END

Robin2

Sorry, but I can't figure out how your program is supposed to work.

Is the problem with the code in the function servoSweep() or is it somewhere else ?

What values does this give you?
servoInt = ((b/speed_mapping [a]) * 1000);

I suspect b/speed_mapping returns 0 because it is integer maths. Maybe try doing the * 1000 first.

"b" and "a"  are poor names for variables. Code is much easier to understand with meaningful names.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Go Up