H bridge code, trying to install/expand potentiometer deadband.

Hello everyone, i am once again trying to get on board with programming micro controllers.
normally this goes for about a day and then i get furious with the C language (it just doesnt make sense to me) and just leave it for months, but this time i am going to try and get through a problem and start to actually use the damn things.

Below is some code i found for a h bridge controlled by a pot.
i have loaded the code and assembled a breadboard circuit with 2 LEDs as outputs and a pot as an input.
the code works very well and is smooth and seems fine, but i dont like the very very narrow dead band for the pot.

By what i can see the dead band is a val of 512 (confirmed with serial monitor).

Obviously this is too small of a value so i need to make adjustments to the other two values and also write some sort of bandpass (>xxx <xxx write val 0) or something.
any suggestions?

I have found other h bridge code but i would like to see what is involved in fitting a deadband into this one.

Copying and pasting wont help me learn how to solve problems myself, so any help would be great.

Please remember, i do not know how to code to save my life.

const int analogInPin = A0; // Analog input pin that the potentiometer is attached to
const int analogOutPin = 8; // Analog output pin that the LED is attached to

int sensorValue = 0; // value read from the pot
int outputValue = 0; // value output to the PWM (analog out)

int portaH1 = 9;
int portaH2 = 10;
int potpin = 0;
int val;
int val1;
int val2;

void setup()
{ // initialize serial communications at 9600 bps:
Serial.begin(9600);
}

