Skip to content

Instantly share code, notes, and snippets.

@pepf
Created October 4, 2014 13:30
Show Gist options
  • Save pepf/42be25b2d62982aeac68 to your computer and use it in GitHub Desktop.
Save pepf/42be25b2d62982aeac68 to your computer and use it in GitHub Desktop.
arduino led test code
int pins[] = {3,5,9,10,11};
int nr = 5;
// the setup routine runs once when you press reset:
void setup() {
Serial.begin(9600);
// declare pins[] to be an output:
for (int i =0; i<nr; i++) {
pinMode(pins[i],OUTPUT);
analogWrite(pins[i],0);
delay(500);
analogWrite(pins[i],255);
delay(500);
analogWrite(pins[i],0);
}
}
// the loop routine runs over and over again forever:
void loop() {
for (int i =0; i<nr; i++) {
fadeInOut(pins[i]);
Serial.print("X:\t");
readSensor(1);
Serial.print("Y:\t");
readSensor(2);
Serial.print("Z:\t");
readSensor(3);
}
}
void readSensor(int pin) { //X= A1 Y=A2 Z=A3
int X=0;
for(int i=0; i<5; i++) { //improves accuracy
X += analogRead(pin);
delay(2);
}
X = X/5;
Serial.println(X);
}
void fadeInOut(int pin) {
int brightness = 0;
int fadeAmount = 5; // how many points to fade the LED by
while(1){
// change the brightness for next time through the loop:
brightness = brightness + fadeAmount;
// reverse the direction of the fading at the ends of the fade:
if (brightness >= 255) {
fadeAmount = -fadeAmount ;
}
// set the brightness of pin
analogWrite(pin, brightness);
if (brightness <=0 ) { break; }
// wait for 30 milliseconds to see the dimming effect
delay(30);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment