Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- */
- #define S0 4
- #define S1 5
- #define S2 6
- #define S3 7
- #define sensorOut 8
- #define SPEAKER 9
- const int buttonPin = 2;
- const int switchPin = 3;
- int buttonstate = 0;
- int switchstate = 0;
- int frequencies[20] = {0};
- int red_frequency = 0;
- int blue_frequency = 0;
- int green_frequency = 0;
- int sound_frequency = 0;
- int frequency_calculation(int red_freq, int green_freq, int blue_freq) {
- return (red_freq + green_freq + blue_freq);
- }
- void setup() {
- pinMode(S0, OUTPUT);
- pinMode(S1, OUTPUT);
- pinMode(S2, OUTPUT);
- pinMode(S3, OUTPUT);
- pinMode(sensorOut, INPUT);
- pinMode(buttonPin, INPUT);
- pinMode(switchPin, INPUT);
- }
- void loop() {
- buttonState = digitalRead(buttonPin);
- switchState = digitalRead(swithPin);
- if (switchState == HIGH) {
- for (int i = 0; i < 20; i++) {
- tone(SPEAKER, frequencies[i], 100);
- delay(100);
- }
- } else {
- if (buttonState == HIGH) {
- for (int i = 0; i < 20; i++) {
- /*Getting the red frequency */
- digitalWrite(S2, LOW);
- digitalWrite(S3,LOW);
- red_frequency = pulseIn(sensorOut, LOW);
- /*Getting the green frequency */
- digitalWrite(S2,HIGH);
- digitalWrite(S3,HIGH);
- green_frequency = pulseIn(sensorOut, LOW);
- /*Getting the blue frequency */
- digitalWrite(S2,LOW);
- digitalWrite(S3,HIGH);
- blue_frequency = pulseIn(sensorOut, LOW);
- /*Getting the sound frequency and saving the value in the array*/
- sound_frequency = frequency_calculation(red_frequency, green_frequency, blue_frequency);
- tone(SPEAKER, sound_frequency, 100);
- frequencies[i] = sound_frequency;
- }
- } else {
- /*Getting the red frequency */
- digitalWrite(S2, LOW);
- digitalWrite(S3,LOW);
- red_frequency = pulseIn(sensorOut, LOW);
- /*Getting the green frequency */
- digitalWrite(S2,HIGH);
- digitalWrite(S3,HIGH);
- green_frequency = pulseIn(sensorOut, LOW);
- /*Getting the blue frequency */
- digitalWrite(S2,LOW);
- digitalWrite(S3,HIGH);
- blue_frequency = pulseIn(sensorOut, LOW);
- /*Playing the sound */
- sound_frequency = frequency_calculation(red_frequency, green_frequency, blue_frequency);
- tone(SPEAKER, sound_frequency, 100);
- delay(100);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement