Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // senses hand wave movement
- // lights
- int l1 = 6;
- int l2 = 9;
- int l3 = 10;
- int l4 = 11;
- // analog inputs
- int i1 = A3;
- int i2 = A2;
- int i3 = A1;
- int i4 = A0;
- // voltages at the inputs
- float v1;
- float v2;
- float v3;
- float v4;
- // array of the order in which pins should dim
- int order[] = {0,0,0,0};
- int orderCount = 0;
- void setup() {
- // run the reset funciton once just to get everything zeroed
- dimSetReset();
- pinMode(l1, OUTPUT);
- pinMode(l2, OUTPUT);
- pinMode(l3, OUTPUT);
- pinMode(l4, OUTPUT);
- }
- void loop() {
- // we only want to loop when we see a "motion"
- if((analogRead(i1) * .004882814 > 0.30) || (analogRead(i2) * .004882814 > 0.30) || (analogRead(i3) * .004882814 > 0.30) || (analogRead(i4) * .004882814 > 0.30)) {
- // run thru the process a bunch to see what happens
- for(int i = 0; i < 100; ++i) {
- v1 = getvoltage(i1);
- checkvoltage(v1,l1);
- v2 = getvoltage(i2);
- checkvoltage(v2,l2);
- v3 = getvoltage(i3);
- checkvoltage(v3,l3);
- v4 = getvoltage(i4);
- checkvoltage(v4,l4);
- delay(5);
- }
- // we have gone thru the loop, lets see what happens and reset for next time
- dimEnd();
- dimSetReset();
- }
- }
- // return the voltage that a pin sees using a conversion factor
- float getvoltage(int pin) {
- return (analogRead(pin) * 0.004882814);
- }
- // big function that figures out what order the pins are seeing the voltage in
- void checkvoltage(float voltage, int pin) {
- int pinFound = 0;
- // don't do anything unless we see a voltage above the threshold
- if(voltage >= 0.30) {
- // turn the LED on first
- digitalWrite(pin, HIGH);
- // make sure that we havent seen a voltage on the given pin already
- // we don't want to clog up the array
- for(int i = 0; i < 4; ++i) {
- if(order[i] == pin)
- pinFound = 1;
- }
- // if we haven't found a dupicate pin (good) and the order we are at is unfilled (0)
- // then fill the array with the value of the pin that we detected a voltage change at
- if(!pinFound && order[orderCount] == 0) {
- order[orderCount] = pin;
- orderCount++;
- }
- }
- }
- // dim the specified pin using PWM
- void dim(int pin) {
- for(int i = 255; i >= 0; --i) {
- analogWrite(pin, i);
- delay(1);
- }
- }
- // set the order in which to dim the LEDs and dim them
- void dimEnd() {
- // look thru the order array and dim the LEDs in the specified order
- for(int i = 0; i < 4; ++i) {
- switch(order[i]) {
- case 6:
- dim(l1);
- break;
- case 9:
- dim(l2);
- break;
- case 10:
- dim(l3);
- break;
- case 11:
- dim(l4);
- break;
- default:
- break;
- }
- }
- }
- // reset so we can begin afresh
- void dimSetReset() {
- // reset order array
- for(int i = 0; i < 4; ++i) {
- order[i] = 0;
- }
- // start over with what position we are in the order of LEDs
- orderCount = 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement