Two potentiometers and relay working problem

Hi everyone, I'm new at arduino
I'm trying to activate two relay using two potentiometers. While first potentiometer value bigger than the second one, first relay must be activate. While second potentiomer value bigger than the first one, the second relay must be activate. If the first potentiometer is equal the second potentiomer, relays must be stop.

But I couldn't this thing. I'm getting this error code ''too few arguments to function 'void digitalWrite(uint8_t, uint8_t)' '' Could you help me please?

Thanks for assistance.

I add fritzing sketch and codes.

int potB = 0;
int potR = 0 ;
int potBRIDGE = A1;
int potRUDDER = A2;
int relaySTBD = 5; 
int relayPORT = 6; 

void setup(){
 pinMode(potBRIDGE, INPUT); 
 pinMode(potRUDDER, INPUT); 
 pinMode(relaySTBD, OUTPUT); 
 pinMode(relayPORT, OUTPUT); 

}

void loop(){

 potB=analogRead(potBRIDGE); 
 potB= map(potBRIDGE,0,1023,0,90);
  
 potR=analogRead(potRUDDER); 
 potR= map(potRUDDER,0,1023,0,90);

 if (potR>potB){
  digitalWrite(relayPORT == LOW);
  digitalWrite(relaySTBD == HIGH);
 }

 if (potR<potB) {
  digitalWrite(relayPORT == LOW);
  digitalWrite(relaySTBD == HIGH);
 }[/b]
 
 else }
  digitalWrite(relayPORT == HIGH);
  digitalWrite(relaySTBD == HIGH); 
  }

}

Moderator edit:
</mark> <mark>[code]</mark> <mark>

</mark> <mark>[/code]</mark> <mark>
tags added.

These lines:

digitalWrite(relayPORT == LOW);

.... should look like this:

digitalWrite(relayPORT, LOW);

Yes, ı did this but anyway ı didn't get any reaction of relay

For a start you have the same combination of high/low for both of the < and > cases (assuming you fixed the == vs , thing). Presumably one should be low/high and one high/low?

And your brace in the else is back to front: should be { not }

Oh, and in the maps you are using the pin number as your source not the reading.

potB= map(potBRIDGE,0,1023,0,90);

Should be:

potB= map(potB,0,1023,0,90);

Below code works for me with LEDs since I have no relays. Not Serial prints to help debugging.

int potB = 0;
int potR = 0 ;
int potBRIDGE = A1;
int potRUDDER = A2;
int relaySTBD = 5;
int relayPORT = 6;

void setup()
{
  Serial.begin(9600);
  pinMode(potBRIDGE, INPUT);
  pinMode(potRUDDER, INPUT);
  pinMode(relaySTBD, OUTPUT);
  pinMode(relayPORT, OUTPUT);

}

void loop()
{

  potB = analogRead(potBRIDGE);
  potB = map(potB, 0, 1023, 0, 90);

  potR = analogRead(potRUDDER);
  potR = map(potR, 0, 1023, 0, 90);
  Serial.print(potB);
  Serial.print(" ");
  Serial.println(potR);

  if (potR > potB)
  {
    digitalWrite(relayPORT, LOW);
    digitalWrite(relaySTBD, HIGH);
  }

  else if (potR < potB)
  {
    digitalWrite(relayPORT, HIGH);
    digitalWrite(relaySTBD, LOW);
  }

  else
  {
    digitalWrite(relayPORT, HIGH);
    digitalWrite(relaySTBD, HIGH);
  }

}

I worked your codes but i think the program entered the endless loop. It doesn't work porperly. Thanks for help

It's your code with some mistakes fixed and serial prints included.

And of course it enters an endless loop: that's what loop() is.

How can ı fix this problem or what should be the codes like....? Could u help me?

I don't know what the problem is: your code with a few fixes, works for me with LEDs.

Thanks for your help, ı checked the circuit and ı connected wrongly the potentiometer negative point. Now ıt's working so beatiful :smiley: :smiley:

// hello I have found your mistakes and edited your code. this should work perfectly. Enjoy

