Go Down

Topic: Shake with Accelerometer  (Read 793 times) previous topic - next topic

ArduinoNovice45

Mar 08, 2016, 03:16 pm Last Edit: Mar 08, 2016, 05:41 pm by ArduinoNovice45 Reason: forgot to add
Hi I'm relatively new to arduino and I was hoping someone might be able to help...
My goal is to use an accelerometer and neopixels so that every time you shake it, the neopixel changes colour.
I have basic coding knowledge but struggling to find any real solution. If anyone could give me some advice or direct me to code, that would be great!

this is what i currently have but i don't know if its worth anything, neopixels work but it isn't reacting to the accelerometer ..


#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
  #include <avr/power.h>
#endif

// Which pin on the Arduino is connected to the NeoPixels?
// On a Trinket or Gemma we suggest changing this to 1
#define PIN            2

// How many NeoPixels are attached to the Arduino?
#define NUMPIXELS      1

// When we setup the NeoPixel library, we tell it how many pixels, and which pin to use to send signals.
// Note that for older NeoPixel strips you might need to change the third parameter--see the strandtest
// example for more information on possible values.
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);

int delayval = 1000; // delay for half a second

int groundpin = 18;             // analog input pin 4
int powerpin = 19;              // analog input pin 5
int xpin = A3;                   // x-axis of the accelerometer
int ypin = A2;                   // y-axis
int zpin = A1;                   // z-axis (only on 3-axis models)
long xy_max = 470;
long xy_min = 550;

int forwardPin = 9;
int backPin = 10;
int leftPin = 11;
int rightPin = 12;
int centerPin = 8;

long x = 0;
long y = 0;


void setup()
{
 Serial.begin(9600);
 
 pinMode(forwardPin, OUTPUT);
 pinMode(backPin, OUTPUT);
 pinMode(leftPin, OUTPUT);
 pinMode(rightPin, OUTPUT);
 
 pinMode(groundpin, OUTPUT);
 pinMode(powerpin, OUTPUT);
 digitalWrite(groundpin, LOW);
 digitalWrite(powerpin, HIGH);
 #if defined (__AVR_ATtiny85__)
  if (F_CPU == 8000000) clock_prescale_set(clock_div_1);
#endif

  pixels.begin(); // This initializes the NeoPixel library.
}

void loop()
{

  for(int i=0;i<NUMPIXELS;i++){
   
 x = analogRead(xpin);
 y = analogRead(ypin);
 digitalWrite(centerPin, LOW);
 


 if (y < xy_max) { //backwards
 
     pixels.setPixelColor(i, pixels.Color(0,150,0));
    pixels.show(); // This sends the updated pixel color to the hardware.
   delay(delayval); // Delay for a period of time (in milliseconds).
   
 } else{
 
   pixels.setPixelColor(i, pixels.Color(0,0,0));
    pixels.show(); // This sends the updated pixel color to the hardware.
   delay(delayval); // Delay
   
 }
 
 
 if (y > xy_min) { //forwards

     pixels.setPixelColor(i, pixels.Color(0,150,0));
    pixels.show(); // This sends the updated pixel color to the hardware.
   delay(delayval); // Delay for a period of time (in milliseconds).
 } else {
 
   pixels.setPixelColor(i, pixels.Color(0,0,0));
    pixels.show(); // This sends the updated pixel color to the hardware.
   delay(delayval); // Delay for a period of time (in milliseconds).
 
 }
 
   if (x > xy_min) { //left

    pixels.setPixelColor(i, pixels.Color(150,0,0));
    pixels.show(); // This sends the updated pixel color to the hardware.
   delay(delayval); // Delay for a period of time (in milliseconds).
 
 } else {
   
  pixels.setPixelColor(i, pixels.Color(150,0,0));
    pixels.show(); // This sends the updated pixel color to the hardware.
   delay(delayval); // Delay for a period of time (in milliseconds).
 
 
 }
 
  if (x < xy_max) { //right

   pixels.setPixelColor(i, pixels.Color(0,0,150));
    pixels.show(); // This sends the updated pixel color to the hardware.
   delay(delayval); // Delay for a period of time (in milliseconds).
 
  }  else{
   
    pixels.setPixelColor(i, pixels.Color(0,0,0));
    pixels.show(); // This sends the updated pixel color to the hardware.
   delay(delayval); // Delay for a period of time (in milliseconds).
 

 }
 
 if (y >= xy_max && y <= xy_min && x <= xy_min && x >= xy_max)     { // nothing
   
   pixels.setPixelColor(i, pixels.Color(0,0,0));
    pixels.show(); // This sends the updated pixel color to the hardware.
   delay(delayval); // Delay for a period of time (in milliseconds).
 
}
}
}

Robin2

Write a short program to detect values from your accelerometer and display the values on the Serial Monitor.

Write a separate short program that changes the neopixel colours.

Then merge the programs.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Go Up