Pages: [1]   Go Down
Author Topic: help with rgb project  (Read 885 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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);
  }
}
 
Logged

Central Europe
Offline Offline
Edison Member
*
Karma: 7
Posts: 1220
Use the Source, Luke.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 610
Posts: 49033
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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:
  digitalRead(one);
  digitalRead(two);
  digitalRead(three);
What is the point of reading from a pin if you ignore the return value?

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

Central Europe
Offline Offline
Edison Member
*
Karma: 7
Posts: 1220
Use the Source, Luke.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Korman
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 610
Posts: 49033
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
if (sum = 0){
works fine.
Logged

Central Europe
Offline Offline
Edison Member
*
Karma: 7
Posts: 1220
Use the Source, Luke.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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:
if (sum == 0){


Korman
Logged

New Jersey
Offline Offline
Faraday Member
**
Karma: 67
Posts: 3674
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
if(sum=0)

That sets sum to be zero.

What you want is
Code:
if(sum==0)

and the same applies to your other if tests
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: [1]   Go Up
Jump to: