motor control

const int switchPin = 2; // switch connected to digital pin 2
int pot1 = 0; // pot1 connected to analog 0
int pot2 = 1; // pot2 connected to analog 1
int pot3 = 2; // pot3 connected to analog 2
int val1 = 0;
int val2 = 0;
int val3 = 0;
int motor1Pin = 5; // h bridge pin connected digital 5
int motor2Pin = 6; // h bridge pin connected to digital 6
int motor3Pin = 10; // hbridge pin connected to digital 10
int enablePin = 9; //h bridge enable pin connected to digital 9
int enablePin1 = 12; // hbridge enable pin connected to digital 12


void setup()
{
 Serial.begin(9600);
  pinMode(switchPin, INPUT);
  pinMode(motor1Pin, OUTPUT);
  pinMode(motor2Pin, OUTPUT);
  pinMode(enablePin, OUTPUT);
  digitalWrite(enablePin, HIGH);
  digitalWrite(enablePin1, HIGH);






}
void loop()
{
if (digitalRead(switchPin) == HIGH) {
  val1 = analogRead(pot1);
  val1 = map(val1, 0, 1023, 0, 255);
  analogWrite(motor1Pin, val1);
  Serial.print(val1);
  Serial.print(" Motor 1: ");

  val2 = analogRead(pot2);
  val2 = map(val2, 0, 1023, 0, 255);
  analogWrite(motor2Pin, val2);
  Serial.print(val2);
  Serial.print(" Motor 2: ");

  val3 = analogRead(pot3);
  val3 = map(val3, 0, 1023, 0, 255);
  analogWrite(motor3Pin, val3);
  Serial.println(val3);
  Serial.print(" Motor 3: ");
}
else {
delay(1000);
  analogWrite(motor1Pin, 100);
  delay(2000);
  analogWrite(motor1Pin, 124);
  delay(2000);
  analogWrite(motor2Pin, 100);
  analogWrite(motor2Pin, 200);
  analogWrite(motor3Pin, 155);
  delay(2000);
  analogWrite(motor1Pin, 200);
  analogWrite(motor3Pin, 0);
  analogWrite(motor2Pin, 167);
  delay(2000);
analogWrite(motor3Pin, 0);
analogWrite(motor2Pin, 130);
analogWrite(motor1Pin, 180);
delay (2000);
analogWrite(motor3Pin, 109);
analogWrite(motor1Pin, 190);
analogWrite(motor2Pin, 205);
delay (2000);
analogWrite(motor1Pin, 115);
delay (100);
analogWrite(motor3Pin, 160);
analogWrite(motor1Pin, 0);
analogWrite(motor3Pin, 135);
delay (2000);
analogWrite(motor3Pin, 140);
analogWrite(motor2Pin, 200);
analogWrite(motor1Pin, 190);
delay (2000);
analogWrite(motor1Pin, 100);
analogWrite(motor2Pin, 0);
analogWrite(motor3Pin, 0);
delay(100);
analogWrite(motor2Pin, 152);
analogWrite(motor1Pin, 0);
analogWrite(motor3Pin, 115);
delay(100);
analogWrite(motor1Pin, 199);
analogWrite(motor2Pin, 140);
analogWrite(motor3Pin, 178);
delay(2000);
analogWrite(motor3Pin, 100);
analogWrite(motor1Pin, 0);
analogWrite(motor2Pin, 0);
delay(100);
analogWrite(motor1Pin, 240);
analogWrite(motor2Pin, 200);
analogWrite(motor3Pin, 120);
delay(2000);
analogWrite(motor1Pin, 170);
delay(100);
analogWrite(motor2Pin, 200);
analogWrite(motor3Pin, 130);
delay(2000);
analogWrite(motor1Pin, 0);
analogWrite(motor2Pin, 0);
analogWrite(motor3Pin, 0);
delay(100);
analogWrite(motor3Pin, 105);
analogWrite(motor2Pin, 105);
analogWrite(motor1Pin, 235);
delay(2000);

}
}

Simplest way (you don't need interrupt handlers) would be to instead of calling delay (inside the your else clause), call a function called "myDelay", and loop inside that function like so (rusty C code ahead!):

boolean myDelay(int delay) {
  int count = 0;
  boolean retval = false;

  while (!(retval = digitalRead(switchPin)) && !(count == delay)) {
    count++;
    delay(1);
  }

  return retval;
}

So - while you delay, you are checking for the button state, and if you press it, break out, like so:

  ...
}
else {
  if (myDelay(1000)) break;
  analogWrite(motor1Pin, 100);
  ...
  ... etc...
  ...
}

Does this make sense (or at least, does it give you a sense of what to do)? Hope it helps, at least...

:)

Hmm ya that's some old c code... Kinda understand it sorda But why I dont get is why my code wont do the same?

i tried changing the code but no luck i havent tried yours yet

const int switchPin = 2; // switch connected to digital pin 2 
int pot1 = 0; // pot1 connected to analog 0
int pot2 = 1; // pot2 connected to analog 1
int pot3 = 2; // pot3 connected to analog 2
int val1 = 0;
int val2 = 0;
int val3 = 0;
int switchpinState = 0;
int motor1Pin = 5; // h bridge pin connected digital 5
int motor2Pin = 6; // h bridge pin connected to digital 6
int motor3Pin = 10; // hbridge pin connected to digital 10
int enablePin = 9; //h bridge enable pin connected to digital 9
int enablePin1 = 12; // hbridge enable pin connected to digital 12


void setup()
{
 Serial.begin(9600);
  pinMode(switchPin, INPUT);
  pinMode(motor1Pin, OUTPUT);
  pinMode(motor2Pin, OUTPUT);
  pinMode(enablePin, OUTPUT);
  digitalWrite(enablePin, HIGH);
  digitalWrite(enablePin1, HIGH);



  


}
void loop()
{
switchpinState = digitalRead(switchPin);
if (switchpinState == HIGH)
{
  val1 = analogRead(pot1);
  val1 = map(val1, 0, 1023, 0, 255);
  analogWrite(motor1Pin, val1);
  Serial.print(val1);
  Serial.print(" Motor 1: ");

  val2 = analogRead(pot2);
  val2 = map(val2, 0, 1023, 0, 255);
  analogWrite(motor2Pin, val2);
  Serial.print(val2);
  Serial.print(" Motor 2: ");
  
  val3 = analogRead(pot3);
  val3 = map(val3, 0, 1023, 0, 255);
  analogWrite(motor3Pin, val3);
  Serial.println(val3);
  Serial.print(" Motor 3: ");
}
else {
  digitalRead(switchpinState == LOW);
  analogWrite(motor1Pin, 100);
  delay(2000);
  analogWrite(motor1Pin, 124);
  delay(2000);
  analogWrite(motor2Pin, 100);
  analogWrite(motor2Pin, 200);
  analogWrite(motor3Pin, 155);
  delay(2000);
  analogWrite(motor1Pin, 200);
  analogWrite(motor3Pin, 0);
  analogWrite(motor2Pin, 167);
  delay(2000);
analogWrite(motor3Pin, 0);
analogWrite(motor2Pin, 130);
analogWrite(motor1Pin, 180);
delay (2000);
analogWrite(motor3Pin, 109);
analogWrite(motor1Pin, 190);
analogWrite(motor2Pin, 205);
delay (2000);
analogWrite(motor1Pin, 115);
delay (100);
analogWrite(motor3Pin, 160);
analogWrite(motor1Pin, 0);
analogWrite(motor3Pin, 135);
delay (2000);
analogWrite(motor3Pin, 140);
analogWrite(motor2Pin, 200);
analogWrite(motor1Pin, 190);
delay (2000);
analogWrite(motor1Pin, 100);
analogWrite(motor2Pin, 0);
analogWrite(motor3Pin, 0);
delay(100);
analogWrite(motor2Pin, 152);
analogWrite(motor1Pin, 0);
analogWrite(motor3Pin, 115);
delay(100);
analogWrite(motor1Pin, 199);
analogWrite(motor2Pin, 140);
analogWrite(motor3Pin, 178);
delay(2000);
analogWrite(motor3Pin, 100);
analogWrite(motor1Pin, 0);
analogWrite(motor2Pin, 0);
delay(100);
analogWrite(motor1Pin, 240);
analogWrite(motor2Pin, 200);
analogWrite(motor3Pin, 120);
delay(2000);
analogWrite(motor1Pin, 170);
delay(100);
analogWrite(motor2Pin, 200);
analogWrite(motor3Pin, 130);
delay(2000);
analogWrite(motor1Pin, 0);
analogWrite(motor2Pin, 0);
analogWrite(motor3Pin, 0);
delay(100);
analogWrite(motor3Pin, 105);
analogWrite(motor2Pin, 105);
analogWrite(motor1Pin, 235);
delay(2000);

}
}

The reason why it doesn't work is because of all of the calls to the delay() function; during the delay() call, the execution pauses for however many milliseconds you are delaying for - so a call like "delay(1000);" waits for 1 second, and nothing else occurs (outside of interrupts, serial handling, and few other odds and ends):

http://arduino.cc/en/Reference/Delay

You might also want to review the "BlinkWithoutDelay" example sketch (at the bottom of the reference page above), too (which is another - and probably more accurate - way to implement this kind of functionality).

Good luck...

:)

I understand what your saying. I looked at the example. Reason why I had the delays was to have that "picture" display for 2 seconds etc... Still researching on it. Hopefully Ill get it figured out ;) So do I need to define delay? Also, do I need to define motor speed (0-255) Like: int mydelay1 = 1000 int motorspeed = randumnumber ?

So basically call else { ....

Else { ....

Etc etc

} }

Maybe this will make things clearer?

const int switchPin = 2; // switch connected to digital pin 2

int pot1 = 0; // pot1 connected to analog 0
int pot2 = 1; // pot2 connected to analog 1
int pot3 = 2; // pot3 connected to analog 2

int val1 = 0;
int val2 = 0;
int val3 = 0;

int switchpinState = 0;

int motor1Pin = 5;   // h bridge pin connected digital 5
int motor2Pin = 6;   // h bridge pin connected to digital 6
int motor3Pin = 10;  // h bridge pin connected to digital 10

int enablePin = 9;   // h bridge enable pin connected to digital 9
int enablePin1 = 12; // h bridge enable pin connected to digital 12

void setup() {
  Serial.begin(9600);

  pinMode(switchPin, INPUT);
  pinMode(motor1Pin, OUTPUT);
  pinMode(motor2Pin, OUTPUT);
  pinMode(enablePin, OUTPUT);

  digitalWrite(enablePin, HIGH);
  digitalWrite(enablePin1, HIGH);
}

