Pages: [1]   Go Down
Author Topic: Proximity sensors to pwm  times 4  (Read 737 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 9
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

 

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);
 }
Logged

UK
Offline Offline
Faraday Member
**
Karma: 17
Posts: 2884
Gorm deficient
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
#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);
}
Logged

Per Arduino ad Astra

Pages: [1]   Go Up
Jump to: