#include <Wire.h>
//#include "I2Cdev.h"
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h>
#endif
const int DRV8830 = 0x64;
//const int DRV8830 = 0x60;
const int analogInPin = 2;
int sensorValue = 0;
#define PIN 1
#define NUMPIXELS 1
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
void setup() {
Wire.begin();
//Serial.begin(9600);
#if defined (__AVR_ATtiny85__)
if (F_CPU == 16000000) clock_prescale_set(clock_div_1);
#endif
pixels.begin();
}
//ref http://makers-with-myson.blog.so-net.ne.jp/2014-05-15
void writeMotorResister(byte vset, byte data1){
int vdata = vset << 2 | data1;
Wire.beginTransmission(DRV8830);
Wire.write(0x00);
Wire.write(vdata);
Wire.endTransmission(true);
}
void loop() {
sensorValue = analogRead(analogInPin);
//Serial.println(sensorValue);
if(sensorValue > 520 || sensorValue < 455){
//LED ON (red)
pixels.setPixelColor(0, pixels.Color(255,0,0));
pixels.show();
//Motor ON
writeMotorResister(0x20, 0x01);
}else{
//LED OFF
pixels.clear();
pixels.show();
//Motor OFF
writeMotorResister(0x00, 0x00);
}
delay(50);
}
Comments