const int groundpin = 18; // analog input pin 4 -- ground
const int powerpin = 19; // analog input pin 5 -- voltage
const int xpin = 2; // x-axis of the accelerometer
const int ypin = 1; // y-axis
const int zpin = 0; // z-axis (only on 3-axis models)
const int blueLed = 11; // output pin for blue LED
const int redLed = 10; // output pin for red LED
const int greenLed = 9; // output pin for green LED(not needed)
int Combine;
int x;
int y;
int z;
int x1;
int y1;
int z1;
int middlex;
int middley;
int middlez;
void setup() // Arduino only reads this part of the code once at the start
{
middlex = analogRead(xpin); //taking the value of the xpin
middley = analogRead(ypin); //taking the value of the ypin
middlez = analogRead(zpin); //taking the value of the zpin
analogRead(ypin);
analogRead(zpin);
pinMode(groundpin, OUTPUT);
pinMode(powerpin, OUTPUT);
digitalWrite(groundpin, LOW);
digitalWrite(powerpin, HIGH);
//sets pins for LEDs as outputs
pinMode(blueLed, OUTPUT);
pinMode(redLed, OUTPUT);
pinMode(greenLed, OUTPUT);
}
void loop()
{
//run tilitToLight function for each axis
tiltToLight(xpin, blueLed); //til to light up blue LED
}
void tiltToLight(int readFrom, int writeTo){
// subtracting the intial values of the x, y, z axis so the values return to zero when there is no movement
x = analogRead(xpin)-middlex;
y = analogRead(ypin)-middley;
z = analogRead(zpin)-middlez;
// using the abs function so that none of the values returned are below zero(abs function turns negative values into positive
x1 = abs(x);
y1 = abs(y);
z1 = abs(z);
Combine = (x1 + y1 + z1)/3; // combining each value from the x, y, z axis and dividing by 3 to get a value for the total acceleration
if (Combine < 0) {
Combine = 0;
}
else if(Combine > 255)
{
Combine = 255;}
analogWrite(redLed, Combine); // depending on the total combine value will determine the amount of red that will appear inside the RGB LED
analogWrite(blueLed, 255- Combine); // depending on the total combine value will determine how much blue will appear inside the RGB LED
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment