MotorShield switch case won't change directions

Adafruit Ethernet, Motor, Proto, Wave, Datalogger, GPS Shields - etc!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
Jack777
 
Posts: 11
Joined: Fri Jun 26, 2009 11:38 pm

MotorShield switch case won't change directions

Post by Jack777 »

Code: Select all

if ( Serial.available()) {
    char Incoming = Serial.read();  //read incoming byte

    switch(Incoming) {
      case('w'):case('W'): motor1.run(FORWARD);motor2.run(FORWARD); 
      case('a'):case('A'): motor1.run(FORWARD);motor2.run(BACKWARD);  //and for some reason the motors never turn off
      case('s'):case('S'): motor1.run(BACKWARD);motor2.run(FORWARD);
      case('d'):case('D'): motor1.run(BACKWARD);motor2.run(BACKWARD);
The Motors never switch direction from the initial command, what am I doing wrong? :(

I've tried making the case RELEASE each motor before giving them the new command

User avatar
adafruit_support_bill
 
Posts: 88086
Joined: Sat Feb 07, 2009 10:11 am

Re: MotorShield switch case won't change directions

Post by adafruit_support_bill »

You need a 'break;' at the end of each case.

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

Jack777
 
Posts: 11
Joined: Fri Jun 26, 2009 11:38 pm

Re: MotorShield switch case won't change directions

Post by Jack777 »

Ugh... can't believe I forgot that :(

Thanks a huge amount :)... and hey, while I've got ya....

any idea how to make it stop moving when I release the key? (Processing?)

I thought this would work, and it does with a graphic in processing...
But the serial line would never STOP seeing the initial Char, any way to cancel it out?
(Release motors when keyReleased? Stutters when there's still an active keypressed?)


This part's complicated, I understand if you're done, helped a huge amount thanks!

Code: Select all

switch(COMPOSITOR) {                 //most important part, this is for sending composite movements

    case 1: port.write("w"); break;    //COMPOSITOR = forward=1, right=2, back=4, left=8;
    case 2: port.write("d"); break;
    case 4: port.write("s"); break;
    case 8: port.write("a"); break;
 }}                 

void keyPressed(){
  OUTPUT = key; 
  switch(key) {
    case('w'):case('W'):   b =0; COMPOSITOR |= 1 ;break; 
    case('d'):case('D'):   f =0; COMPOSITOR |= 2 ;break; //add(OR) COMPOSITOR if not that # already
    case('s'):case('S'):   e =0; COMPOSITOR |= 4 ;break;
    case('a'):case('A'):   d =0; COMPOSITOR |= 8 ;break;  // |=  (is OR statement, bitwise)
  }}
  void keyReleased(){       //abcdef are the key overlay masks for making the key "light up" when pressed
  switch(key) {
    case('w'):case('W'):   b =150; COMPOSITOR ^= 1 ;break; // ^=  (is XOR statement, bitwise)
    case('d'):case('D'):   f =150; COMPOSITOR ^= 2 ;break;
    case('s'):case('S'):   e =150; COMPOSITOR ^= 4 ;break;
    case('a'):case('A'):   d =150; COMPOSITOR ^= 8 ;break;
  }}

User avatar
adafruit_support_bill
 
Posts: 88086
Joined: Sat Feb 07, 2009 10:11 am

Re: MotorShield switch case won't change directions

Post by adafruit_support_bill »

But the serial line would never STOP seeing the initial Char, any way to cancel it out?
Are you sending a specific character to stop, or just relying on the lack of characters?

Jack777
 
Posts: 11
Joined: Fri Jun 26, 2009 11:38 pm

Re: MotorShield switch case won't change directions

Post by Jack777 »

Oh... now i feel kinda retarded...

I was relying on the lack of characters... which in this case would have it follow the previous command :(

what I was trying to do was make composite motion... so if you held W and A, it would move diagonally Left (Speed change one motor)

I think I know how to do what I want now... feel free to add any other input though ;p


thanks :)
-J

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino Shields from Adafruit”