Communication between LEDs

So, i am trying to figure out how to make two LEDs communicate with each other. More specifically, when one LED(1) is turned on, the other LED(2) will turn on, and when the LED(1) shuts off, the other LED(2) will also turn off.


//BY: RICARDO DE LEMOS 1/17/2007
int led1 = 13;
int cath = 2; // negative
int ando = 3;  // positive

void setup()
  pinMode(led1, OUTPUT);
  pinMode(cath, OUTPUT);
  pinMode(ando, OUTPUT);

void loop()
  digitalWrite(cath, HIGH);
  digitalWrite(ando, LOW);
  pinMode(ando, INPUT);
  if (digitalRead(ando) == LOW)
  pinMode(ando, OUTPUT);

And the translated version for Processing.



import processing.serial.*;

import cc.arduino.*;

Arduino arduino;


int turnOnLED = 13;

int sensorP = 3;

int sensorN = 2;


void setup(){

 size (500,500);


 arduino = new Arduino(this, Arduino.list()[0], 9600);

 arduino.pinMode(turnOnLED, Arduino.OUTPUT);

 arduino.pinMode(sensorP, Arduino.OUTPUT);

 arduino.pinMode(sensorN, Arduino.OUTPUT);



void draw(){
 //charging LED
 arduino.pinMode(sensorP, Arduino.OUTPUT);
 arduino.digitalWrite(sensorN, Arduino.LOW);
 arduino.digitalWrite(sensorP, Arduino.HIGH);

 arduino.pinMode(sensorP, Arduino.INPUT);
 if(arduino.digitalRead(sensorP) == Arduino.LOW){ 

does this code look right?

also, is there any specific way of actually building this? Any links or suggestions would be very helpful

yes i did look at this, but i was hoping for some other form of references...

i think there is an issue with the code....

i'm not exactly sure what

It may be that the "digitalWrite" and "pinMode" routines are taking too long, so you're missing the conditions you're looking for - you may have to resort to direct port manipulation.

look what I did:-

how can i approach this?

I am tempted to just fix up the digital write somehow so that when the one LED on pin 13 is on and the other LED detects it, it will go on. I wish i was more code-savvy

Do you actually need to use the LED as a sensor and emitter. If not then use a normal photo sensor as LEDs while they will work are not at their most reliable doing this.

for this experiment I’m trying, yes i do want to use LEDs…I just want to see the many things i can do with this…

but anyways, in the code, i see there is Cathode and Anode, which i assume mean the anode and cathode of an LED? would it be possible if i manipulated those to do what i want?

i found something on youtube that reflects about the same idea of what i would like to do:

would it be possible if i manipulated those to do what i want?

Yes that is what that page was showing you.


so i think ill take this one step at a time:

my first goal would be to have the LED turn on whenever an accelerometer on the board is moved to a certain threshold.

i already made the code for it which i have here:

    Read the Memsic 2125 two-axis accelerometer.  Converts the
    pulses output by the 2125 into milli-g's (1/1000 of earth's
    gravity) and prints them over the serial connection to the
    The circuit:
       * X output of accelerometer to digital pin 2
       * Y output of accelerometer to digital pin 3
       * +V of accelerometer to +5V
       * GND of accelerometer to ground
    created 6 Nov 2008
    by David A. Mellis
    modified 30 Jun 2009
    by Tom Igoe
 // these constants won't change:
 const int xPin = 2;            // X output of the accelerometer
 const int yPin = 3;            // Y output of the accelerometer
 int LEDpin = 13;   //LED connected to digital pin 13
 int thresholdx= 100;
 int thresholdy= 100;
 // variables to read the pulse widths:
   int pulseX, pulseY;
   // variables to contain the resulting accelerations
   int accelerationX, accelerationY;
 void setup() {
   // initialize serial communications:
   // initialize the pins connected to the accelerometer
   // as inputs:
   pinMode(xPin, INPUT);
   pinMode(yPin, INPUT);
   pinMode(LEDpin, OUTPUT);
   // variables to read the pulse widths:
   int pulseX, pulseY;
   // variables to contain the resulting accelerations
   int accelerationX, accelerationY;
   // read pulse from x- and y-axes:
 void loop() {
   // read pulse from x- and y-axes:
   pulseX = pulseIn(xPin,HIGH);  
   pulseY = pulseIn(yPin,HIGH);
   // convert the pulse width into acceleration
   // accelerationX and accelerationY are in milli-g's: 
   // earth's gravity is 1000 milli-g's, or 1g.
   accelerationX = ((pulseX / 10) - 500) * 8;
   accelerationY = ((pulseY / 10) - 500) * 8;
   if (accelerationX >thresholdx) 
   digitalWrite(LEDpin, HIGH);
   else digitalWrite(LEDpin, LOW);
   if (accelerationY > thresholdy)
   digitalWrite(LEDpin, HIGH);
   else digitalWrite(LEDpin, LOW);
   // print the acceleration
   // print a tab character:

hopefully this is correct

hopefully this is correct

You tell us, you have the hardware, does it work as expected?

alright an update

the code is correct! Whenever i turn the circuit board containing the accelerometer sideways, the LED lights up!