Skip to content

Instantly share code, notes, and snippets.

@darkwave
Created June 20, 2016 05:58
Show Gist options
  • Save darkwave/64877b9c581b9484de339d8661129164 to your computer and use it in GitHub Desktop.
Save darkwave/64877b9c581b9484de339d8661129164 to your computer and use it in GitHub Desktop.
Moisture sensor on pin A0 and RGB LED on pins 13, 11, 10 with GND on pin 12
int rPin = 13;
int gndPin = 12;
int gPin = 11;
int bPin = 10;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Serial.println("ciao");
pinMode(rPin, OUTPUT);
pinMode(gPin, OUTPUT);
pinMode(bPin, OUTPUT);
pinMode(gndPin, OUTPUT);
digitalWrite(gndPin, LOW);
digitalWrite(rPin, HIGH);
delay(1000);
digitalWrite(rPin, LOW);
digitalWrite(gPin, HIGH);
delay(1000);
digitalWrite(gPin, LOW);
digitalWrite(bPin, HIGH);
delay(1000);
digitalWrite(bPin, LOW);
}
void setColor(int r, int g, int b) {
int red = map(r, 0, 255, LOW, HIGH);
int green = map(g, 0, 255, LOW, HIGH);
int blue = map(b, 0, 255, LOW, HIGH);
digitalWrite(rPin, red);
digitalWrite(gPin, green);
digitalWrite(bPin, blue);
}
void loop() {
// put your main code here, to run repeatedly:
int val = analogRead(A0);
Serial.println(val);
if (val < 10) {
setColor(255, 0, 0);
} else if (val >= 10 && val < 500) {
setColor(0, 255, 0);
} else if (val > 500) {
setColor(0, 0, 255);
}
delay(100);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment