I have this code for a tone.
#include <TimerInterrupt.h>
TimerInterrupt timer_interrupt;
int zaehler=0;
int digit1=7;
int digit2=6;
int digit3=5;
int digit4=4;
int ds=11;
int sh=12;
int st=13;
int position=0;
int LEDgruen=3;
int buttonrot=A3;
int LEDrot=A4;
int Alarm=9;
int LEDcheck=0;
int Verzoegerung=0;
int AnAus=0;
int Zeit=913;
int Alarmzeit=2020;
int Sekunde;
int Modus=0;
int Segment7position[4] = {
digit1,
digit2,
digit3,
digit4
};
int Segment7zahl[10][8]={
{1,1,1,1,1,1,0,0},//0
{1,1,0,0,0,0,0,0},//1
{1,0,1,1,0,1,1,0},//2
{1,1,1,0,0,1,1,0},//3
{1,1,0,0,1,0,1,0},//4
{0,1,1,0,1,1,1,0},//5
{0,1,1,1,1,1,1,0},//6
{1,1,0,0,0,1,0,0},//7
{1,1,1,1,1,1,1,0},//8
{1,1,1,0,1,1,1,0}//9
};
int Segment7dezimalposition[4] = {
1000,
100,
10,
1
};
int Segment7segmentzahl ( int position , int zahl ) {
return((zahl/(Segment7dezimalposition[position]))%10);
}
void Segment7anzeige ( int position , int dezimalzahl ) {
for(int counter=0;counter<=3;counter++){
if(position!=counter)
digitalWrite(Segment7position[counter],LOW);}
digitalWrite(st,0);
for (int index1=0;index1<=7;index1++)
{
digitalWrite(ds,0);
digitalWrite(sh,0);
digitalWrite(ds,Segment7zahl[dezimalzahl][index1]);
digitalWrite(sh,1);
}
digitalWrite(st,1);
digitalWrite(Segment7position[position], HIGH);
}
ISR(TIMER1_COMPA_vect) {
position=((position+1)%4);
if(Modus==0){
Segment7anzeige(position, Segment7segmentzahl(position,Zeit));}
else if(Modus==1){
Segment7anzeige(position, Segment7segmentzahl(position,Alarmzeit));}
if(Modus==2){
Segment7anzeige(position, Segment7segmentzahl(position,Zeit));}
if ((Verzoegerung++)%200==0){
Uhrwerk();
AnAus=~AnAus;
digitalWrite(LEDgruen, AnAus);
}
}
void Uhrwerk(){
Sekunde=Sekunde+1;
if (Sekunde%60==0){
Zeit=Zeit+1;
if (Zeit%100==60){
Zeit=Zeit+40;};
if (Zeit%2400==0){
Zeit=0;}
}
}
void Uhrwerkein(){
if (digitalRead(8)==HIGH){
Zeit=Zeit+1;
while (digitalRead(8)==HIGH) delay(1);
if (Zeit%100==60){
Alarmzeit=Zeit+40;}
if (Zeit%2400==0){
Zeit=0;}
}
if (digitalRead(10)==HIGH){
Zeit=Zeit+100;
while (digitalRead(10)==HIGH) delay(1);
if (Zeit%100==60){
Alarmzeit=Zeit+40;}
if (Zeit>2400){
Zeit=Zeit-2400;}
}
}
void Alarmzeitein(){
if (digitalRead(8)==HIGH){
Alarmzeit=Alarmzeit+1;
while (digitalRead(8)==HIGH) delay(1);
if (Alarmzeit%100==60){
Alarmzeit=Alarmzeit+40;}
if (Alarmzeit%2400==0){
Alarmzeit=0;}
}
if (digitalRead(10)==HIGH){
Alarmzeit=Alarmzeit+100;
while (digitalRead(10)==HIGH) delay(1);
if (Alarmzeit%100==60){
Alarmzeit=Alarmzeit+40;}
if (Alarmzeit>2400){
Alarmzeit=Alarmzeit-2400;}
}
}
void setup() {
Serial.begin(9600);
pinMode(ds,OUTPUT);
pinMode(sh,OUTPUT);
pinMode(st,OUTPUT);
pinMode(LEDgruen,OUTPUT);
pinMode(LEDrot,OUTPUT);
pinMode(Alarm,OUTPUT);
pinMode(digit1,OUTPUT);
pinMode(digit2,OUTPUT);
pinMode(digit3,OUTPUT);
pinMode(digit4,OUTPUT);
pinMode(2,INPUT);
pinMode(8,INPUT);
pinMode(10,INPUT);
pinMode(buttonrot,INPUT);
digitalWrite(LEDrot,LOW);
cli();//stop interrupts
TCCR1A = 0;// set entire TCCR1A register to 0
TCCR1B = 0;// same for TCCR1B
TCNT1 = 0;//initialize counter value to 0
OCR1A = 9999;// = (1610^6) / (2008) - 1 (must be <65536)
TCCR1B |= (1 << WGM12);
TCCR1B |= (0 << CS12) | (1 << CS11)| (0 >> CS10);
TIMSK1 |= (1 << OCIE1A);
sei();//allow interrupts
}
void loop () {
digitalWrite(Alarm, LOW);
if ((Alarmzeit==Zeit) && (Modus==0) && (digitalRead(LEDrot)==HIGH)) {
while (digitalRead(LEDrot)==HIGH){
tone(Alarm, 440);
delay(500);
noTone(Alarm);
delay(200);
tone(Alarm, 440);
delay(500);
noTone(Alarm);
delay(200);
tone(Alarm, 440);
delay(500);
noTone(Alarm);
delay(200);
tone(Alarm, 349);
delay(350);
noTone(Alarm);
delay(100);
tone(Alarm, 523);
delay(150);
noTone(Alarm);
delay(50);
tone(Alarm, 440);
delay(500);
noTone(Alarm);
delay(200);
tone(Alarm, 349);
delay(350);
noTone(Alarm);
delay(100);
tone(Alarm, 523);
delay(150);
noTone(Alarm);
delay(50);
tone(Alarm, 440);
delay(650);
noTone(Alarm);
delay(650);
tone(Alarm, 659);
delay(500);
noTone(Alarm);
delay(200);
tone(Alarm, 659);
delay(500);
noTone(Alarm);
delay(200);
tone(Alarm, 659);
delay(500);
noTone(Alarm);
delay(200);
tone(Alarm, 698);
delay(350);
noTone(Alarm);
delay(100);
tone(Alarm, 523);
delay(150);
noTone(Alarm);
delay(50);
tone(Alarm, 415);
delay(500);
noTone(Alarm);
delay(200);
tone(Alarm, 349);
delay(350);
noTone(Alarm);
delay(100);
tone(Alarm, 523);
delay(150);
noTone(Alarm);
delay(50);
tone(Alarm, 440);
delay(650);
noTone(Alarm);
delay(650);
tone(Alarm, 880);
delay(500);
noTone(Alarm);
delay(200);
tone(Alarm, 440);
delay(300);
noTone(Alarm);
delay(100);
tone(Alarm, 440);
delay(150);
noTone(Alarm);
delay(200);
tone(Alarm, 880);
delay(500);
noTone(Alarm);
delay(200);
tone(Alarm, 830);
delay(325);
noTone(Alarm);
delay(100);
tone(Alarm, 784);
delay(175);
noTone(Alarm);
delay(50);
tone(Alarm, 740);
delay(125);
noTone(Alarm);
delay(200);
tone(Alarm, 698);
delay(125);
noTone(Alarm);
delay(200);
tone(Alarm, 740);
delay(250);
noTone(Alarm);
delay(650);
tone(Alarm, 455);
delay(250);
noTone(Alarm);
delay(200);
tone(Alarm, 622);
delay(500);
noTone(Alarm);
delay(200);
tone(Alarm, 587);
delay(325);
noTone(Alarm);
delay(100);
tone(Alarm, 554);
delay(175);
noTone(Alarm);
delay(150);
tone(Alarm, 523);
delay(125);
noTone(Alarm);
delay(200);
tone(Alarm, 466);
delay(125);
noTone(Alarm);
delay(200);
tone(Alarm, 523);
delay(250);
noTone(Alarm);
delay(650);
tone(Alarm, 349);
delay(250);
noTone(Alarm);
delay(200);
tone(Alarm, 415);
delay(500);
noTone(Alarm);
delay(100);
tone(Alarm, 349);
delay(350);
noTone(Alarm);
delay(50);
tone(Alarm, 440);
delay(125);
noTone(Alarm);
delay(200);
tone(Alarm, 523);
delay(500);
noTone(Alarm);
delay(100);
tone(Alarm, 440);
delay(375);
noTone(Alarm);
delay(50);
tone(Alarm, 523);
delay(125);
noTone(Alarm);
delay(200);
tone(Alarm, 659);
delay(650);
noTone(Alarm);
delay(650);
tone(Alarm, 349);
delay(250);
noTone(Alarm);
delay(200);
tone(Alarm, 415);
delay(500);
noTone(Alarm);
delay(100);
tone(Alarm, 349);
delay(350);
noTone(Alarm);
delay(50);
tone(Alarm, 523);
delay(125);
noTone(Alarm);
delay(200);
tone(Alarm, 440);
delay(500);
noTone(Alarm);
delay(100);
tone(Alarm, 349);
delay(375);
noTone(Alarm);
delay(50);
tone(Alarm, 523);
delay(125);
noTone(Alarm);
delay(200);
tone(Alarm, 440);
delay(650);
noTone(Alarm);
delay(650);
}
}
digitalWrite(Alarm, LOW);
if (digitalRead(2)==HIGH) {
Modus=Modus+1;
while (digitalRead(2)==HIGH) delay(1000);
if(Modus==3){
(Modus=0);
}
}
if (Modus==1) {
Alarmzeitein();
}
if (Modus==2) {
Uhrwerkein();
}
if (digitalRead(buttonrot)==HIGH){
if (LEDcheck==0){
LEDcheck=1;
digitalWrite(LEDrot, HIGH);
}
else {
LEDcheck=0;
digitalWrite(LEDrot, LOW);
}
delay(1000);
}
Serial.println(Modus);
}
If the conditions are given i want the tone to start but as soon as i press the button, so when digitalRead(buttonrot)==HIGH, I want it to stop.
Is there something like a "till"?
Thanks for your help