Go Down

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

issac131

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

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

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy