Created
December 16, 2015 06:12
-
-
Save exilente/682e4e0ccaadc49f56f0 to your computer and use it in GitHub Desktop.
Gunther code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Ultrasonic.h> | |
Ultrasonic disSensor1(8,7); | |
#include <VarSpeedServo.h> | |
#include <Adafruit_NeoPixel.h> | |
#ifdef __AVR__ | |
#include <avr/power.h> | |
#endif | |
#define PIN 6 | |
#define NUMPIXELS 5 | |
Adafruit_NeoPixel strip = Adafruit_NeoPixel(2, PIN, NEO_GRB + NEO_KHZ800); | |
//For Servo | |
VarSpeedServo myservo; // create servo object to control a servo | |
VarSpeedServo myservoTwo; | |
//#define trigpin 8//set trig pin | |
//#define echopin 7//set echo pin | |
int distance;//declare variable for unltrasonic sensor | |
long lastSample; | |
int sTime = 100; | |
int choice; | |
long data; | |
int decision; | |
int randVal; | |
int randSpeed; | |
int randInOut; | |
unsigned long currentMillis1; | |
unsigned long currentMillis2; | |
unsigned long currentMillis3; | |
unsigned long currentMillis4; | |
unsigned long prevMillis1 = 0; | |
unsigned long prevMillis2 = 0; | |
unsigned long prevMillis3 = 0; | |
unsigned long prevMillis4 = 0; | |
int lookMode = 2; | |
int mode = 1; | |
int idleMode = 1; | |
void setup() { | |
// put your setup code here, to run once: | |
myservo.attach(9, 544, 2400); // Movement IN and OUT | |
myservoTwo.attach(10, 544, 2400); // Movement Left, center, right | |
Serial.begin(9600); | |
#if defined (__AVR_ATtiny85__) | |
if (F_CPU == 16000000) clock_prescale_set(clock_div_1); | |
#endif | |
strip.begin(); | |
strip.show(); | |
// pinMode(trigpin, OUTPUT); | |
// pinMode(echopin, INPUT); | |
} | |
void loop() { | |
// put your main code here, to run repeatedly: | |
if(millis()-lastSample>=sTime) | |
{ | |
distance= disSensor1.Ranging(CM); | |
Serial.print("distance away: "); | |
Serial.println(distance); | |
lastSample=millis(); | |
} | |
if (mode == idleMode) { | |
randVal = random(45, 135); | |
randInOut = random(10, 100); | |
myservo.write(randInOut, 15, true); | |
myservoTwo.write(randVal, 10, true); | |
rainbow(20); | |
} | |
else (mode == lookMode); { | |
if (distance <126) { | |
choice = random (2); | |
if (choice = 2) { | |
myservo.write(120, 50, true); | |
myservoTwo.write(90, 40, true); | |
theaterChase(strip.Color(0, 150, 0), 50); | |
mode = idleMode; | |
} else { | |
myservo.write(10, 50, true); | |
myservoTwo.write(90, 40, true); | |
theaterChase(strip.Color(0, 0, 150), 50); | |
mode = idleMode; | |
} | |
} | |
} | |
} | |
///////////////////////////// Neopixel code ///////////////////////////// | |
void rainbow(uint8_t wait) { | |
uint16_t i, j; | |
for(j=0; j<256; j++) { | |
for(i=0; i<strip.numPixels(); i++) { | |
strip.setPixelColor(i, Wheel((i+j) & 255)); | |
} | |
strip.show(); | |
delay(10); | |
} | |
} | |
void colorWipe(uint32_t c, uint8_t wait) { | |
for(uint16_t i=0; i<strip.numPixels(); i++) { | |
strip.setPixelColor(i, c); | |
strip.show(); | |
delay(10); | |
} | |
} | |
void scared(){ | |
for(int i=0;i<NUMPIXELS;i++){ | |
// pixels.Color takes GRB values, from 0,0,0 up to 255,255,255 | |
strip.setPixelColor(i, strip.Color(0,250,0)); // Moderately bright green color. | |
strip.show(); // This sends the updated pixel color to the hardware. | |
} | |
} | |
void look(){ | |
for(int i=0;i<NUMPIXELS;i++){ | |
// pixels.Color takes GRB values, from 0,0,0 up to 255,255,255 | |
strip.setPixelColor(i, strip.Color(0,0,250)); // Moderately bright green color. | |
strip.show(); // This sends the updated pixel color to the hardware. | |
} | |
} | |
uint32_t Wheel(byte WheelPos) { | |
WheelPos = 255 - WheelPos; | |
if(WheelPos < 85) { | |
return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3); | |
} | |
if(WheelPos < 170) { | |
WheelPos -= 85; | |
return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3); | |
} | |
WheelPos -= 170; | |
return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0); | |
} | |
void theaterChase(uint32_t c, uint8_t wait) { | |
for (int j=0; j<10; j++) { //do 10 cycles of chasing | |
for (int q=0; q < 3; q++) { | |
for (int i=0; i < strip.numPixels(); i=i+3) { | |
strip.setPixelColor(i+q, c); //turn every third pixel on | |
} | |
strip.show(); | |
delay(wait); | |
for (int i=0; i < strip.numPixels(); i=i+3) { | |
strip.setPixelColor(i+q, 0); //turn every third pixel off | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment