Skip to content

Instantly share code, notes, and snippets.

@hhayley
Created October 2, 2016 16:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hhayley/552a759cb1ef8ed9acf89d737d6f2315 to your computer and use it in GitHub Desktop.
Save hhayley/552a759cb1ef8ed9acf89d737d6f2315 to your computer and use it in GitHub Desktop.
const int transistorPin = 9;
int redSwitch = 3;
int switchValue = 0;
int poten = A0;
void setup() {
Serial.begin(9600);
pinMode(transistorPin, OUTPUT);
pinMode(redSwitch, INPUT);
pinMode(poten, INPUT);
}
void loop() {
switchValue = digitalRead(redSwitch);
if (switchValue == 1) {
int sensorValue = analogRead(A0);
// map the sensor value to a range from 0 - 255:
int outputValue = map(sensorValue, 0, 1023, 0, 255);
analogWrite(transistorPin, outputValue);
} else {
analogWrite(transistorPin, 0);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment