Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const int analogInPin = A0; // Analog input pin that the potentiometer is attached to
- const int analogOutPin1 = 9; // Analog output pin that the LED is attached to
- const int analogOutPin2 = 10; // Analog output pin that the LED is attached to
- const int analogOutPin3 = 11; // Analog output pin that the LED is attached to
- int sensorValue = 0; // value read from the pot
- int outputValue1 = 0; // value output to the PWM (analog out)
- int outputValue2 = 0; // value output to the PWM (analog out)
- int outputValue3 = 0; // value output to the PWM (analog out)
- unsigned long previousMillis; // for slowing down serial output
- void setup() {
- // initialize serial communications at 9600 bps:
- Serial.begin(9600);
- }
- //Start the loop
- void loop() {
- // read the analog in value:
- sensorValue = analogRead(analogInPin);
- // reset outputValueX
- outputValue1 = 0;
- outputValue2 = 0;
- outputValue3 = 0;
- // map it to the range of the analog out:
- if (sensorValue <= 341)
- outputValue1 = map(sensorValue, 0, 341, 0, 255);
- if ((sensorValue >= 342) && (sensorValue <= 683))
- outputValue2 = map(sensorValue, 342, 683, 0, 255);
- if (sensorValue >= 684)
- outputValue3 = map(sensorValue, 684, 1023, 0, 255);
- // Force LEDs to Turn off
- if (outputValue1 < 25)
- outputValue1 = 0;
- if (outputValue3 < 25)
- outputValue3 = 0;
- if (outputValue3 < 25)
- outputValue3 = 0;
- // update all of the analog Values
- analogWrite(analogOutPin1, outputValue1);
- analogWrite(analogOutPin2, outputValue2);
- analogWrite(analogOutPin3, outputValue3);
- // only serial print once per second.
- unsigned long currentTime = millis();
- if (currentTime - previousMillis > 1000) {
- // print the results to the serial monitor:
- Serial.println("\n\n");
- Serial.print("sensor = ");
- Serial.print(sensorValue);
- Serial.print("\t o1 = ");
- Serial.println(outputValue1);
- Serial.print("\t o2 = ");
- Serial.println(outputValue2);
- Serial.print("\t o3 = ");
- Serial.println(outputValue3);
- // reset millis "clock"
- previousMillis = currentTime;
- }
- // wait before the next loop
- // delay(5);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement