Code 4 sensors Maxsonar EZ0?

We’re using an arduino Mega and 4 Maxsonsar ultrasonic senors EZ0 to make a sound installation.

We’re a bit stuck on how to use different sensors, each influencing a different LED to blink (as a trial- it should blink faster as you come more near to the sensor). The connections we have now should be OK. We’re using the analog inputs on the arduino.

We can manage to make 1 LED blink (influenced by 1 sensor), but can’t figure out how to prepare the code to make the other LEDS blink (each sensor should influence 1 LED).

Here’s the code that we use to make 1 led blinking:

Analog Input
Demonstrates analog input by reading an analog sensor on analog pin 0 and
turning on and off a light emitting diode(LED) connected to digital pin 13.
The amount of time the LED will be on and off depends on
the value obtained by analogRead().

The circuit:

  • Potentiometer attached to analog input 0

  • center pin of the potentiometer to the analog pin

  • one side pin (either one) to ground

  • the other side pin to +5V

  • LED anode (long leg) attached to digital output 13

  • LED cathode (short leg) attached to ground

  • Note: because most Arduinos have a built-in LED attached
    to pin 13 on the board, the LED is optional.

Created by David Cuartielles
Modified 16 Jun 2009
By Tom Igoe


int sensorPin = 0; // select the input pin for the potentiometer
int ledPin = 13; // select the pin for the LED
int sensorValue = 0; // variable to store the value coming from the sensor

void setup() {
// declare the ledPin as an OUTPUT:
pinMode(ledPin, OUTPUT);

void loop() {
// read the value from the sensor:
sensorValue = analogRead(sensorPin);
// turn the ledPin on
digitalWrite(ledPin, HIGH);
// stop the program for milliseconds:
// turn the ledPin off:
digitalWrite(ledPin, LOW);
// stop the program for for milliseconds:

What do we need to change in this code to make the other LEDS blink, influenced by their sensor? (more inputs - sensors and more outputs - LEDS)

Thank you very much,

If you need four of something, the code will almost certainly look like four copies of the code for one of the somethings. In this case, the advice is always to look at arrays and "for" loops, and watch out for "delay"s. It's worth looking at the Blink-without-delay tutorial, and working on expanding it.