please help me. I can't figure out what's wrong. at first the knappstate is LOW and when I push the button I go to knappstate HIGH. so far its working. but when I release the button everything stops. what's wrong with the code? please help >:(
#include <Volume3.h>
#include <TimerOne.h>
const int led = 13;
const int pitch = A0; // sensor pinne
const int volym = A1; // sensor pinne
const int bpm = A2; // sensor pinne
const int knapp = 2; // ledpinne
const int ljud = 9; //högtalarpinne
int rattValue = 0; //värdet som lagras när sensorn läst av ljusstyrkan
int volymValue = 0; //värdet som lagras när sensorn läst av ljusstyrkan
int knappState = 0;
int ledState = 0;
int ljudState = 0;
int tempo;
long previousMillis = 0;
long interval = 1000;
void setup() {
Serial.begin(9600); pinMode(led, OUTPUT); pinMode(ljud, OUTPUT); pinMode(knapp, INPUT);
}
void loop() {
knappState = digitalRead(knapp);
if (knappState == LOW) {
int pitchValue = analogRead(pitch); Serial.println(pitchValue);
int thisPitch = map(pitchValue, 1023, 0, 880, 110);
int volymValue = analogRead(volym); Serial.println(volymValue);
int thisLoud = map(volymValue, 1023, 0, 1000, 0);
int bpmValue = analogRead(bpm); Serial.println(bpmValue);
int tempo = map(bpmValue, 1023, 0, 560, 80);
vol.tone(ljud, thisPitch, thisLoud);
int interval = (1000/tempo)*60; //algorithm to convert tempo into BPM
unsigned long currentMillis = millis();
if(currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
if (ledState == 0)
digitalWrite(led, HIGH);
} else
ledState = 0;
digitalWrite(led, LOW);
}
if (knappState == HIGH) {
int pitchValue = analogRead(pitch); Serial.println(pitchValue);
int thisPitch = map(pitchValue, 1023, 0, 880, 110);
int volymValue = analogRead(volym); Serial.println(volymValue);
int thisLoud = map(volymValue, 1023, 0, 1000, 0);
int bpmValue = analogRead(bpm); Serial.println(bpmValue);
int tempo = map(bpmValue, 1023, 0, 560, 80);
int interval = (1000/tempo)*60; //algorithm to convert tempo into BPM
unsigned long currentMillis = millis();
if(currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
if (ljudState == 0)
tone(ljud, thisPitch, thisLoud);
digitalWrite(led, HIGH);
} else
ljudState = 0;
digitalWrite(led, LOW);}
knappState = digitalRead(knapp);
}