brmxcy
October 29, 2023, 7:31pm
1
Hello, right now I am trying to get a light and sound to play simultaneously for one second on and one second off, repeating four times but only when the potentiometer is in a particular range. If the potentiometer is moved the light and sound should stop and reset once the potentiometer is moved back into range. If the potentiometer stays within range, the light and noise shouls stop after 4 cycles. I will post full code, but the part I seek help on is the last if statement thats between values 513-768.
int hertz = 1000;
int bomb = 200;
void setup()
{
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
pinMode(12, OUTPUT);
pinMode(7, INPUT_PULLUP);
}
void loop()
{
for(int hertz = 1000; hertz <= 4000; hertz = hertz +250)
{
if(analogRead(A0) <=256)
{
digitalWrite(12,HIGH);
delay(200);
tone(8,hertz,bomb);
digitalWrite(12,LOW);
delay(200);
}
}
hertz = 1000;
if(analogRead(A0) >= 257 && analogRead(A0) <= 512)
{
digitalWrite(10,HIGH);
delay(10);
digitalWrite(10,LOW);
if(digitalRead(7) == LOW)
{
while(analogRead(A0) >= 257 && analogRead(A0) <= 512)
{
digitalWrite(10,LOW);
}
}
}
if(analogRead(A0) >=513 && analogRead(A0) <= 768)
{
for(int i = 0;i <4; i++)
{
digitalWrite(9, HIGH);
tone(8, hertz, 1000);
delay(1000);
digitalWrite(9, LOW);
delay(1000);
}
}
}
LarryD
October 29, 2023, 7:45pm
2
The first thing you need to master is how to make non-blocking TIMERs.
i.e. avoid using delay(...)
Part 2
We have seen how to turn an LED on and off at intervals which is fascinating for maybe a minute or two at most but how about using the same principle to change the brightness of an LED ?
I will use an LED on pin 10 in this example as that pin supports PWM output. The basis of the program will be the same as before except that when the period ends the brightness of the LED will be changed. The period will be much less than 1 second, unless you want to wait a long while, but otherwise theā¦
In general, you should not use magic numbers. The I/O pins love to have a functional name.
gcjr
November 1, 2023, 10:35am
4
brmxcy:
play simultaneously for one second on and one second off, repeating four times but only when the potentiometer is in a particular range. If the potentiometer is moved the light and sound should stop and reset once the potentiometer is moved back into range. If the potentiometer stays within range, the light and noise shouls stop after 4 cycles.
i hope this is saying you want something to happen no more than 4 times after the pot value is within a certain range
looks like the code
toggles pin 12 and generates a tone from 1 to 4K if the pot <= 256
toggles pin 10 if the pot is 257 <= 512 and doesn't nothing while the pot value remains within that range
toggles pin 9 and turns a tone on/off 4 times if the pot is 513 <= 768
the code doesn't seem to match your stated goal
look this over
int hertz = 1250;
int cnt;
void loop()
{
int potVal = analogRead(A0);
if (257 <= potVal && potVal <= 512) {
if (4 > cnt) {
cnt++;
Serial.println ("action");
tone(8, hertz, 1000);
delay(1000);
}
}
else {
cnt = 0;
Serial.println (potVal);
delay (500);
}
}
void setup()
{
Serial.begin (9600);
}
system
Closed
April 29, 2024, 10:36am
5
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.