Advertisement
baldengineer

Rouge Map Example Cleaned Up

Jun 16th, 2016
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.19 KB | None | 0 0
  1. const int analogInPin = A0;  // Analog input pin that the potentiometer is attached to
  2. const int analogOutPin1 = 9; // Analog output pin that the LED is attached to
  3. const int analogOutPin2 = 10; // Analog output pin that the LED is attached to
  4. const int analogOutPin3 = 11; // Analog output pin that the LED is attached to
  5.  
  6. int sensorValue = 0;        // value read from the pot
  7. int outputValue1 = 0;        // value output to the PWM (analog out)
  8. int outputValue2 = 0;        // value output to the PWM (analog out)
  9. int outputValue3 = 0;        // value output to the PWM (analog out)
  10.  
  11. unsigned long previousMillis; // for slowing down serial output
  12.  
  13. void setup() {
  14.   // initialize serial communications at 9600 bps:
  15.   Serial.begin(9600);
  16. }
  17. //Start the loop
  18. void loop() {
  19.   // read the analog in value:
  20.   sensorValue = analogRead(analogInPin);
  21.  
  22.  
  23.   // reset outputValueX
  24.   outputValue1 = 0;
  25.   outputValue2 = 0;
  26.   outputValue3 = 0;
  27.  
  28.   // map it to the range of the analog out:
  29.   if (sensorValue <= 341)
  30.     outputValue1 = map(sensorValue, 0, 341, 0, 255);
  31.    
  32.   if ((sensorValue >= 342) && (sensorValue <= 683))
  33.     outputValue2 = map(sensorValue, 342, 683, 0, 255);
  34.    
  35.   if (sensorValue >= 684)
  36.   outputValue3 = map(sensorValue, 684, 1023, 0, 255);
  37.  
  38.   // Force LEDs to Turn off
  39.   if (outputValue1 < 25)
  40.     outputValue1 = 0;
  41.  
  42.   if (outputValue3 < 25)
  43.     outputValue3 = 0;
  44.  
  45.   if (outputValue3 < 25)
  46.     outputValue3 = 0;
  47.  
  48.   // update all of the analog Values
  49.   analogWrite(analogOutPin1, outputValue1);
  50.   analogWrite(analogOutPin2, outputValue2);
  51.   analogWrite(analogOutPin3, outputValue3);
  52.  
  53.  
  54.   // only serial print once per second.
  55.   unsigned long currentTime = millis();
  56.   if (currentTime - previousMillis > 1000) {
  57.     // print the results to the serial monitor:
  58.     Serial.println("\n\n");
  59.     Serial.print("sensor = ");
  60.     Serial.print(sensorValue);
  61.     Serial.print("\t o1 = ");
  62.     Serial.println(outputValue1);
  63.     Serial.print("\t o2 = ");
  64.     Serial.println(outputValue2);
  65.     Serial.print("\t o3 = ");
  66.     Serial.println(outputValue3);
  67.     // reset millis "clock"
  68.     previousMillis = currentTime;
  69.   }
  70.  
  71.   // wait before the next loop
  72.   // delay(5);
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement