2 potentiometers , two 2way switches controlling both (A&B) motor shield outputs

Hello All, Glad to be here. I'm new but I try first, then listen.
I currently started a project. Simply a POT, switch, resistor using a Arduino motor shield. The project purpose I found online was to use the shield as a DC output to control the speed (POT) and direction (switch) of a HO scale train. The OP also supplied the code which works fine.

So basically I designed a Dual control box to 3d print that I wanted to use to turn the two POTs for the
project. I wanted each pot to control an output to each (A and B) of the shield's outputs. The original code got it's outputs from pins 12 and 9. Both 12 and 9 signifies A on the shield for dir and brk. It then says that 13 and 8 should be the same for output B dir and brk. So I just copied his code and edited it to match the second set of pins for my new POT addition. The problem is that both POTs never work at the same time. If I switch the outputs in the code from 12&9 to 13&8, the only thing it does is switches the little light by the A output. The B out leds never light up. I hope I make sense.

I can post the code, but i'm about to read up on the posting rules first.

Thanks

I can post the code, but i'm about to read up on the posting rules first.

Good to hear

A circuit diagram would be helpful too

[codeint sensorval;
int Speed;
int Direction;

void setup() {
Serial.begin(9600);
pinMode(12,OUTPUT); //Initiates DC power on Motor A
pinMode(9,OUTPUT); //Intitiates Brake on Motor A
pinMode(A0,INPUT); //estabish speed from rotary knob input
pinMode(A1,INPUT); //establish direction input

//I added below 4 lines

pinMode(13,OUTPUT); //Initiates DC power on Motor B
pinMode(8,OUTPUT); //Intitiates Brake on Motor B
pinMode(A4,INPUT); //estabish speed from rotary knob input
pinMode(A5,INPUT); //establish direction input

}

void loop() {
sensorval = analogRead(A0); //reads the value from A0
Direction = analogRead(A1); //reads the value from A1

//I added below 2 lines

sensorval = analogRead(A4); //reads the value from A4
Direction = analogRead(A5); //reads the value from A5

Serial.println(sensorval);
Speed = map(sensorval,0,1023,0,255);
analogWrite(3,Speed);


if (Direction<500){
  digitalWrite(12,HIGH); //Establish Forward Direction
  digitalWrite(9,LOW); //Release the Brake

  //I added below 2 lines
  
  digitalWrite(13,HIGH); //Establish Forward Direction
  digitalWrite(8,LOW); //Release the Brake
  
}

else if (Direction>500){
  digitalWrite(12,LOW); //Establishes the reverse Direction
  digitalWrite(9,LOW); //Releases the Brake
  
  //I added below 2 lines
  
  digitalWrite(13,LOW); //Establishes the reverse Direction
  digitalWrite(8,LOW); //Releases the Brake

}
}
////////////////////////////My Code

///int sensorval;
///int Speed;
///int Direction;

///void setup() {
///Serial.begin(9600);
///pinMode(13,OUTPUT); //Initiates DC power on Motor B
///pinMode(8,OUTPUT); //Intitiates Brake on Motor B
///pinMode(A4,INPUT); //estabish speed from rotary knob input
///pinMode(A5,INPUT); //establish direction input

///}

///void loop() {
///sensorval = analogRead(A4); //reads the value from A4
///Direction = analogRead(A5); //reads the value from A5
///Serial.println(sensorval);
///Speed = map(sensorval,0,1023,0,255);
///analogWrite(3,Speed);

///if (Direction<500){
  ///digitalWrite(13,HIGH); //Establish Forward Direction
  ///digitalWrite(8,LOW); //Release the Brake
///}

///else if (Direction>500){
  ///digitalWrite(13,LOW); //Establishes the reverse Direction
  ///digitalWrite(8,LOW); //Releases the Brake
///}
///}]

slank6000:
but i'm about to read up on the posting rules first.

Didn't work so good :slight_smile:

To make it easy for people to help you please modify your post and use the code button </>
codeButton.png

so your code 
looks like this

and is easy to copy to a text editor. See How to use the Forum

Also please use the AutoFormat tool to indent your code consistently for easier reading.

...R

Okay. working on it.

Thanks

I don't understand why you are doing an analogRead() for direction. What are you using to control the direction? I would expect a 2-way switch and if so I would be using digitalRead(). You can use digitalRead() with analog pins.

You have a big chunk of commented out code and I don't know what it is intended to be - it seems to be a complete second program.

...R

Robin2:
I don't understand why you are doing an analogRead() for direction. What are you using to control the direction? I would expect a 2-way switch and if so I would be using digitalRead(). You can use digitalRead() with analog pins.

You have a big chunk of commented out code and I don't know what it is intended to be - it seems to be a complete second program.

...R

Thanks.. The original code is from the author. He did mention why he used analog over digital reads. He stated something about less errors I think. The second program is just a copy of his first that I pasted and edited to try and add another POT and switch. Thanks!!!

slank6000:
The second program is just a copy of his first that I pasted and edited to try and add another POT and switch. Thanks!!!

You can't have two functions with the same name in a program - for example 2 setup()s or 2 loop()s. You could call them setup2() and loop2() and then within setup() have a line setup2(); and within loop() have a line loop2();

It would probably be a good idea to try your own version of the code on its own to make sure things work before trying to combine them

...R

Robin2:
You can't have two functions with the same name in a program - for example 2 setup()s or 2 loop()s. You could call them setup2() and loop2() and then within setup() have a line setup2(); and within loop() have a line loop2();

It would probably be a good idea to try your own version of the code on its own to make sure things work before trying to combine them

...R

I think I follow you thanks.

This code works, but only one POT works and it only controls output A on the shield.. 
I removed my version of the code that I had commented out.   Thanks!!

int sensorval;
int Speed;
int Direction;

void setup() {
Serial.begin(9600);
pinMode(12,OUTPUT); //Initiates DC power on Motor A
pinMode(9,OUTPUT); //Intitiates Brake on Motor A
pinMode(A0,INPUT); //estabish speed from rotary knob input
pinMode(A1,INPUT); //establish direction input

//I added below 4 lines

pinMode(13,OUTPUT); //Initiates DC power on Motor B
pinMode(8,OUTPUT); //Intitiates Brake on Motor B
pinMode(A4,INPUT); //estabish speed from rotary knob input
pinMode(A5,INPUT); //establish direction input

}

void loop() {
sensorval = analogRead(A0); //reads the value from A0
Direction = analogRead(A1); //reads the value from A1

//I added below 2 lines

sensorval = analogRead(A4); //reads the value from A4
Direction = analogRead(A5); //reads the value from A5

Serial.println(sensorval);
Speed = map(sensorval,0,1023,0,255);
analogWrite(3,Speed);


if (Direction<500){
  digitalWrite(12,HIGH); //Establish Forward Direction
  digitalWrite(9,LOW); //Release the Brake

  //I added below 2 lines
  
  digitalWrite(13,HIGH); //Establish Forward Direction
  digitalWrite(8,LOW); //Release the Brake
  
}

else if (Direction>500){
  digitalWrite(12,LOW); //Establishes the reverse Direction
  digitalWrite(9,LOW); //Releases the Brake
  
  //I added below 2 lines
  
  digitalWrite(13,LOW); //Establishes the reverse Direction
  digitalWrite(8,LOW); //Releases the Brake

}
}

slank6000:
This code works, but only one POT works and it only controls Out A on the shield..
I removed my version of the code that I had commented out. Thanks!!

I'm a bit confused.

I thought what you want is to add extra functionality?

Are you able to change the posted program so it read the other pot and controls output B ?

...R

Robin2:
I'm a bit confused.

I thought what you want is to add extra functionality?

...R

Robin2:
I'm a bit confused.

I thought what you want is to add extra functionality?

...R

The lines in this code where I state //I added is the code I changed to add the second POT and switch, but to send the output to B Thanks.

His original code only used pin 12 and 9 and A0 and A1

I added 13 and 8 and A4 and A5 .

Thanks

These two new lines are using the same variable names as the earlier similar lines. They need to have different variable names

//I added below 2 lines

sensorval = analogRead(A4); //reads the value from A4
Direction = analogRead(A5); //reads the value from A5

Maybe call then sensorValB and DirectionB

And you will also need the equivalent of these lines for your second motor

Serial.println(sensorval);
Speed = map(sensorval,0,1023,0,255);
analogWrite(3,Speed);

...R

Robin2:
These two new lines are using the same variable names as the earlier similar lines. They need to have different variable names

//I added below 2 lines

sensorval = analogRead(A4); //reads the value from A4
Direction = analogRead(A5); //reads the value from A5



Maybe call then sensorValB and DirectionB


And you will also need the equivalent of these lines for your second motor


Serial.println(sensorval);
Speed = map(sensorval,0,1023,0,255);
analogWrite(3,Speed);




...R

Okay Thanks!!! I did this last night, but before I knew how to work the braces/brackets. I have a little code experience. I did sensorval2 and direction2... Let me try that again.

Also, will I need a int sensorvalb
and a int directionb

??

THANKS!!! a million. I am working on this as we speak!!! thx

This code compiles and works, but still only does one POT and no B output.

If pin 12 and 9 are for Output's A dir and brake,, then shouldn't pin 13 and 8 work the same way?
Thanks AGAIN!!!!

int sensorval;
int Speed;
int Direction;
int sensorvalB;
int DirectionB;
int SpeedB;

void setup() {
Serial.begin(9600);
pinMode(12,OUTPUT); //Initiates DC power on Motor A
pinMode(9,OUTPUT); //Intitiates Brake on Motor A
pinMode(A0,INPUT); //estabish speed from rotary knob input
pinMode(A1,INPUT); //establish direction input
}

void setupB() {
Serial.begin(9600);
pinMode(13,OUTPUT); //Initiates DC power on Motor B
pinMode(8,OUTPUT); //Intitiates Brake on Motor B
pinMode(A4,INPUT); //estabish speed from rotary knob input
pinMode(A5,INPUT); //establish direction input
}

void loop() {
sensorval = analogRead(A0); //reads the value from A0
Direction = analogRead(A1); //reads the value from A1

Serial.println(sensorval);
Speed = map(sensorval,0,1023,0,255);
analogWrite(3,Speed);
}

void loopB()  {
sensorvalB = analogRead(A4); //reads the value from A4
DirectionB = analogRead(A5); //reads the value from A5

Serial.println(sensorvalB);
SpeedB = map(sensorval,0,1023,0,255);
analogWrite(3,Speed);

if (Direction<500){
  digitalWrite(12,HIGH); //Establish Forward Direction
  digitalWrite(9,LOW); //Release the Brake
}
 
if (DirectionB<500){  
  digitalWrite(13,HIGH); //Establish Forward Direction
  digitalWrite(8,LOW); //Release the Brake
}

else if (Direction>500){
  digitalWrite(12,LOW); //Establishes the reverse Direction
  digitalWrite(9,LOW); //Releases the Brake
}  
 
if (DirectionB>500){  
  digitalWrite(13,LOW); //Establishes the reverse Direction
  digitalWrite(8,LOW); //Releases the Brake

}
}

This code compiles and works, but still only does one POT and no B output.

void loop()
{
  sensorval = analogRead(A0); //reads the value from A0
  Direction = analogRead(A1); //reads the value from A1
  Serial.println(sensorval);
  Speed = map(sensorval, 0, 1023, 0, 255);
  analogWrite(3, Speed);
}

This is your entire loop function.
None of the code relating to sensor B is ever run

UKHeliBob:

void loop()

{
  sensorval = analogRead(A0); //reads the value from A0
  Direction = analogRead(A1); //reads the value from A1
  Serial.println(sensorval);
  Speed = map(sensorval, 0, 1023, 0, 255);
  analogWrite(3, Speed);
}



This is your entire loop function.
None of the code relating to sensor B is ever run

Okay Thanks.. Let me research some more.

Maybe Closer?   Thanks

int sensorval;
int Speed;
int Direction;
int sensorvalB;
int DirectionB;
int SpeedB;

void setup() {
Serial.begin(9600);
pinMode(12,OUTPUT); //Initiates DC power on Motor A
pinMode(9,OUTPUT); //Intitiates Brake on Motor A
pinMode(A0,INPUT); //estabish speed from rotary knob input
pinMode(A1,INPUT); //establish direction input

pinMode(13,OUTPUT); //Initiates DC power on Motor B
pinMode(8,OUTPUT); //Intitiates Brake on Motor B
pinMode(A4,INPUT); //estabish speed from rotary knob input
pinMode(A5,INPUT); //establish direction input
}

void loop() {
sensorval = analogRead(A0); //reads the value from A0
Direction = analogRead(A1); //reads the value from A1
sensorvalB = analogRead(A4); //reads the value from A4
DirectionB = analogRead(A5); //reads the value from A5

Serial.println(sensorval);
Speed = map(sensorval,0,1023,0,255);
analogWrite(3,Speed);

Serial.println(sensorvalB);
SpeedB = map(sensorval,0,1023,0,255);
analogWrite(3,Speed);


if (Direction<500){
  digitalWrite(12,HIGH); //Establish Forward Direction
  digitalWrite(9,LOW); //Release the Brake

 
if (DirectionB<500){  
  digitalWrite(13,HIGH); //Establish Forward Direction
  digitalWrite(8,LOW); //Release the Brake
}

else if (Direction>500){
  digitalWrite(12,LOW); //Establishes the reverse Direction
  digitalWrite(9,LOW); //Releases the Brake

 
if (DirectionB>500){  
  digitalWrite(13,LOW); //Establishes the reverse Direction
  digitalWrite(8,LOW); //Releases the Brake

}
}
}
}

Don't do code like this

}
}
}
}

Use the AutoFormat tool to indent your blocks of code consistently. It will make it much easier for you (and us) to see how the different blocks of code relate to each other.

What happens when you run the program in Reply #17?

...R

Robin2:
Don't do code like this

}

}
}
}




Use the AutoFormat tool to indent your blocks of code consistently. It will make it much easier for you (and us) to see how the different blocks of code relate to each other.

What happens when you run the program in Reply #17?

...R

Thanks for the reply. I am back in front of it now.

The code in #17 compiles and uploads. Both POTs work in serial monitor, but only one side of output A lights up. (each output A and B has two small LEDs)

IF I switch the code by changing output pins 12 to 13,, then the other side of output A will light up.

I get no signal on output B

I'm about to learn the auto margin function.

Thanks