programming fault in my code?

so i am a bit of a newbie with Arduino,
i wrote this code for a potmeter with a RGB.
my code works fine if i cancel out the;
if (potValue > 340 < 680)digitalWrite(GREEN,HIGH);
i can turn my pot and it will give me red and blue and the colors go off if i go into the potValue < 340 < 680 range.
but if i turn on the green high, it goes all green no matter how i turn it...
could somebody take a look at my code and say what's wrong?
ps i am pretty sure i set my Arduino op right.

int potPin = A0;
int potValue = 0;
const byte RED = 10;
const byte GREEN = 11;
const byte BLUE = 12;

void setup() {
Serial.begin(9600);
// put your setup code here, to run once:
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
pinMode(12, OUTPUT);

}

void loop() {
potValue = analogRead(potPin);

Serial.print("potValue: ");
Serial.println(potValue);

if (potValue < 340) digitalWrite(RED, HIGH);
if (potValue < 340) digitalWrite(BLUE,LOW) ;
if (potValue < 340) digitalWrite(GREEN,LOW);
if (potValue > 680) digitalWrite(BLUE,HIGH);
if (potValue > 680) digitalWrite(RED, LOW) ;
if (potValue > 680)digitalWrite(GREEN,LOW);
if (potValue > 340 < 680)digitalWrite(GREEN,HIGH); //this is where it goes wrong if i cancel this one out it works like it should, but if i put it in it goes all green, no matter how i turn th pot
if (potValue > 340 < 680) digitalWrite(RED,LOW);
if (potValue > 340 < 680)digitalWrite(BLUE,LOW);

}

Instead of

if (potValue < 340) digitalWrite(RED, HIGH);
  if (potValue < 340) digitalWrite(BLUE,LOW) ;
  if (potValue < 340) digitalWrite(GREEN,LOW);

Why not

if (potValue < 340) 
{
    digitalWrite(RED, HIGH);
    digitalWrite(BLUE,LOW) ;
    digitalWrite(GREEN,LOW);
}

?

if (potValue > 340 < 680)

Try if (potValue > 340 && potValue < 680)

This is a very common question.
The expression evaluates left to right.
"Is potValue > 340?" - the answer is true (1) or false (0)
"is 1 or 0 < 680" Yes - always.

i did what you said, and it looks a lot cleaner this way, but the code still doesn't work like how i want it...
what could i have done wrong?

but the code still doesn't work like how i want it...
what could i have done wrong?

You haven't told us how you want it.

ohw i'm sorry i wired the pot up to take values and if that value is lower than 340 i want the RGB to shine red, between 340 and 680 green and between 680 and higher blue.
also quick add, if i cancel the green out, the blue and red show but very weak.

So, post your code as it looks now.

int potPin = A0;
int potValue = 0;
const byte RED = 10;
const byte GREEN = 11;
const byte BLUE = 12;

void setup() {
Serial.begin(9600);
// put your setup code here, to run once:
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
pinMode(12, OUTPUT);

}

void loop() {
potValue = analogRead(potPin);

Serial.print("potValue: ");
Serial.println(potValue);

if (potValue < 340)
{
digitalWrite(RED, HIGH);
digitalWrite(BLUE, LOW);
digitalWrite(GREEN, LOW);
}

if (potValue > 680)
{
digitalWrite(RED,LOW);
digitalWrite(BLUE,HIGH);
digitalWrite(GREEN,LOW);
}

if (potValue > 340 < 680)
{
digitalWrite(RED,LOW);
digitalWrite(BLUE,LOW);
digitalWrite(GREEN,HIGH);
}

}

Didn't you read reply #1?

Please remember to use code tags when posting code.

i am so sorry and i am so blind, excuse me. it works fine now and it isn't dimmed too thanks a lot!!

Even after correcting the code, you have 3 mutually exclusive conditions:

  • The value is less than 340
  • The value is more than 680
  • The value is between 340 and 680

When the conditions ARE exclusive, if/else if/else is a better construct.

You could also 'map' the pot range into three equal blocks.
And then use 'switch case' to control the LEDs.
(untested)
Leo..

const byte redPin = 10;
const byte greenPin = 11;
const byte bluePin = 12;
const byte potPin = A0;
int potValue;

void setup() {
  Serial.begin(9600);
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);
}

void loop() {
  potValue = analogRead(potPin);
  Serial.print("potValue: ");
  Serial.println(potValue);
  potValue = map(potValue, 0, 1024, 0, 3);
  switch (potValue) {
    case 0:
      digitalWrite(redPin, HIGH);
      digitalWrite(greenPin, LOW);
      digitalWrite(bluePin, LOW);
      break;
    case 1:
      digitalWrite(redPin, LOW);
      digitalWrite(greenPin, LOW);
      digitalWrite(bluePin, HIGH);
      break;
    case 2:
      digitalWrite(redPin, LOW);
      digitalWrite(greenPin, HIGH);
      digitalWrite(bluePin, LOW);
      break;
  }
}