Go Down

Topic: Graphite resistance mood light (Read 884 times)previous topic - next topic

issac131

Mar 02, 2012, 08:40 pm
The lights work by how much resistance is there and depending on how much resistance is there then certain lights turn on or off.

Code: [Select]
` int sVal = 0; int sPin = 0; int ledPin5 = 10;int ledPin4 = 9;int ledPin3 = 6;int ledPin2 = 5;int ledPin1 = 3;void setup() {  pinMode(sPin,INPUT);  pinMode(ledPin1,OUTPUT);  pinMode(ledPin2,OUTPUT);  pinMode(ledPin3,OUTPUT);  pinMode(ledPin4,OUTPUT);  pinMode(ledPin5,OUTPUT);  Serial.begin(9600); } void loop() {  sVal = analogRead(sPin);   Serial.print(sVal);  Serial.print(", ");  sVal = map(sVal, 0, 170, 0, 5);    if(sVal==1){    digitalWrite(ledPin1,HIGH);    digitalWrite(ledPin2,LOW);    digitalWrite(ledPin3,LOW);    digitalWrite(ledPin4,LOW);    digitalWrite(ledPin5,LOW);  }else if(sVal==2){    digitalWrite(ledPin1,HIGH);    digitalWrite(ledPin2,HIGH);    digitalWrite(ledPin3,LOW);    digitalWrite(ledPin4,LOW);    digitalWrite(ledPin5,LOW);  }else if(sVal==3){    digitalWrite(ledPin1,HIGH);    digitalWrite(ledPin2,HIGH);    digitalWrite(ledPin3,HIGH);    digitalWrite(ledPin4,LOW);    digitalWrite(ledPin5,LOW);  }else if(sVal==4){    digitalWrite(ledPin1,HIGH);    digitalWrite(ledPin2,HIGH);    digitalWrite(ledPin3,HIGH);    digitalWrite(ledPin4,HIGH);    digitalWrite(ledPin5,LOW);  }else if(sVal>=5){    digitalWrite(ledPin1,HIGH);    digitalWrite(ledPin2,HIGH);    digitalWrite(ledPin3,HIGH);    digitalWrite(ledPin4,HIGH);    digitalWrite(ledPin5,HIGH);  } else if(sVal < 1){    digitalWrite(ledPin1,LOW);    digitalWrite(ledPin2,LOW);    digitalWrite(ledPin3,LOW);    digitalWrite(ledPin4,LOW);    digitalWrite(ledPin5,LOW);  }   Serial.println(sVal);}`

johnwasser

#1
Mar 03, 2012, 06:31 am
This block of code:
Code: [Select]
`   if(sVal==1){    digitalWrite(ledPin1,HIGH);    digitalWrite(ledPin2,LOW);    digitalWrite(ledPin3,LOW);    digitalWrite(ledPin4,LOW);    digitalWrite(ledPin5,LOW);  }else if(sVal==2){    digitalWrite(ledPin1,HIGH);    digitalWrite(ledPin2,HIGH);    digitalWrite(ledPin3,LOW);    digitalWrite(ledPin4,LOW);    digitalWrite(ledPin5,LOW);  }else if(sVal==3){    digitalWrite(ledPin1,HIGH);    digitalWrite(ledPin2,HIGH);    digitalWrite(ledPin3,HIGH);    digitalWrite(ledPin4,LOW);    digitalWrite(ledPin5,LOW);  }else if(sVal==4){    digitalWrite(ledPin1,HIGH);    digitalWrite(ledPin2,HIGH);    digitalWrite(ledPin3,HIGH);    digitalWrite(ledPin4,HIGH);    digitalWrite(ledPin5,LOW);  }else if(sVal>=5){    digitalWrite(ledPin1,HIGH);    digitalWrite(ledPin2,HIGH);    digitalWrite(ledPin3,HIGH);    digitalWrite(ledPin4,HIGH);    digitalWrite(ledPin5,HIGH);  } else if(sVal < 1){    digitalWrite(ledPin1,LOW);    digitalWrite(ledPin2,LOW);    digitalWrite(ledPin3,LOW);    digitalWrite(ledPin4,LOW);    digitalWrite(ledPin5,LOW);  }`

can be replaced by this block of code:
Code: [Select]
`    digitalWrite(ledPin1,sVal>=1);    digitalWrite(ledPin2,sVal>=2);    digitalWrite(ledPin3,sVal>=3);    digitalWrite(ledPin4,sVal>=4);    digitalWrite(ledPin5,sVal>=5);`
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Go Up

Please enter a valid email to subscribe