Analog Read & Write

Hi,
I am designing a simple voltage adder but with a twist. Here I have two potentiometers attached to two Analog inputs A0 & A1. I want to add these voltages. I took the data from A0 & A1 into two variables val & valA and then i store the addition in another variable valT. Everything works fine up till here. But the twist is.. I want valT or the output pin at which i read valT to become 0 if any one or both of the inputs are 0. I have tried to make valT 0 as well as the output pin 0. I even tried to digitalWrite it LOW but no success so far.
I may be doing a very silly mistake but thats what noobs do :stuck_out_tongue:

Here's the code:

int ledPin = 9;      // LED connected to digital pin 9
int ledPinA = 10;      // LEDA connected to digital pin 10
int ledPinT = 11;      // LEDT connected to digital pin 11
int analogPin = A0;   // potentiometer connected to analog pin 0
int analogPinA = A1;   // potentiometerA connected to analog pin 1


int val = 0;         // variable to store the read value
int valA = 0;         // variable to store the A read value
int valT = 0;         // variable to store the T calculated value

void setup()

{
Serial.begin(9600);
  pinMode(ledPin, OUTPUT);   // sets the pin as output
  pinMode(ledPinA, OUTPUT);   // sets the pin as output
  pinMode(ledPinT, OUTPUT);   // sets the pin as output 
}

void loop()

{

  val = analogRead(analogPin);   // read the input pin
  valA = analogRead(analogPinA);   // read the A input pin
    Serial.print("Val- ");    //*
  Serial.print(val);          //*
  Serial.print(" ValA- ");    //*
  Serial.print(valA);         //*-- Print values for observation
  Serial.print(" ValT- ");    //*
  Serial.print(valT);         //*
  Serial.println();           //*
  valT = val + valA;          // Add the 2 values
  
  if (val == 0)  analogWrite(ledPinT, 0 );  //*
  if (valA == 0)  analogWrite(ledPinT, 0 ); //*-- Turn output 0 if any of the inputs are 0
    
  analogWrite(ledPin, val / 4);  // analogRead values go from 0 to 1023, analogWrite values from 0 to 255
 analogWrite(ledPinA, valA / 4);  // analogRead values go from 0 to 1023, analogWrite values from 0 to 255
 analogWrite(ledPinT, valT / 8 ); //valT / 8
}

Thanks in advance!

Names like val and valA, ledPin and ledPinA, etc. do not make sense. If you are going to assign suffixes, assign them to all variables in the set. valA and valB -> valT. ledPinA and ledPinB.

  val = analogRead(analogPin);   // read the input pin
  valA = analogRead(analogPinA);   // read the A input pin
    Serial.print("Val- ");    //*
  Serial.print(val);          //*
  Serial.print(" ValA- ");    //*
  Serial.print(valA);         //*-- Print values for observation

Read some values and print them. Makes sense.

  Serial.print(" ValT- ");    //*
  Serial.print(valT);         //*
  Serial.println();           //*
  valT = val + valA;          // Add the 2 values

Print the total, and then compute the total. That does NOT make sense.

  if (val == 0)  analogWrite(ledPinT, 0 );  //*
  if (valA == 0)  analogWrite(ledPinT, 0 ); //*-- Turn output 0 if any of the inputs are 0

Are you getting exactly 0? Turning the pin off if either value is less than some small threshold makes more sense to me.

I want valT or the output pin at which i read valT to become 0 if any one or both of the inputs are 0.

"the output pin at which I read valT" does not make sense. You do not read valT. You compute valT, based on readings from two analog pins.

You have not said exactly what the code does (with the ledPinT) when val is 0, when val is not 0, when valA is 0, or when valA is not 0, so it is hard to understand what your problem is.

comments in the code

int ledPin = 9;      // LED connected to digital pin 9
int ledPinA = 10;      // LEDA connected to digital pin 10
int ledPinT = 11;      // LEDT connected to digital pin 11
int analogPin = A0;   // potentiometer connected to analog pin 0
int analogPinA = A1;   // potentiometerA connected to analog pin 1


