Go Down

Topic: help with rgb project (Read 956 times) previous topic - next topic

kevman0044

my led on this project is not lighting how i think it should. If anyone has the time to look at it and try and help me it would be great. I am very new to this and am trying to teach myself how to do this.

Code: [Select]
int one = 3;
int two = 4;
int three = 5;
int four = 6;
int five = 7;
int six = 8;
int seven = 9;
int eight = 10;
int nine = 11;
int ten = 12;
int sum;
int redpin = 0;
int greenpin = 1;
int bluepin = 3;

void setup()
{
  Serial.begin(9600);
  pinMode(one, INPUT);
  pinMode(two, INPUT);
  pinMode(three, INPUT);
  pinMode(four, INPUT);
  pinMode(five, INPUT);
  pinMode(six, INPUT);
  pinMode(seven, INPUT);
  pinMode(eight, INPUT);
  pinMode(nine, INPUT);
  pinMode(ten, INPUT);
  pinMode(redpin, OUTPUT);
  pinMode(greenpin, OUTPUT);
  pinMode(bluepin, OUTPUT);
}

void loop()
{
  digitalRead(one);
  digitalRead(two);
  digitalRead(three);
  digitalRead(four);
  digitalRead(five);
  digitalRead(six);
  digitalRead(seven);
  digitalRead(eight);
  digitalRead(nine);
  digitalRead(ten);
 
  sum =  digitalRead(one)+digitalRead(two)+digitalRead(three)+digitalRead(four)+digitalRead(five)+digitalRead(six)+digitalRead(seven)+digitalRead(eight)+digitalRead(nine)+digitalRead(ten);
 
  Serial.println(sum, DEC);
  delay(1000);

  if (sum = 0){
    analogWrite(redpin, 0);
    analogWrite(greenpin, 0);
    analogWrite(bluepin, 255);
  }
  if (sum = 1){
    analogWrite(redpin, 0);
    analogWrite(greenpin, 125);
    analogWrite(bluepin, 125);
  }
  if (sum = 2){
    analogWrite(redpin, 0);
    analogWrite(greenpin, 255);
    analogWrite(bluepin, 0);
  }
  if (sum = 3){
    analogWrite(redpin, 173);
    analogWrite(greenpin, 255);
    analogWrite(bluepin, 47);
  }
  if (sum = 4){
    analogWrite(redpin, 255);
    analogWrite(greenpin, 255);
    analogWrite(bluepin, 0);
  }
  if (sum = 5){
    analogWrite(redpin, 255);
    analogWrite(greenpin, 204);
    analogWrite(bluepin, 0);
  }
  if (sum = 6){
    analogWrite(redpin, 255);
    analogWrite(greenpin, 153);
    analogWrite(bluepin, 0);
  }
  if (sum = 7){
    analogWrite(redpin, 255);
    analogWrite(greenpin, 102);
    analogWrite(bluepin, 0);
  }
  if (sum = 8){
    analogWrite(redpin, 255);
    analogWrite(greenpin, 51);
    analogWrite(bluepin, 0);
  }
  if (sum = 9){
    analogWrite(redpin, 255);
    analogWrite(greenpin, 0);
    analogWrite(bluepin, 0);
  }
  if (sum = 10){
    analogWrite(redpin, 255);
    analogWrite(greenpin, 255);
    analogWrite(bluepin, 255);
  }
}
 

Korman

Read the documentation about analogWrite() again. It works only with a small selection of pins, 0 and 1 aren't part of it. Don't mix it up with the pins for analogRead(), that's a completely different set. Also, don't use pins 0 and 1 for digital IO (which analogWrite() is), they're used for your serial communication via USB to your computer.

Korman

kevman0044

Thanks a lot. I disabled the serial so i could use pin one and i rearange some pins and it works great.

PaulS

Code: [Select]
int one = 3;
int two = 4;

Hmm. one is not 3. two is not 4. Use variable names that make sense. These do not.

Code: [Select]
  digitalRead(one);
  digitalRead(two);
  digitalRead(three);

What is the point of reading from a pin if you ignore the return value?

Code: [Select]
  if (sum = 0){
= is an assignment operator. == is the equality operator. Back to the reference page for you.

Korman

Paul, I assumed he has a set of buttons labelled one to ten he wants to monitor.

Korman

PaulS

Quote
I assumed he has a set of buttons labelled one to ten he wants to monitor.

pinOne, pinTwo, etc. would have removed the need to assume anything.

kevman0044

i have ten switches which i called one two three etc but they are not in pins 1 2 3 etc. Naming them one two three helped me to keep them in order.
Code: [Select]
if (sum = 0){ works fine.

Korman


Code: [Select]
if (sum = 0){ works fine.


No it doesn't work fine. You just didn't test your code properly. That statement does something different than you expect. What you meant, and what Paul was telling you is that you want:

Code: [Select]
if (sum == 0){


Korman

wildbill

It compiles and executes, sure. But as was alluded to above, it probably doesn't do what you want. You carefully read your inputs and add them together to get sum. Then you execute this:
Code: [Select]

if(sum=0)


That sets sum to be zero.

What you want is
Code: [Select]

if(sum==0)


and the same applies to your other if tests

kevman0044

All i know is that it does exactly what i want it to do.

Go Up