int potB ;
int potR ;
int potBRIDGE = A1;
int potRUDDER = A2;
int relaySTBD = 5;
int relayPORT = 6;

void setup(){
pinMode(potBRIDGE, INPUT);
pinMode(potRUDDER, INPUT);
pinMode(relaySTBD, OUTPUT);
pinMode(relayPORT, OUTPUT);
Serial.begin(9600);
}

void loop(){

potB=analogRead(potBRIDGE);
potB= map(potBRIDGE,0,1023,0,90);

potR=analogRead(potRUDDER);
potR= map(potRUDDER,0,1023,0,90);

if (potR>potB)
{
digitalWrite(relayPORT, LOW);
digitalWrite(relaySTBD, HIGH);
}

if (potR<potB)
{
digitalWrite(relayPORT, LOW);
digitalWrite(relaySTBD, HIGH);
}

else }
digitalWrite(relayPORT, LOW);
digitalWrite(relaySTBD, LOW);
}

}

@kjp_Durgesh: A few things:

  1. Code tags (lack of).
  2. This else }
  3. Why are pin numbers int variables?
  4. Why do the variable potR and potB have global scope?
  5. Pins used by analogRead don't need to have their pinMode set.
potB=analogRead(potBRIDGE);
potB= map(potBRIDGE,0,1023,0,90);
 
potR=analogRead(potRUDDER);
potR= map(potRUDDER,0,1023,0,90);

Why do you overwrite the values by a mapping of an analog pin number?

I would write:

potR = map(analogRead(potRUDDER), 0, 1023, 0, 90;

The complete sketch I gave in #4 ran perfectly for me, although I just have LEDs on the outputs not relays. It also worked for OP who found a wiring error so from #9 onwards, all is good.

kjp_Durgesh just clouded the issue by re-hashing stuff I pointed out to OP and fixed in #3 and #4. (And which you say you fixed in #10, but left errors.) Not sure why you even posted a "fix" in the post immediately after the one where the OP already said it was working. Because of that, posts #11, #12 and #13 are redundant since they relate to kjp_Durgesh's duff code not the OP's code which was fixed by #4 and "approved by the client" in #9.

@hsnhsynarslan maybe it would help if you edited the title in the first post and added [answered] or similar.

Hi everyone, thanks for attention. I fix the problem with @ard_guy suggestions. I had another problem witth potentiometer, because ı forget the pulldown in the potentiometers gnd side. Now everythings is okey but ı have still sensivity problem on the system. Could you suggest me anything possible in this issue?

hsnhsynarslan:
Now everythings is okey but ı have still sensivity problem on the system. Could you suggest me anything possible in this issue?

Well, you are mapping 1024 values to 90 values. That will reduce sensitivity. If all you care about is that the pots have the same value, then why map them down to 0-90? 90 is not a factor of 1024, so doing this will also introduce aliasing: "jagged edges" in the mapping.

If the problem is that the system is hunting when the pots are nearly at the same value, then you need to introduce a little tolerance and/or hysteresis. That is, if the statem is in once state, the pots have to be a little further along to shift to state B.

// tolerance is how close to zero the difference is to count as being zero
// tolerance 5 means that a difference of -9 to +9 counts as zero
const int TOLERANCE = 5;

// hysteresis is how "sticky" the system is. To change state, the difference
// has to exceed this value in the other direction
const int HYSTERESIS = 3;

hysteresis = 0;


void loop() {

 int potB=analogRead(potBRIDGE);
 int potR=analogRead(potRUDDER);

 if (potR - potB + hysteresis >= TOLERANCE ) 
 {
  digitalWrite(relayPORT, LOW);
  digitalWrite(relaySTBD, HIGH);
  hysteresis = HYSTERESIS; // hysteresis
 } 
 else if (potR - potB + hysteresis<= - TOLERANCE)
 {
  digitalWrite(relayPORT, LOW);
  digitalWrite(relaySTBD, HIGH);
  hysteresis = -HYSTERESIS; // hysteresis
 }  
else 
 {
  digitalWrite(relayPORT, LOW);
  digitalWrite(relaySTBD, LOW);
  hysteresis = 0;
 }
}