void loop() {
  switchpinState = digitalRead(switchPin);

  if (switchpinState == HIGH) {
    val1 = analogRead(pot1);
    val1 = map(val1, 0, 1023, 0, 255);
  
    analogWrite(motor1Pin, val1);

    Serial.print(val1);
    Serial.print(" Motor 1: ");

    val2 = analogRead(pot2);
    val2 = map(val2, 0, 1023, 0, 255);

    analogWrite(motor2Pin, val2);

    Serial.print(val2);
    Serial.print(" Motor 2: ");

    val3 = analogRead(pot3);
    val3 = map(val3, 0, 1023, 0, 255);

    analogWrite(motor3Pin, val3);

    Serial.println(val3);
    Serial.print(" Motor 3: ");
  }
  else {
    analogWrite(motor1Pin, 100);
    if (myDelay(2000)) break; // break out of else clause and continue loop if button is pressed

    analogWrite(motor1Pin, 124);
    if (myDelay(2000)) break;

    analogWrite(motor2Pin, 100);
    analogWrite(motor2Pin, 200);
    analogWrite(motor3Pin, 155);
    if (myDelay(2000)) break;

    analogWrite(motor1Pin, 200);
    analogWrite(motor3Pin, 0);
    analogWrite(motor2Pin, 167);
    if (myDelay(2000)) break;

    analogWrite(motor3Pin, 0);
    analogWrite(motor2Pin, 130);
    analogWrite(motor1Pin, 180);
    if (myDelay(2000)) break;

    analogWrite(motor3Pin, 109);
    analogWrite(motor1Pin, 190);
    analogWrite(motor2Pin, 205);
    if (myDelay(2000)) break;

    analogWrite(motor1Pin, 115);
    if (myDelay(100)) break;

    analogWrite(motor3Pin, 160);
    analogWrite(motor1Pin, 0);
    analogWrite(motor3Pin, 135);
    if (myDelay(2000)) break;

    analogWrite(motor3Pin, 140);
    analogWrite(motor2Pin, 200);
    analogWrite(motor1Pin, 190);
    if (myDelay(2000)) break;

    analogWrite(motor1Pin, 100);
    analogWrite(motor2Pin, 0);
    analogWrite(motor3Pin, 0);
    if (myDelay(100)) break;

    analogWrite(motor2Pin, 152);
    analogWrite(motor1Pin, 0);
    analogWrite(motor3Pin, 115);
    if (myDelay(100)) break;

    analogWrite(motor1Pin, 199);
    analogWrite(motor2Pin, 140);
    analogWrite(motor3Pin, 178);
    if (myDelay(2000)) break;

    analogWrite(motor3Pin, 100);
    analogWrite(motor1Pin, 0);
    analogWrite(motor2Pin, 0);
    if (myDelay(100)) break;

    analogWrite(motor1Pin, 240);
    analogWrite(motor2Pin, 200);
    analogWrite(motor3Pin, 120);
    if (myDelay(2000)) break;

    analogWrite(motor1Pin, 170);
    if (myDelay(100)) break;

    analogWrite(motor2Pin, 200);
    analogWrite(motor3Pin, 130);
    if (myDelay(2000)) break;

    analogWrite(motor1Pin, 0);
    analogWrite(motor2Pin, 0);
    analogWrite(motor3Pin, 0);
    if (myDelay(100)) break;

    analogWrite(motor3Pin, 105);
    analogWrite(motor2Pin, 105);
    analogWrite(motor1Pin, 235);
    if (myDelay(2000)) break;
  }
}

boolean myDelay(int delay) {
  int count = 0;
  boolean retval = false;

  while (!(retval = digitalRead(switchPin)) && !(count == delay)) {
    count++;
    delay(1);
  }

  return retval;
}

I basically took your code and modified it to use the new delay function that I wrote earlier (I can't guarantee everything is correct)...

:)

thanks ;) I was getting errors earlier trying to implement your code previously