void loop()
{ // read the analog in value:
sensorValue = analogRead(analogInPin);
// map it to the range of the analog out:
outputValue = map(sensorValue, 0, 1023, 0, 255);
// change the analog out value:
analogWrite(analogOutPin, outputValue);

// print the results to the Serial Monitor:
Serial.print("sensor = “);
Serial.print(sensorValue);
Serial.print(”\t output = ");
Serial.println(outputValue);

// wait 2 milliseconds before the next loop for the analog-to-digital
// converter to settle after the last reading:
delay(2);
val = analogRead(potpin);
val1 = map(val, 0, 511, 255, 0); //LH change 511 values for zero and such
val2 = map(val, 513, 1023, 0, 255); //RH change 513 values for zero and such
if (val > 512) val1 = 0; // existing deadband point, far too narrow.
else val2 = 0;
analogWrite(portaH1, val1);
analogWrite(portaH2, val2);

Which Arduino? Pin 8 is NOT a PWM pin on UNO, Nano, pro Mini, etc. with Atmega 328 processor, pins 3, 5, 6, 9, 10, 11 are. What is your code supposed to do? What is it not doing?

I didnt even notice the pin 8 mentioned in the code, i am not using that at all.

The code drives 2 digital outputs (LEDs representing FWD and REV) from an analog input (pot representing a joystick).

it does this very well and provides nice smooth ramp up and down in each direction but i cannot set a deadband for the pot/joystick neutral position.

that is what i am looking for help with.

i have been reading about ELSE and IF stuff for the past few hours and its not helping just yet.

f111:
I didnt even notice the pin 8 mentioned in the code, i am not using that at all.

The code drives 2 digital outputs (LEDs representing FWD and REV) from an analog input (pot representing a joystick).

it does this very well and provides nice smooth ramp up and down in each direction but i cannot set a deadband for the pot/joystick neutral position.

that is what i am looking for help with.

i have been reading about ELSE and IF stuff for the past few hours and its not helping just yet.

ok, deadband implies a RANGE

so, in pseudo code that would be something like this porbably:

if (val > lower_deadband && val < upper_deadband)
operate motors.
else
you in neutral position

Something like this (untested)?

Void loop()
{
  const byte deadBand = 5; // adjust for desired deadband
.
.
.
.
 delay(2);
  val = analogRead(potpin); 
  if(val < (512 - deadBand))
  {
    val2 = 0;          
    val1 = map(val, 0, 511, 255, 0);    //LH change 511 values for zero and such
  }
  else if(val > (512 + deadBand))
  {
    val1 = 0;
    val2 = map(val, 513, 1023, 0, 255); //RH change 513 values for zero and such
  }
  else val1 = val2 = 0;
  analogWrite(portaH1, val1);
  analogWrite(portaH2, val2);
}

sherzaad:
ok, deadband implies a RANGE

so, in pseudo code that would be something like this porbably:

if (val > lower_deadband && val < upper_deadband)
operate motors.
else
you in neutral position

so i attempted to use your solution but i havnt been able to write it correctly and it caused some pretty whacky outputs lol.

delay(2);
val = analogRead(potpin);
val1 = map(val, 0, 505, 255, 0); //LH change 511 values for zero and such
val2 = map(val, 520, 1023, 0, 255); //RH change 513 values for zero and such
if (val > 505 && val < 520); // change this if you get a sudden output from an opposite output.
else val2 = 0;
analogWrite(portaH1, val1);
analogWrite(portaH2, val2);

outsider:
Something like this (untested)?

Void loop()

{
  const byte deadBand = 5; // adjust for desired deadband
.
.
.
.
delay(2);
  val = analogRead(potpin);
  if(val < (512 - deadBand))
  {
    val2 = 0;         
    val1 = map(val, 0, 511, 255, 0);    //LH change 511 values for zero and such
  }
  else if(val > (512 + deadBand))
  {
    val1 = 0;
    val2 = map(val, 513, 1023, 0, 255); //RH change 513 values for zero and such
  }
  else val1 = val2 = 0;
  analogWrite(portaH1, val1);
  analogWrite(portaH2, val2);
}

ok so i stuffed that in there and it works, adjusting that deadband value just makes it as easy as pie.
it works really nicely.

I still have no idea whats going on though lol.

so now it all looks like this

const int analogInPin = A0; // Analog input pin that the potentiometer is attached to
const int analogOutPin = 8; // Analog output pin that the LED is attached to

int sensorValue = 0; // value read from the pot
int outputValue = 0; // value output to the PWM (analog out)

int portaH1 = 9;
int portaH2 = 10;
int potpin = 0;
int val;
int val1;
int val2;

void setup()
{ // initialize serial communications at 9600 bps:
Serial.begin(9600);
}

void loop()
{ // read the analog in value:
sensorValue = analogRead(analogInPin);
// map it to the range of the analog out:
outputValue = map(sensorValue, 0, 1023, 0, 255);
// change the analog out value:
analogWrite(analogOutPin, outputValue);

// print the results to the Serial Monitor:
Serial.print("sensor = “);
Serial.print(sensorValue);
Serial.print(”\t output = ");
Serial.println(outputValue);

const byte deadBand = 40; // adjust for desired deadband

delay(2);
val = analogRead(potpin);
if(val < (512 - deadBand))
{
val2 = 0;
val1 = map(val, 0, 511, 255, 0); //LH change 511 values for zero and such
}
else if(val > (512 + deadBand))
{
val1 = 0;
val2 = map(val, 513, 1023, 0, 255); //RH change 513 values for zero and such
}
else val1 = val2 = 0;
analogWrite(portaH1, val1);
analogWrite(portaH2, val2);

}

There’s a handy-dandy post at the top of every forum page titled: Read this before posting a programming question ….

Despite this rather obvious title, it appears you have not read it. Please do so now. Item #6 in that post will tell you how to properly post your code so that it’s easy for others to read it and also copy it into the Arudino IDE to work on. Also, your indenting of the code is atrocious. Please auto-format it using ‘ctrl-t’ in the IDE before posting.

Remember, you’re asking people to help you FOR FREE. Why not make doing that easier for them? While you’re at it, read Item #7 here: How to use this forum - please read.

Here’s your latest code with proper indenting and using code tags. See how better it looks?

int sensorValue = 0;        // value read from the pot
int outputValue = 0;        // value output to the PWM (analog out)

int portaH1 = 9;
int portaH2 = 10;
int potpin = 0;
int val;
int val1;
int val2;

void setup()
{ // initialize serial communications at 9600 bps:
  Serial.begin(9600);
}

void loop()
{ // read the analog in value:
  sensorValue = analogRead(analogInPin);
  // map it to the range of the analog out:
  outputValue = map(sensorValue, 0, 1023, 0, 255);
  // change the analog out value:
  analogWrite(analogOutPin, outputValue);

  // print the results to the Serial Monitor:
  Serial.print("sensor = ");
  Serial.print(sensorValue);
  Serial.print("\t output = ");
  Serial.println(outputValue);

  const byte deadBand = 40; // adjust for desired deadband

  delay(2);
  val = analogRead(potpin);
  if (val < (512 - deadBand))
  {
    val2 = 0;
    val1 = map(val, 0, 511, 255, 0);    //LH change 511 values for zero and such
  }
  else if (val > (512 + deadBand))
  {
    val1 = 0;
    val2 = map(val, 513, 1023, 0, 255); //RH change 513 values for zero and such
  }
  else val1 = val2 = 0;
  analogWrite(portaH1, val1);
  analogWrite(portaH2, val2);
}

Is this easier to “get”? :slight_smile:

Void loop()
{
  const byte deadBand = 40; // adjust for desired deadband
  const int neutral = 512; // or whatever ADC reading is with stick in 
                           // neutral position
.
.
.
.
 delay(2);
  val = analogRead(potpin); 
  if(val < (neutral - deadBand)) // if val is less than 472, do this, else,
  {
    val2 = 0;          
    val1 = map(val, 0, 511, 255, 0);    //LH change 511 values for zero and such
  }
  else if(val > neutral + deadBand) // if val is greater than 552, do this, else
  {
    val1 = 0;
    val2 = map(val, 513, 1023, 0, 255); //RH change 513 values for zero and such
  }
  else val1 = val2 = 0; // if val is between 471 and 553, set portaH1 & H2 to 0
  analogWrite(portaH1, val1);
  analogWrite(portaH2, val2);

GFVALVO I can't stand the little window to display the code, I suppose it is good if it's a particularly long example though.

What is indenting?

then i get furious with the C language (it just doesnt make sense to me) and just leave it for months

That problem is simple. You're jumping in too deep. All those really boring simple lessons at the beginning... go ahead and do them too. There's only actually a handful of things to learn and then everything is built on that. But when you try to jump ahead to the part that is actually interesting then you miss out on those fundamentals.

f111:
GFVALVO I can't stand the little window to display the code, I suppose it is good if it's a particularly long example though.

But without it you get a lot of people who won't help. It's a simple enough thing to do for people who are helping you for FREE.

f111:
What is indenting?

Ever see code written by a real coder? Note how all the blocks of code are all lined up nice and neat and indented the same. We don't just do that to make it pretty. It also makes it a LOT easier to read and understand the control structure of code. You can press Control-T in the IDE and it will do all of that for you so there really is no excuse for not doing it.

If you want to find out what a term means, why not use a search engine, its wasteful asking people simple
stuff like definitions of words / technical jargon, ask better questions and you'll get better answers! We are
not really here to be mechanical turks for Googling stuff!

f111:
Hello everyone, i am once again trying to get on board with programming micro controllers.
normally this goes for about a day and then i get furious with the C language (it just doesnt make sense to me)

Delta_G:
That problem is simple. You're jumping in too deep. All those really boring simple lessons at the beginning... go ahead and do them too. There's only actually a handful of things to learn and then everything is built on that. But when you try to jump ahead to the part that is actually interesting then you miss out on those fundamentals.

So, I'll play the part of the old (because I am) curmudgeon (because I am). This symptom is simply the result of the Arduino Philosophy: make available to Novice-Coders as much abstracted functionality as possible. When I took my first 'C' course in college (30+ years ago) there weren't 'Libraries' for running servos, Lora Comms, RGB LED strips, etc. I wrote endless programs for sorting arrays in-situ, handling user I/O, manipulating (Unix) files, etc. I could traverse a doubly-linked, dynamically-allocated binary tree in my sleep. I learned the language cold, inside and out.

Mind you, I'm all for getting as many newbies coding as possible. But, it seems to me, some are, as @Delta_G implied, simply out-driving their headlights.