Last active
May 15, 2016 19:54
-
-
Save ankovsh/c75d7b5f3a66458ea5fc87e4ad07ff0f to your computer and use it in GitHub Desktop.
Лазерная арфа на базе Arduino
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 <Stepper.h> | |
int potentiometerPin = 1; | |
int LDRPin = 0; | |
int motorPin1 = 2; | |
int motorPin2 = 3; | |
int motorPin3 = 4; | |
int motorPin4 = 5; | |
int motorSpeed = 1000; | |
int delayMotor = 3; | |
int delayLaser = 2; | |
int LaserState = LOW; | |
int LaserPin = 7; | |
int thresholdConst = 400; | |
int sensor = 0; | |
int notes[] = {0, 323, 324, 325, 326, 327, 328, 329, 330}; | |
int noteStatuses[] = { 0, 0, 0, 0, 0, 0, 0, 0 }; | |
int noteCount = 7; | |
bool isRevers = false; | |
const int stepsPerRevolution = 8; | |
Stepper myStepper(stepsPerRevolution, 2, 3, 4, 5); | |
void setup() | |
{ | |
myStepper.setSpeed(motorSpeed); | |
Serial.begin(9600); | |
pinMode(motorPin1, OUTPUT); | |
pinMode(motorPin2, OUTPUT); | |
pinMode(motorPin3, OUTPUT); | |
pinMode(motorPin4, OUTPUT); | |
pinMode(LaserPin, OUTPUT); | |
if (!isRevers) | |
{ | |
delay(delayMotor); | |
for (int i = 1; i < noteCount; i++) | |
{ | |
myStepper.step(1); | |
delay(delayMotor); | |
} | |
isRevers = true; | |
} | |
else | |
{ | |
for (int i = 0; i < noteCount-1; i++) | |
{ | |
myStepper.step(-1); | |
delay(delayMotor); | |
delay(delayLaser); | |
} | |
isRevers = false; | |
} | |
} | |
int threshold() | |
{ | |
return thresholdConst - analogRead(potentiometerPin)*0.2; | |
} | |
void thresholdSettings() | |
{ | |
digitalWrite(LaserPin, HIGH); | |
int noteForCheck = 0; | |
while(noteStatuses[noteForCheck] == 0) | |
{ | |
if(analogRead(LDRPin) > threshold()) | |
{ | |
noteOn(0x90, notes[noteForCheck], 0x7F); | |
noteStatuses[noteForCheck] = 1; | |
} | |
delay(1000); | |
} | |
if(noteStatuses[noteForCheck] == 1) | |
{ | |
noteOn(0x90, notes[noteForCheck], 0x00); | |
noteStatuses[noteForCheck] = 0; | |
} | |
} | |
void noteOn(int cmd, int pitch, int velocity) | |
{ | |
Serial.write(cmd); | |
Serial.write(pitch); | |
Serial.write(velocity); | |
} | |
bool isAnyNoteExceptThisPlaying(int number) | |
{ | |
int sum = 0; | |
for(int i = 0; i < noteCount; ++i) | |
{ | |
if(i != number) | |
{ | |
sum += noteStatuses[i]; | |
} | |
} | |
return sum > 0; | |
} | |
void check(int number) | |
{ | |
digitalWrite(LaserPin, HIGH); | |
delay(delayLaser); | |
if(analogRead(LDRPin) > threshold()) | |
{ | |
if(!isAnyNoteExceptThisPlaying(number)) | |
{ | |
if(noteStatuses[number] == 0) | |
{ | |
noteOn(0x90, notes[number], 0x7F); | |
} | |
noteStatuses[number] = 1; | |
} | |
} | |
else if(analogRead(LDRPin) < threshold()) | |
{ | |
if(noteStatuses[number] >= 1) | |
{ | |
if(++noteStatuses[number] == 2) | |
{ | |
noteOn(0x90, notes[number], 0x00); | |
noteStatuses[number] = 0; | |
} | |
} | |
} | |
digitalWrite(LaserPin, LOW); | |
} | |
void loop() | |
{ | |
if (!isRevers) | |
{ | |
delay(delayLaser); | |
delay(delayMotor); | |
for (int i = 1; i <= noteCount; i++) | |
{ | |
myStepper.step(1); | |
delay(delayMotor); | |
check(i); | |
} | |
isRevers = true; | |
} | |
else | |
{ | |
for (int i = 0; i < noteCount; i++) | |
{ | |
myStepper.step(-1); | |
delay(delayMotor); | |
delay(delayLaser); | |
} | |
isRevers = false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment