Hello,
Iam totally new with Arduino (Attiny25/45/85).
Iam trying to program an Attiny45 with a special tone sequence.
The sequence consists of two tones A=2kHz and B=3,2kHz ( A,A,B,mute,B,A).
A member of a German forum helped me with the sequence:
include <avr/io.h>
#define F_CPU 16000000L
#include <util/delay.h>
void A (void) {
for(int i=0; i<100; i++) { // A: 2kHz, 50ms
PORTB = 0x01;
_delay_us(250);
PORTB = 0x00;
_delay_us(250);
}
}
void B (void) {
for(int i=0; i<160; i++) { // B: 3k2Hz, 50ms
PORTB = 0x01;
_delay_us(155);
PORTB = 0x00;
_delay_us(156);
}
}
int main(void)
{
DDRB = 0x01; // B0 = LED
while(1)
{
// 50ms Ton an, 20ms Pause Tonfrequenz A: 2000Hz Tonfrequenz B: 3200Hz
// Die Sequenz ist: A,A,B,still,B,A Das ganze dauert 6*70ms = 420ms
A();
_delay_ms(20); // kurze Pause
A();
_delay_ms(20); // kurze Pause
B();
_delay_ms(20); // kurze Pause
_delay_ms(50); // still
_delay_ms(20); // kurze Pause
B();
_delay_ms(20); // kurze Pause
A();
_delay_ms(3000); // wart mal ab...
}
}
This works perfectly. Now I want to toggle this sequence with a LOW signal on the input (PB1).
I figured out how to switch on an LED (for 500ms) with LOW on the input:
const int led = 0;
const int button = 1;
void setup() {
pinMode(button, INPUT_PULLUP);
pinMode(led, OUTPUT);
}
void loop() {
if (digitalRead(button)== HIGH)digitalWrite(led, LOW); // Turn LED on if button is not pressed
else {
digitalWrite(led, HIGH); // Turn LED off if button is pressed
delay(500);
}
}
This works as supposed.
I know that I cant just put these codes after each other, but Iam missing some basic understanding how to stack the different code parts toghether logically. ![]()
What would I have to add/change to get the code working like it should?
I would be very happy if anyone here has some suggestions or hints, so that I maybe can get in the right direction.
Best regards and thank you very much for your help.
Alex

