SparkFun Forums 

Where electronics enthusiasts find answers.

General project discussion / help
Did you make a robotic coffee pot which implements HTCPCP and decafs unauthorized users? Show it off here!
By chickenhawk
#198227
// THIS IS TANK NUMBER 3


const int motorA_pin_1=7; // The numbers of the motor pins
const int motorA_pin_2=6; // For motor A
const int motorA_pin_speed=5;

const int motorB_pin_1=9; // The number of the motor pins
const int motorB_pin_2=8; // For motor B
const int motorB_pin_speed=10;

const int ledPin=13; // The number of the LED pin

int motorA_state_pin_1=LOW; // Used to set the state of the motors
int motorA_state_pin_2=LOW; // Low is off
int motorB_state_pin_1=LOW;
int motorB_state_pin_2=LOW;

int ledState=LOW; // The LED state is low or off

unsigned long previousMillis1=0; // Stores the time the state
unsigned long previousMillis2=0; // of the motors was checked
unsigned long previousMillis3=0;
unsigned long previousMillis4=0;
unsigned long previousMillis5=0;
unsigned long previousMillis6=0;
unsigned long previousMillis7=0;
unsigned long previousMillis8=0;
unsigned long previousMillis9=0;

const long interval_A=6000; // How long the motor will run for
const long interval_AB=5000;
const long interval_AC=4000;
const long interval_AD=3000;
const long interval_AE=2000;

const long led_interval=2000; // How long the LED will blink

void setup() { // Put your code hear to run once



pinMode(motorA_pin_1,OUTPUT); // Sets the pins to output
pinMode(motorA_pin_2,OUTPUT);
pinMode(motorA_pin_speed,OUTPUT);
pinMode(motorB_pin_1,OUTPUT);
pinMode(motorB_pin_2,OUTPUT);
pinMode(motorB_pin_speed,OUTPUT);
pinMode(ledPin,OUTPUT);

analogWrite(motorA_pin_speed,255); // sets how fast the motors will turn
analogWrite(motorB_pin_speed,255);

unsigned long currentMillis=millis();
if(currentMillis-previousMillis1>=interval_A);{ // This makes both motors
previousMillis1=currentMillis; // run forward for 6 seconds
if(motorA_state_pin_1==LOW){
if(motorB_state_pin_1==LOW)
motorA_state_pin_1=HIGH; // THIS IS LEG 1
motorB_state_pin_1=HIGH;
}else{
motorA_state_pin_1=LOW;
motorB_state_pin_1=LOW;

} // This closes the "else" statement

} // This closees the time count

} // This closes void setup

void loop() {

// put your main code here, to run repeatedly:
digitalWrite(motorA_pin_1,motorA_state_pin_1);
digitalWrite(motorB_pin_1,motorB_state_pin_1);
digitalWrite(motorA_pin_2,motorA_state_pin_2);
digitalWrite(motorB_pin_2,motorB_state_pin_2);
digitalWrite(ledPin,ledState);

} // This closes the void loop


This is my grandsons Tank program. We can't get the motors to turn off after the 6 seconds. He put in " void setup() " because he only wants it to run once. We have both looked and looked and can't seem to see where we went wrong. Can some have a look and give a hint what we may have missed.
Code: Select all
User avatar
By DanV
#198231
See, if you want to resolve millis() it can't be done inside setup().
setup() runs once and only once.
if you put this code inside void loop() it should work because loop() gets called over and over by the system.
Code: Select all
if(currentMillis-previousMillis1>=interval_A);{ // This makes both motors
   previousMillis1=currentMillis; // run forward for 6 seconds
   if(motorA_state_pin_1==LOW){
     if(motorB_state_pin_1==LOW)
     motorA_state_pin_1=HIGH; // THIS IS LEG 1
     motorB_state_pin_1=HIGH;
   }else{
     motorA_state_pin_1=LOW;
     motorB_state_pin_1=LOW;

    } // This closes the "else" statement

 } // This closees the time count
By chickenhawk
#198232
He does not to run over and over again. He want the tank to run forward for 6 seconds then stop then turn right- go forward-then turn right again. What he is trying to do is make the tank do one big square on the floor then stop. Is it because you can not use mills in side setup()?

He put it in setup() because he only want it to run once.
By jremington
#198236
Use code tags when posting code.

The body of an if statement executes only once (or not at all) regardless of where it is. So, the logic of, the construction of and the comments associated with the following lines of code are wrong. Also, there must not be a ";" between the if clause and the opening brace.
Code: Select all
if(currentMillis-previousMillis1>=interval_A);{ // This makes both motors
   previousMillis1=currentMillis; // run forward for 6 seconds
In setup() you would need to use a loop block like while() or do ... while() to do the job.
By chickenhawk
#198250
Sorry we are very new to this, he did have it working using " delay " but he was told to get rid of the " delay " and use " blink without delay" He will remove the ";" but we are not sure about " the construction of and the comments associated with the following lines of code are wrong". We will sit down and read about using while().