I tried uploading that: In function 'void loop()': error: break statement not within loop or switch In function 'boolean myDelay(int)':

 }
  else {
    analogWrite(motor1Pin, 100);
    if (myDelay(2000)) break; // break out of else clause and continue loop if button is pressed

thats where it pops up

from what I'm seeing its not recognziing myDelay

Hmm - ok, my assumption that it would break out of the else clause was WRONG...

:(

Lemme think a minute...

Try this implementation - it uses a state machine instead and should break ok (Note - I am coding all of this "blind"; I don't have my Arduino or IDE anywhere near me, so no idea if it will work!):

const int switchPin = 2; // switch connected to digital pin 2

int pot1 = 0; // pot1 connected to analog 0
int pot2 = 1; // pot2 connected to analog 1
int pot3 = 2; // pot3 connected to analog 2

int val1 = 0;
int val2 = 0;
int val3 = 0;

int switchpinState = 0;

int motor1Pin = 5;   // h bridge pin connected digital 5
int motor2Pin = 6;   // h bridge pin connected to digital 6
int motor3Pin = 10;  // h bridge pin connected to digital 10

int enablePin = 9;   // h bridge enable pin connected to digital 9
int enablePin1 = 12; // h bridge enable pin connected to digital 12

void setup() {
  Serial.begin(9600);

  pinMode(switchPin, INPUT);
  pinMode(motor1Pin, OUTPUT);
  pinMode(motor2Pin, OUTPUT);
  pinMode(enablePin, OUTPUT);

  digitalWrite(enablePin, HIGH);
  digitalWrite(enablePin1, HIGH);
}

void loop() {
  switchpinState = digitalRead(switchPin);

  if (switchpinState == HIGH) {
    val1 = analogRead(pot1);
    val1 = map(val1, 0, 1023, 0, 255);
  
    analogWrite(motor1Pin, val1);

    Serial.print(val1);
    Serial.print(" Motor 1: ");

    val2 = analogRead(pot2);
    val2 = map(val2, 0, 1023, 0, 255);

    analogWrite(motor2Pin, val2);

    Serial.print(val2);
    Serial.print(" Motor 2: ");

    val3 = analogRead(pot3);
    val3 = map(val3, 0, 1023, 0, 255);

    analogWrite(motor3Pin, val3);

    Serial.println(val3);
    Serial.print(" Motor 3: ");
  }
  else {
    staticDisplay();
  }
}

void staticDisplay() {
  int frame = 1;
  boolean exitStaticDisplay = false;

  while (true) {
    switch (frame) {
      case 1:
        analogWrite(motor1Pin, 100);
        exitStaticDisplay = myDelay(2000);
        break;
      case 2:
        analogWrite(motor1Pin, 124);
        exitStaticDisplay = myDelay(2000);
        break;
      case 3:
        analogWrite(motor2Pin, 100);
        analogWrite(motor2Pin, 200);
        analogWrite(motor3Pin, 155);
        exitStaticDisplay = myDelay(2000);
        break;
      case 4:
        analogWrite(motor1Pin, 200);
        analogWrite(motor3Pin, 0);
        analogWrite(motor2Pin, 167);
        exitStaticDisplay = myDelay(2000);
        break;
      case 5:
        analogWrite(motor3Pin, 0);
        analogWrite(motor2Pin, 130);
        analogWrite(motor1Pin, 180);
        exitStaticDisplay = myDelay(2000);
        break;
      case 6:
        analogWrite(motor3Pin, 109);
        analogWrite(motor1Pin, 190);
        analogWrite(motor2Pin, 205);
        exitStaticDisplay = myDelay(2000);
        break;
      case 7:
        analogWrite(motor1Pin, 115);
        exitStaticDisplay = myDelay(100);
        break;
      case 8:
        analogWrite(motor3Pin, 160);
        analogWrite(motor1Pin, 0);
        analogWrite(motor3Pin, 135);
        exitStaticDisplay = myDelay(2000);
        break;
      case 9:
        analogWrite(motor3Pin, 140);
        analogWrite(motor2Pin, 200);
        analogWrite(motor1Pin, 190);
        exitStaticDisplay = myDelay(2000);
        break;
      case 10:
        analogWrite(motor1Pin, 100);
        analogWrite(motor2Pin, 0);
        analogWrite(motor3Pin, 0);
        exitStaticDisplay = myDelay(100);
        break;
      case 11:
        analogWrite(motor2Pin, 152);
        analogWrite(motor1Pin, 0);
        analogWrite(motor3Pin, 115);
        exitStaticDisplay = myDelay(100);
        break;
      case 12:
        analogWrite(motor1Pin, 199);
        analogWrite(motor2Pin, 140);
        analogWrite(motor3Pin, 178);
        exitStaticDisplay = myDelay(2000);
        break;
      case 13:
        analogWrite(motor3Pin, 100);
        analogWrite(motor1Pin, 0);
        analogWrite(motor2Pin, 0);
        exitStaticDisplay = myDelay(100);
        break;
      case 14:
        analogWrite(motor1Pin, 240);
        analogWrite(motor2Pin, 200);
        analogWrite(motor3Pin, 120);
        exitStaticDisplay = myDelay(2000);
        break;
      case 15:
        analogWrite(motor1Pin, 170);
        exitStaticDisplay = myDelay(100);
        break;
      case 16:
        analogWrite(motor2Pin, 200);
        analogWrite(motor3Pin, 130);
        exitStaticDisplay = myDelay(2000);
        break;
      case 17:
        analogWrite(motor1Pin, 0);
        analogWrite(motor2Pin, 0);
        analogWrite(motor3Pin, 0);
        exitStaticDisplay = myDelay(100);
        break;
      case 18:
        analogWrite(motor3Pin, 105);
        analogWrite(motor2Pin, 105);
        analogWrite(motor1Pin, 235);
        exitStaticDisplay = myDelay(2000);
      default:
        frame = 0;
        break;
    }

    if (exitStaticDisplay == true) break; // exit from loop

    frame++;
  }
}

boolean myDelay(int delay) {
  int count = 0;
  boolean retval = false;

  while (!(retval = digitalRead(switchPin)) && !(count == delay)) {
    count++;
    delay(1);
  }

  return retval;
}

Hope this helps this time...?

:D

I'll c when I get home.

it works but theres an error so I toook out delay at the end... motors spin full blast and no change in speed when switch is pressed

edit: so I changed this part to this :

while (!(retval = digitalRead(switchPin)) && !(count == delay)) {
    count++;
delayMicroseconds(10);
  }

  return retval;
}

Same result... hmmm

whats happpening is when button is pressed the pwm stays the same at each motor and doesnt delay each time. whenever the button is pressed again the manual control of the pots work.

chris:

I think you have enough code in front of you to understand what I was trying to do; since I am not in front of my workstation with my Arduino stuff, nor do I have the circuit in front of me, I think you can understand that my position of debugging the code is going to be difficult at best.

I am going to explain what I was trying to blindly do with the code, so that you can potentially spot where the code is wrong and giving you the problems you are seeing.

  1. In loop(), you are checking the state of a pin a button is connected to; whether it is HIGH (on/pressed, I presume) or LOW (off/not-pressed). When it is pressed, you read and alter the motor speeds based on the potentiometers to change the pattern; when you release the button, you want to show a series of pre-defined and timed patterns in sequence. So, when the switch is pressed, the code reads the pots and changes the speed (this part is the same as your old code), otherwise the code calls the "staticDisplay()" function.

  2. In the staticDisplay() function, I set an exit condition flag and a frame counter, then enter an "endless" loop (while() always evaluating TRUE), and for each frame number, I write the values for the motor speeds for each display you had before (a total of 18 different displays), then I call the "myDelay()" function with a time in milliseconds (roughly) - and I set the exit condition flag (TRUE/FALSE) based on the result from that call.

  3. In the function "myDelay()", I loop until my count is equal to the delay value I passed to the function (the delay time) or until the the switch is pressed (where I exit immediately). If the switch is pressed, it will have a HIGH value (should be same as TRUE), or if the delay time has passed (without the button pressed) a LOW value (which should be same as FALSE). So out of that function, a TRUE or FALSE value is returned.

  4. Back in the staticDisplay() function, I set the exit display flag based on the return value from myDelay() - TRUE or FALSE; if TRUE (button pressed), I break out of the "endless" while() loop, which should dump me back to the loop() function, and allow you to control the speeds again with the pots immediately (at least, that's the idea and logic, I think).

  5. Otherwise, the frame counter is incremented and the next static display is shown. If the frame counter goes "out-of-bounds" by incrementing to 19, then the default condition of the switch-case structure is taken, frame is reset to 0 (then immediately incremented back to 1 for the first frame). It should never hit this condition, though, because frame 18 is like a "shutdown" frame, and it drops thru the default condition case, so it should never get to 19 - but I do that just in case, because you can't be too paranoid!

Obviously - there is something somewhere in the code that is wrong, but once again, diagnosing this remotely without the hardware in front of me, and coding blind, is something that could take a while to come to a conclusion.

I think with my description above, plus the code, plus the hardware in front of you, you should be able to get this figured out. Good luck with the project, and I can't wait to see where it ends up (obviously, you're going to allow for storage and pattern playback, right?)...

:)

then break out of the case statement,

I'll keep trying (never give up ;) ) Edit: Tried some differents ways havent had any luck yet...

If any1 feals free jump in...