Go Down

Topic: Proximity sensors to pwm  times 4 (Read 810 times) previous topic - next topic

jaunders



Hi guys im really new to this world and are struggling  with geting the following  to work with four LEDs and Fours sensor can anu one help



int ledPin =  9;            // LED connected to digital pin 9
int sensorPin = 2;        // value read from the sensor
int outputValue = 0;        // value output to the PWM (analog out)


void setup() {
 Serial.begin(9600);
}

void loop() {

  // read the analog input
  int analogValue = analogRead(sensorPin);

  // NOTE: you should modify following script in according with your project goals, resistors and used sensors.


  // Sensors return values from 0 until 1023 but I mapped them from 250 to 900 to get a better visual result.
  // The map() method will return us a value from 0 to 255, good PWM values to turn on my LED
  outputValue = map(analogValue, 250, 900, 0, 255);


  // Some sensors, when their value is near to zero, return a variable value between 2 and 3, creating a loop
  // that could generate bad visual effects. To avoid this issue I always turn off the LEd when sensor value is < 5
  if (outputValue<5)
    analogWrite(ledPin, 0);
   else
    analogWrite(ledPin, outputValue);



  // print the result to the serial monitor
  Serial.print(analogValue);
  Serial.print(" : ");
  Serial.print(outputValue);
  Serial.println("----");



  // wait 10 milliseconds for the analog-to-digital converter
  delay(10);
}

Groove

Code: [Select]
#define N_LEDS 4
const int ledPin [N_LEDS] =  {3, 5, 6, 9};  // LEDs connected to these digital pins
const int sensorPin [N_LEDS] = {2, 3, 4, 5};        
int outputValue = 0;        // value output to the PWM (analog out)


void setup() {
Serial.begin(9600);
}

void loop() {
 for (int i = 0; i < N_LEDS; i++ ) {
   // read the analog input
   int analogValue = analogRead(sensorPin [i]);

   int outputValue = map(analogValue, 250, 900, 0, 255);

 if (outputValue<5)
   analogWrite(ledPin[i], 0);
  else
   analogWrite(ledPin[i], outputValue);



 // print the result to the serial monitor
 Serial.print(analogValue);
 Serial.print(" : ");
 Serial.print(outputValue);
 Serial.println("----");



 // wait 10 milliseconds for the analog-to-digital converter
 delay(10);
}
Per Arduino ad Astra

Go Up