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