Skip to content

Instantly share code, notes, and snippets.

@podstawek
podstawek / pwm_melody.ino
Created April 25, 2021 12:55
The tune of Przasniczka using Arduino and a PC speaker
View pwm_melody.ino
int speakerPin = 26; // speaker on digital pin 26
float frequencies[] = {146.8, 146.8, 146.8, 174.6, 196, 220, 146.8, 146.8, 246.94, 220, 196, 220, 220, 220, 164.8, 220, 164.8, 174.6, 146.8, 174.6, 220, 164.8, 164.8, 174.6, 220, 261.6, 246.9, 220, 196, 174.6, 174.6, 196, 196, 164.8, 130.8, 440, 440, 493.9, 440, 415.3, 440, 587.3, 493.9, 392, 329.6, 440, 293.6};
float durations[] = {0.3, 0.05, 0.05, 0.2, 0.2, 0.4, 0.2, 0.2, 0.4, 0.2, 0.2, 0.4, 0.4, 0.2, 0.2, 0.2, 0.2, 0.4, 0.2, 0.2, 0.4, 0.2, 0.2, 0.6, 0.2, 0.3, 0.1, 0.2, 0.2, 0.6, 0.2, 0.3, 0.1, 0.2, 0.2, 0.6, 0.3, 0.1, 0.2, 0.2, 0.4, 0.4, 0.2, 0.2, 0.2, 0.2, 0.4};
int musicLength = sizeof(frequencies) / sizeof(frequencies[0]);
float delayInMicroseconds;
int numberOfIterations;
@podstawek
podstawek / a440hz.ino
Created April 25, 2021 12:57
440Hz (A4) note using square wave generated by an Arduino for PC speaker
View a440hz.ino
int speakerPin = 26; // speaker on digital pin 26
float frequency = 440; // in hertz. 440Hz is the note A4
float duration = 2; // in seconds
// Period is the inverse of frequency, measured in seconds.
// To obtain microseconds we multiply by 1 million.
// And to obtain half of period
// (the time in either "on" or "off" state), we divide by 2.
float delayInMicroseconds = 1/frequency*1000000/2;
@podstawek
podstawek / 1hz.ino
Created April 25, 2021 13:00
One tick every 500 milliseconds: Arduino plays on PC Speaker
View 1hz.ino
int speakerPin = 26; // speaker on digital pin 26
void setup() {
pinMode(speakerPin, OUTPUT);
}
void loop() {
@podstawek
podstawek / pwm_arpeggios.ino
Created April 25, 2021 14:29
Fast arpeggios played to simulate chords C a d G
View pwm_arpeggios.ino
int speakerPin = 26; // speaker on digital pin 26
float frequencies[] = {
130.8, 164.8, 196, 130.8, 164.8, 196, 130.8, 164.8, 196, 130.8, 164.8, 196, 130.8, 164.8, 196, 130.8, 164.8, 196, 130.8, 164.8, 196, 130.8, 164.8, 196, 130.8, 164.8, 196, 130.8, 164.8, 196,
220, 261.6, 329.6, 220, 261.6, 329.6, 220, 261.6, 329.6, 220, 261.6, 329.6, 220, 261.6, 329.6, 220, 261.6, 329.6, 220, 261.6, 329.6, 220, 261.6, 329.6, 220, 261.6, 329.6, 220, 261.6, 329.6,
146.8, 174.6, 220, 146.8, 174.6, 220, 146.8, 174.6, 220, 146.8, 174.6, 220, 146.8, 174.6, 220, 146.8, 174.6, 220, 146.8, 174.6, 220, 146.8, 174.6, 220, 146.8, 174.6, 220, 146.8, 174.6, 220,
196, 247, 293.7, 196, 247, 293.7, 196, 247, 293.7, 196, 247, 293.7, 196, 247, 293.7, 196, 247, 293.7, 196, 247, 293.7, 196, 247, 293.7, 196, 247, 293.7, 196, 247, 293.7};
float durations[] = {
0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0
View modulated_speaker.ino
int speakerPin = 26; // speaker on digital pin 26
int coarseTune, fineTune, duty, coarseDuty, fineDuty;
void setup() {
pinMode (speakerPin, OUTPUT);
pinMode (A4, INPUT); // coarse potentiometer on analog pin A4
pinMode (A5, INPUT); // fine potentiometer on analog pin A5
pinMode (A6, INPUT); // duty cycle potentiometer on analog pin A6
}
View receiveFilesFromECB.py
import serial
import sys
from datetime import datetime, date
ourSerial = serial.Serial()
ourSerial.port = '/dev/ttySC1'
ourSerial.baudrate = 4800
ourSerial.bytesize = serial.EIGHTBITS
ourSerial.parity = serial.PARITY_NONE
ourSerial.stopbits = serial.STOPBITS_ONE
View sendFilesToECB.py
import serial
import sys
ourSerial = serial.Serial()
ourSerial.port = '/dev/ttySC1'
ourSerial.baudrate = 4800
ourSerial.bytesize = serial.EIGHTBITS
ourSerial.parity = serial.PARITY_NONE
ourSerial.stopbits = serial.STOPBITS_ONE
ourSerial.xonxoff = False
View freq_and_duty_modulation.asm
* makes beeps
ORG $300
LDA #$FF
STA FREQ
LDA #$80
STA PLSWIDTH
PLAY LDA $C030
LDY PLSWIDTH
PULSE DEY
View freqDutyMod.s
* makes beeps
ORG $300
LDA #$FF
STA FREQ
LDA #$80
STA PLSWIDTH
PLAY LDA $C030
LDY PLSWIDTH
PULSE DEY
View flip2.asm
org $8000
start: ld a,%00010000 ; load accumulator with any value that sets the 4th bit
out (&fe),a ; send that value to port 254, i.e. hexadecimal FE
xor %00010000 ; flip the 4th bit to the opposite of what it was before
out (&fe),a ; send that value to port 254 again
end start