int val = 0;         // variable to store the read value
int valA = 0;         // variable to store the A read value
int valT = 0;         // variable to store the T calculated value

void setup()

{
Serial.begin(9600);
  pinMode(ledPin, OUTPUT);   // sets the pin as output
  pinMode(ledPinA, OUTPUT);   // sets the pin as output
  pinMode(ledPinT, OUTPUT);   // sets the pin as output
}

void loop()

{

  val = analogRead(analogPin);   // read the input pin
  valA = analogRead(analogPinA);   // read the A input pin
    Serial.print("Val- ");    //*
  Serial.print(val);          //*
  Serial.print(" ValA- ");    //*
  Serial.print(valA);         //*-- Print values for observation
  Serial.print(" ValT- ");    //*

  if (val == 0 || valA == 0)  //*-- if val OR valA ==0     <<<<<<<<<<<<<
  {
   analogWrite(ledPinT, 0 ); //*-- Turn output 0 if any of the inputs are 0    
  } 
  else        // else both are not zero   <<<<<<<<<<<<<
  {
    valT = (val + valA)/2;          //*-- Add the 2 values then divide by 2      <<<<<<<<<<<
    valT = map (valT, 0, 1023, 0, 255);    //*-- this maps valT to be between 0-255   <<<<<<<<<<
    analogWrite(ledPinT, valT);
  } 
  Serial.print(valT);         //*-- print this after we know what valT is     <<<<<<<<<<<<<<
  Serial.println();           //*
}

May I offer a constructive example:

const byte potOneLed = 9;
const byte potTwoLed = 10;
const byte sumLed = 11;
const byte potOne = A0;
const byte potTwo = A1;

void setup()
{
  Serial.begin(115200);
  pinMode(potOneLed, OUTPUT);
  pinMode(potTwoLed, OUTPUT);
  pinMode(sumLed, OUTPUT);
}

void loop()
{
  int  vPot1 = analogRead(potOne);
  int  vPot2 = analogRead(potTwo);
  int  vSum = vPot1 + vPot2;
  Serial.print("Pot1 ");
  Serial.print(vPot1);
  Serial.print(" Pot2 ");
  Serial.print(vPot2);
  Serial.print(" sum ");
  Serial.print(vSum);
  Serial.println();
  analogWrite(potOneLed, vPot1 >> 2); // map
  analogWrite(potTwoLed, vPot2 >> 2); // map
  analogWrite(sumLed, vSum >> 3 ); // map sum
  delay(100);
}

Don't be so mysterious...

  analogWrite(sumLed, vSum >> 3 ); // map sum

The optimizer in the compiler recognizes division by constant powers of two, and renders them as shifts in machine code.

  analogWrite(sumLed, vSum/8); // map sum

There is nothing mysterious in shifting for me. :wink:

Hutkikz:
comments in the code

int ledPin = 9;      // LED connected to digital pin 9

int ledPinA = 10;      // LEDA connected to digital pin 10
int ledPinT = 11;      // LEDT connected to digital pin 11
int analogPin = A0;  // potentiometer connected to analog pin 0
int analogPinA = A1;  // potentiometerA connected to analog pin 1

int val = 0;        // variable to store the read value
int valA = 0;        // variable to store the A read value
int valT = 0;        // variable to store the T calculated value

void setup()

{
Serial.begin(9600);
  pinMode(ledPin, OUTPUT);  // sets the pin as output
  pinMode(ledPinA, OUTPUT);  // sets the pin as output
  pinMode(ledPinT, OUTPUT);  // sets the pin as output
}

void loop()

