Super simple switch/case... not working as intended?

Hey all :slight_smile:

Super easy one, so sorry to bother anyone with it…
making a Motor controller, trying to make pins Input and output (reverse)

must’ve screwed up something simple, but cannot see :frowning:

Blink code works exactly as intended

void setup()   {                
  pinMode(1, OUTPUT);     
    pinMode(0, INPUT);     
}
void loop()                     
{
  digitalWrite(1, HIGH);   // set the LED on
  delay(3000);                  // wait for a second
  digitalWrite(1, LOW);    // set the LED off
  delay(3000);                  // wait for a second

But the identical Switch does not work the same way :frowning: confirmed working every step of the way with serial reads… but it’s not actually sending voltage

case 'w':     
      pinMode(0, OUTPUT)   ;   //switch pins to input and output
        pinMode(1, INPUT)  ;     

          digitalWrite(0, HIGH);  
      
        Serial.println("FORWARD");
      break;

what the hell! :slight_smile:

(I want to swap the Inputs/Outputs during operation)

Thanks for your time!

Better post all your code. Nothing obviously wrong with that bit.

I should point out that if you do a Serial.begin then pins 0 and 1 are taken over for doing Serial writes. Try choosing other pins.

Fuller version, still simple

void loop() {
  if (Serial.available() > 0) {
    incomingByte = Serial.read(); // read the incoming byte:
    Serial.print("I received: "); 
    Serial.println(incomingByte, BYTE);

    switch(incomingByte) {

    case 'w':      //do numbers need to be in single quotes???
      pinMode(0, OUTPUT)   ;   pinMode(2, OUTPUT); //switch pins to input and output
        pinMode(1, INPUT)  ;      pinMode(3, INPUT);
          digitalWrite(0, HIGH);  //Both motors On Forwards (Then fire the output pins)
      digitalWrite(2, HIGH);
        Serial.println("FORWARD");
      break;

switched to 2 and 3...

still nothin' : (

  pinMode(2, OUTPUT)   ;  
        pinMode(3, INPUT)  ;     
          digitalWrite(2, HIGH);

Jack777: Fuller version, still simple

void loop() {
  if (Serial.available() > 0) {
    incomingByte = Serial.read(); // read the incoming byte:
    Serial.print("I received: "); 
    Serial.println(incomingByte, BYTE);

    switch(incomingByte) {

    case 'w':      //do numbers need to be in single quotes???       pinMode(0, OUTPUT)   ;   pinMode(2, OUTPUT); //switch pins to input and output         pinMode(1, INPUT)  ;      pinMode(3, INPUT);           digitalWrite(0, HIGH);  //Both motors On Forwards (Then fire the output pins)       digitalWrite(2, HIGH);         Serial.println("FORWARD");       break;

What motors are you using? What current do they draw? Have you got a reverse biased diode across the motor connections? Probably not, as you appear to want to run the motors in both directions. If not, how are you dealing with the back EMF?

What does any of that have to do with programming?

powerwheels or drill motors, 30a max, No since this drives a MOSFET H bridge with capacitors for EMF smoothing.

I ONLY don't have the code working : (

it works on the other 4 applications I've used that copy/pasted code QQ

All your code.

Fuller version, still simple

FullER but still not FULL

Help us help you…

    case 'w':      //do numbers need to be in single quotes???

Your comment confuses me. 'w' is not a number.

making a Motor controller, trying to make pins Input and output (reverse)

Hang on a sec.... Don't you still want the pins to be outputs, just reverse the motor by reversing the polarity?- ie swap the high / low to low /high?

Edit... like in this code (snippet) which I have used before:

  pinMode(motorA_dir_pin1, OUTPUT);
  pinMode(motorA_dir_pin2, OUTPUT);

// followed by one of:

// set dir forward
        digitalWrite(motorA_dir_pin1, 1); 
        digitalWrite(motorA_dir_pin2, 0); 

//or

       // set dir reverse
        digitalWrite(motorA_dir_pin1, 0); 
        digitalWrite(motorA_dir_pin2, 1);
        Serial.println("FORWARD");

You have debugging output. How about posting it?

How to use this forum

[quote author=Nick Gammon link=topic=154700.msg1159868#msg1159868 date=1363502400]

    case 'w':      //do numbers need to be in single quotes???

Your comment confuses me. 'w' is not a number. [/quote]

It's short for wun...

lol, yes, I comment for me :stuck_out_tongue:

Don’t you still want the pins to be outputs, just reverse the motor by reversing the polarity?
yes, this was just a demo to get it working

//Program to control an H bridge, driven by an optoisolator

int incomingByte=0 ;



void setup() {
  Serial.begin(19200); 
  Serial.println("Ready");
}

void loop() {
  if (Serial.available() > 0) {
    incomingByte = Serial.read(); // read the incoming byte:
    Serial.print("I received: "); 
    Serial.println(incomingByte, BYTE);

    switch(incomingByte) {

    case 'w':    
      pinMode(2, OUTPUT)   ;   pinMode(4, OUTPUT); //switch pins to input and output
        pinMode(3, INPUT)  ;      pinMode(5, INPUT);
          digitalWrite(4, HIGH);  //Both motors On Forwards (Then fire the output pins)
      digitalWrite(2, HIGH);
        Serial.println("FORWARD");
      break;
    case 's':    
      pinMode(3, OUTPUT)    ;   pinMode(5, OUTPUT);  
        pinMode(2, INPUT)    ;     pinMode(4, INPUT);
          digitalWrite(3, HIGH);  //Both motors On Reverse
      digitalWrite(5, HIGH);  //Both motors On Reverse
              Serial.println("BACKWARD");
      break;
      
    case '0':
      for (int thisPin = 0; thisPin < 7; thisPin++) 
      {    digitalWrite(thisPin, LOW); 
       }   Serial.print("Reset Initiated"); 
   }
  }
}

debugging output?? everything prints exactly when it should, no errors & every line executes

lol, yes, I comment for me :p

Well funny as that may be, if you want help, start commenting for us....

Don't you still want the pins to be outputs, just reverse the motor by reversing the polarity? yes, this was just a demo to get it working

Which it isn't.... I edited my previous reply #9 with some code snippets.

I do that by removing all comments so my code is easier to read,

I assume this stuff is simple enough there's no point commenting it?

(It's mostly copy/paste from the basic tutorials)

I do that by removing all comments so my code is easier to read,

I’m out…

Which it isn't.... I edited my previous reply #9 with some code snippets. [/quote]

Yes, that's what I was going for... just the First use looks the same as what I have, no?

that's why I posted it without the entire code first...

[/quote] I'm out.... [/quote]

lol, I hate this board.

if I asked for a one second answer on the Blink example, I'd get 40 replies :p

"When posting code, rather than posting a complex sketch, aim to post the simplest sketch that shows the problem."

Hmm,

Zimbo's Variant worked,

which was the more elegant version I wanted to use eventually...

but that makes no sense?

LOW... doesn't mean input, and I thought I actually needed current to flow?

I use my original EXACT same code, to drive a motor driver in another robot

and why would the pinMode(1, OUTPUT); pinMode(0, INPUT); version work fine? but not the switch?

in any... "case", :)

    case 'w':      //do numbers need to be in single quotes???
      pinMode(2, OUTPUT)   ;   pinMode(4, OUTPUT); //switch pins to input and output
        pinMode(3, OUTPUT)  ;      pinMode(5, INPUT);
          digitalWrite(4, HIGH);  //Both motors On Forwards (Then fire the output pins)
          digitalWrite(3, LOW);
      digitalWrite(2, HIGH);
        Serial.println("FORWARD");
      break;

Works fine, thanks guy! :D

Jack777: lol, I hate this board.

Thanks!

Jack777: "When posting code, rather than posting a complex sketch, aim to post the simplest sketch that shows the problem."

Yes, but you didn't do that, did you? Simplest sketch means something that compiles, not just snippets of what you think is relevant.

It also asks you to post your debugging output. So when your sketch prints:

        Serial.println("FORWARD");

Did you see that, or not? Are we supposed to guess?