Using Accelerometer to turn off blinking LED help.

I'm trying to write code that will allow me to turn off an LED using an ADXL335. Essentially, I want to be able to press a button to begin the blinking, then tilt the accelerometer and turn off the LED. I've tried to write something, but I don't know how to "read" the values and actually uses them as a command. Here's the code I have so far.

The accelerometer reads ~333 at rest. I want it to turn the LED off at 350.

  • Using an Uno
int switchPin = 2;
int ledPin = 10;
boolean lastButton = LOW;
boolean currentButton = LOW;
boolean ledOn = false;

int groundpin = A1;             
int powerpin = A5;             
int xpin = A4;
int ground = 8;
int power = 9;

int xRead = analogRead(xpin);

void setup ()
{
  pinMode(switchPin, INPUT);
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);
  pinMode(groundpin, OUTPUT);
  pinMode(powerpin, OUTPUT);
  digitalWrite(groundpin, LOW); 
  digitalWrite(powerpin, HIGH);
}

boolean debounce(boolean last)
{
  boolean current = digitalRead(switchPin);
  if (last !=current)
  {
    delay(5);
    current = digitalRead(switchPin);
  }
  return current;
}

void loop()
{
  currentButton = debounce(lastButton);
  if ( lastButton == LOW && currentButton == HIGH)
  {
    ledOn = !ledOn;
  }
  lastButton = currentButton;
  digitalWrite(ledPin, ledOn);
  delay(400);
  digitalWrite(ledPin, LOW);
  delay(400);
  
  Serial.print(analogRead(xpin));
  Serial.println();
  delay(100);
  
  if (xRead > 350 && ledPin == HIGH)
  {
  digitalWrite(ledPin, LOW);
  }
}
  if (xRead > 350 && ledPin == HIGH)
  {
  digitalWrite(ledPin, LOW);
  }

You are using ledPin wrong and not using ledOn at all. Try this:

  if (xRead > 350)
  {
  ledOn = false;
  }

Unfortunately, that was unsuccessful. I'm thinking I may need to go in a different direction with turning the LED on and starting the blinking. Those delays are significantly slowing the program.

To get rid of the delays you can use the BlinkWithoutDelay example as a base.

Hello...Im tring to make leds blinking using an acelerometer for arduino Lilypad but i dont know how. , here is the code that I´m using.

int xpin = 3; // eixo x do acelerômetro
int ypin = 2; // eixo y
int zpin = 1; // eixo z (disponível só em modelos triaxiais)
int direita = 9;
int esquerda = 11;
void setup()
{
Serial.begin(9600);

// Fornecer terra e energia mediante o uso das entradas analógicas
// como pinos digitais comuns. Isso permite conectar a placa de
// prototipagem diretamente no Arduino. No caso de se usar os pinos
// normais GND e 5V do Arduino, pode-se remover as quatro linhas
// abaixo.
pinMode(9, OUTPUT);
pinMode(11, OUTPUT);

}

void loop()
{
Serial.print(analogRead(xpin));
Serial.print(" ");
Serial.print(analogRead(ypin));
Serial.print(" ");
Serial.print(analogRead(zpin));
Serial.println();
delay(1000);
}

if (xpin = 355 , ypin = 355 , zpin = 355)
{
digitalWrite(9, HIGH);
delay(30);
digitalWrite(9, LOW);
delay(30);
digitalWrite(11, HIGH);
delay(30);
digitalWrite(11, LOW);
delay(30);
}
else
{
digitalWrite(9, HIGH);
digitalWrite(11,HIGH);
}

Can someone help me please ?
I realy nead help please.

Ugh, here are two problems, one from 2014 and the second one from today.

For the first one, in case it worths for someone, the xRead = analogRead() should be continuously called inside the loop().

For the second problem, I noticed this line:

if (xpin = 355 , ypin = 355 , zpin = 355)

should be something like:

if (xpin == 355 || ypin == 355 || zpin == 355)

See the Reference if and Reference Boolean

Beware the program might not work as expected, if you want to test for changes in acceleration you should compare new readings with saved previous ones. An exact match with a certain value does not often happen.

thank you but when i made the alterations on the code it still didn´t work. i changed the code like you mentioned but it didn´t work. here´s the code with the chages.

int xpin = 3; // eixo x do acelerômetro
int ypin = 2; // eixo y
int zpin = 1; // eixo z (disponível só em modelos triaxiais)
int direita = 9;
int esquerda = 11;
void setup()
{
Serial.begin(9600);

// Fornecer terra e energia mediante o uso das entradas analógicas
// como pinos digitais comuns. Isso permite conectar a placa de
// prototipagem diretamente no Arduino. No caso de se usar os pinos
// normais GND e 5V do Arduino, pode-se remover as quatro linhas
// abaixo.
pinMode(9, OUTPUT);
pinMode(11, OUTPUT);

}

void loop()
{
Serial.print(analogRead(xpin));
Serial.print(" ");
Serial.print(analogRead(ypin));
Serial.print(" ");
Serial.print(analogRead(zpin));
Serial.println();
delay(1000);
}

if (xpin == 355 || ypin == 355 || zpin == 355)
{
digitalWrite(9, HIGH);
delay(30);
digitalWrite(9, LOW);
delay(30);
digitalWrite(11, HIGH);
delay(30);
digitalWrite(11, LOW);
delay(30);
}
else
{
digitalWrite(9, HIGH);
digitalWrite(11,HIGH);
}

I think im doing something wrong, could you pls point out the mistakes, ty.

Hmm, it's still a mess. xpin, ypin and zpin are fixed pin numbers, not values returned by analogRead()

you should have three integer variables to store values read by analogRead() from each pin.

...
int xpin = 3
int xaccel; 
...
void loop () { 

   ...
   /* the following line reads in xaccel variable the analog value read from xpin */
   xaccel = analogRead(xpin);
   Serial.print(xvalue); 
   if (xaccel > xlimit )
   {
        // blink or stop blink whatever else you want
   }
   ... 

}

Find some C tutorials, you need to learn basic C programming to be able to understand reference pages like this one https://www.arduino.cc/en/Reference/AnalogRead


Since arduino is a nice platform to learn programming, I wonder if there are C tutorials targeted for arduino environment.

thanks my ideia is to make the leds blink when the ecelerometer finds aceleration, and when the aceleromter dont find aceleration the leds just stays on. I think i understand what you say. But despite the code being correct acording to the program. but it dont do what i want. the led blinks all the time and it dont respond to the aceleromter data. but by the way i will see the link.......
Thank you.

did the acelerometer neads any Library ?