{

val = analogRead(analogPin);  // read the input pin
  valA = analogRead(analogPinA);  // read the A input pin
    Serial.print("Val- “);    //*
  Serial.print(val);          //*
  Serial.print(” ValA- ");    //*
  Serial.print(valA);        //– Print values for observation
  Serial.print(" ValT- ");    //

if (val == 0 || valA == 0)  //– if val OR valA ==0    <<<<<<<<<<<<<
  {
  analogWrite(ledPinT, 0 ); //
– Turn output 0 if any of the inputs are 0   
  }
  else        // else both are not zero  <<<<<<<<<<<<<
  {
    valT = (val + valA)/2;          //– Add the 2 values then divide by 2      <<<<<<<<<<<
    valT = map (valT, 0, 1023, 0, 255);    //
– this maps valT to be between 0-255  <<<<<<<<<<
    analogWrite(ledPinT, valT);
  }
  Serial.print(valT);        //– print this after we know what valT is    <<<<<<<<<<<<<<
  Serial.println();          //

}

Hutkikz, Thank you so much. It works. And i see what i did there :stuck_out_tongue:

Thank you all

Next Step towards the approach, and i am again stuck. Really weak with Programming :frowning:
I have changed the variable names & pin names so now they make some sense. When i run the below code (Full Code), output remains to be 0.0V both for D1 & D2 against both the values of I1. I tried removing the if statement as below but still the problem remains the same

 if (Master == 0 || Comp == 0)  //*-- if Master OR Comp ==0     <<<<<<<<<<<<<
  {
    analogWrite(D1, 0 ); //*-- Turn output 0 if any of the inputs are 0
    analogWrite(D2, 0 ); //*-- Turn output 0 if any of the inputs are 0
  }

Full Code —>

int D1 = 10;      // D1 connected to digital pin 10
int D2 = 11;      // D2 connected to digital pin 11
int I1 = 2;       // I1 connected to digital pin 2
int analogPinA = A0;   // potentiometerA connected to analog pin 0
int analogPinB = A1;   // potentiometerB connected to analog pin 1


int Master = 0;         // variable to store the calculated value
int Comp = 0;         // variable to store the A calculated value
int MplusC = 0;         // variable to store the added value A+B
int Masterread = 0;         // variable to store the PotA read value
int Compread = 0;         // variable to store the PotB read value

void setup()
{
  Serial.begin(9600);
  pinMode(D1, OUTPUT);   // O/P no. 1
  pinMode(D2, OUTPUT);   // O/P no. 2
  pinMode(I1, INPUT);    // I/P no. 1
}

void loop()

{
  Masterread = analogRead(analogPinA);   // read the input pin
  Compread = analogRead(analogPinB);   // read the A input pin
  Master = Masterread * 0.9;   // read the input pin
  Comp = Compread * 0.1;   // read the A input pin
  Serial.print("Master- ");    //*
  Serial.print(Master);          //*
  Serial.print(" Comp- ");    //*
  Serial.print(Comp);         //*-- Print value for observation
  Serial.print(" MplusC- ");    //*

  if (Master == 0 || Comp == 0)  //*-- if Master OR Comp ==0     <<<<<<<<<<<<<
  {
    analogWrite(D1, 0 ); //*-- Turn output 0 if any of the inputs are 0
    analogWrite(D2, 0 ); //*-- Turn output 0 if any of the inputs are 0
  }
  else        // else both are not zero   <<<<<<<<<<<<<
  {
    MplusC = (Master + Comp) / 2;        //*-- Add the 2 values then divide by 2      <<<<<<<<<<<
    MplusC = map (MplusC, 0, 1023, 0, 255);    //*-- this maps MplusC to be between 0-255   <<<<<<<<<<
    Master = map (Master, 0, 1023, 0, 255);  //*-- this maps Master to be between 0-255   <<<<<<<<<<
    Comp = map (Comp, 0, 1023, 0, 255);    //*-- this maps Comp to be between 0-255   <<<<<<<<<<
  
  if (I1 == HIGH)
  {
    analogWrite(D2, Master);
    analogWrite(D1, MplusC);
  }
  if (I1 == LOW)
  {
    analogWrite(D2, MplusC);
    analogWrite(D1, Master);
  }
  }
  Serial.print(MplusC);     //*-- print this after we know what MplusC is     <<<<<<<<<<<<<<
  Serial.println();           //*
}

Any kind of help is appreciated

Because you are using integers (whole numbers) this line

Master = Masterread * 0.9;

is probably doing

Master = Masterread * 0;

You must use float variables if you want to use decimals.

...R

Some lines from you program

int I1 = 2;       // I1 connected to digital pin 2

So, I1 (terrible choice of variable name by the way) equals 2

   if (I1 == HIGH)

When will the number 2 ever be HIGH ?

   if (I1 == LOW)

or LOW ?

Shouldn't there be a digitalRead() in there somewhere ?

Thanks for your replies UKHelibob & Robin2
I have solved the mistakes pointed by you. Now i am getting the output. One weird thing is happening though, When i keep I1 High, D2 must give only 90% of Masterread… whuch means when i turn the pot resistance minimum, the output at D2 mst be 5V * 0.9 = 4.5V. But its showing 5V! Same thing Happens when i keep I1 Low and read D1 for output. The 0.1 multiplication is working though! Any explanantion?

Here’s the modded code:

int D1 = 10;      // D1 connected to digital pin 10
int D2 = 11;      // D2 connected to digital pin 11
int I1 = 2;       // I1 connected to digital pin 2
int analogPinA = A0;   // potentiometerA connected to analog pin 0
int analogPinB = A1;   // potentiometerB connected to analog pin 1


float Master = 0;         // variable to store the calculated value
float Comp = 0;         // variable to store the A calculated value
float MplusC = 0;         // variable to store the added value A+B
float Masterread = 0;         // variable to store the PotA read value
float Compread = 0;         // variable to store the PotB read value
int I1Val = 0;

void setup()
{
  Serial.begin(9600);
  pinMode(D1, OUTPUT);   // O/P no. 1
  pinMode(D2, OUTPUT);   // O/P no. 2
  pinMode(I1, INPUT);    // I/P no. 1
}

void loop()

{
  Masterread = analogRead(analogPinA);   // read the potA pin
  Compread = analogRead(analogPinB);   // read the potB pin
  Master = Masterread * 0.9;   // calc
  Comp = Compread * 0.1;   // calc
  Serial.print("Master- ");    //*
  Serial.print(Master);          //*
  Serial.print(" Comp- ");    //*
  Serial.print(Comp);         //*-- Print values for observation
  Serial.print(" MplusC- ");    //*

  if (Master == 0 || Comp == 0)  //*-- if Master OR Comp ==0     <<<<<<<<<<<<<
  {
    analogWrite(D1, 0 ); //*-- Turn output 0 if any of the inputs are 0
    analogWrite(D2, 0 ); //*-- Turn output 0 if any of the inputs are 0
  }
  else        // else both are not zero   <<<<<<<<<<<<<
  {
    MplusC = Master + Comp;        //*-- Add the 2 values 
    MplusC = map (MplusC, 0, 1023, 0, 255);    //*-- this maps MplusC to be between 0-255   <<<<<<<<<<
    Master = map (Master, 0, 1023, 0, 255);    //*-- this maps Master to be between 0-255   <<<<<<<<<<
    Comp = map (Comp, 0, 1023, 0, 255);    //*-- this maps Comp to be between 0-255   <<<<<<<<<<
 
  I1Val = digitalRead(I1);
  
  if (I1Val == HIGH)
  {
    analogWrite(D2, Master);
    analogWrite(D1, MplusC);
  }
  if (I1Val == LOW)
  {
    analogWrite(D2, MplusC);
    analogWrite(D1, Master);
  }
  }
  Serial.print(MplusC);         //*-- print this after we know what MplusC is     <<<<<<<<<<<<<<
  Serial.println();           //*
}

I am really thankful for all of your efforts.

If that were my project I would stick with the original values (as obtained from analogRead() ) right up to the point where I want to use the value in analogWrite().

Maybe you should use the value in Masterread and only use the value in Master in your IF tests.

I don't really see any reason to use floats and fractions if all you want to do is use a modified version of the integer produced by analogRead() as a byte in analogWrite().

...R