Skip to content

Instantly share code, notes, and snippets.

@Craigson
Created November 26, 2014 04:10
Show Gist options
  • Select an option

  • Save Craigson/ab5bd1967829b4153126 to your computer and use it in GitHub Desktop.

Select an option

Save Craigson/ab5bd1967829b4153126 to your computer and use it in GitHub Desktop.
accelerometer code
#include <Average.h>
int sensorValueX;
int sensorValueY;
#define count 10
int xArray[count];
int yArray[count];
int xValue;
int yValue;
const int motorPin = 3;
void setup(){
Serial.begin(9600);
pinMode(motorPin, OUTPUT);
}
void loop(){
for (int i = 0; i < count; i++){
xArray[i] = analogRead(A0);
delay(1);
}
sensorValueX = mean(xArray, count);
delay(1);
for (int i = 0; i < count; i++){
yArray[i] = analogRead(A1);
delay(1);
}
sensorValueY = mean(yArray, count);
//Serial.println(sensorValue);
if (sensorValueY < 319){
yValue = -2;
digitalWrite(motorPin,HIGH);
}else {
digitalWrite(motorPin, LOW);
}
if (sensorValueY > 319 && sensorValueY < 329){
yValue = -1;
}
if (sensorValueY > 329 && sensorValueY < 340){
yValue = 0;
}
if (sensorValueY > 340 && sensorValueY < 350){
yValue = 1;
}
if (sensorValueY > 360){
yValue = 2;
digitalWrite(motorPin,HIGH);
}else {
digitalWrite(motorPin, LOW);
}
if (sensorValueX < 319){
xValue = -2;
digitalWrite(motorPin,HIGH);
}else {
digitalWrite(motorPin, LOW);
}
if (sensorValueX > 319 && sensorValueX < 329){
xValue = -1;
}
if (sensorValueX > 329 && sensorValueX < 340){
xValue = 0;
}
if (sensorValueX > 340 && sensorValueX < 350){
xValue = 1;
}
if (sensorValueX > 350){
xValue = 2;
digitalWrite(motorPin,HIGH);
} else {
digitalWrite(motorPin, LOW);
}
Serial.print("x: ");
Serial.print(xValue);
Serial.print(", ");
Serial.print("y: ");
Serial.println(yValue);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment