Learned input and output pins on Arduino NANO 33 IoT, the structure picture above is the most common reference I used this week that helps me choose the right pin.
Use button press as input and output voltage for LED
Use button press as input and output voltage for LED
Use "delay" to make the LED blink
Use "delay" to make the LED blink
Use "delay" to make the speaker beep like alarm
Use "delay" to make the speaker beep like alarm
Change output tone in Hz for the speaker, code showing below
Change output tone in Hz for the speaker, code showing below
void setup() {
pinMode(3, OUTPUT);
}
void loop() {
tone(3, 261.63);
delay(500);
tone(3, 293.66);
delay(500);
tone(3, 329.63);
delay(500);
tone(3, 349.23);
delay(500);
tone(3, 392.00);
delay(500);
tone(3, 440.00);
delay(500);
tone(3, 493.88);
delay(500);
tone(3, 523.25);
delay(500);
}
If without delay
, the speaker only play one tone continusly rather than paly different tones. And I can't write noTone()
, otherwise it stops playing even there are codes after that.
On the left image, I set up the potentiometer and photoresistor as analog inputs. I also learned to add a 0.1 microfarad capacitor that can smooth the reading of the potentiometer.
The output reading of the potientiometer range from 0~1023, which is exactly 10-bit data, but the photoresistor only ranges from around 700-1000.
// add delay()
If I only read photoresistor in the circut, it works much better.
In this video, I have the potentiometer as an analog input, which simultaneously controls the light brightness and speaker tone, shown below.
Though the speaker has a frequency output range of 0~2560, it can only play 4 or 5 pitches.
const int ledPin = 2;
int analogValue = 0;
int brightness = 0;
int frequency;
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
}
void loop() {
analogValue = analogRead(A7);
brightness = analogValue / 4;
analogWrite(ledPin, brightness);
Serial.println(brightness);
frequency = (analogValue/4) * 10; //0-2560
tone(3, frequency);
Serial.println(brightness);
}
Sensor_Pushbutton1
Sensor_Pushbutton2