Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * ApplySignal.ino
- * 2018 WLWilliams
- *
- * Basic Signal Generator v1.5
- * Learnelectronics
- * Aug 17 2020
- * www.youtube.com/learnelectronics
- * arduino0169@gmail.com
- *
- * Library code found at: https://github.com/Billwilliams1952/AD9833-Library-Arduino
- *
- */
- #include <Wire.h> //I2c library for the OLED display
- #include <Adafruit_SSD1306.h> //OLED driver
- #define OLED_RESET 4 //necessary for the OLED
- #include <AD9833.h> // Include the library
- #define FNC_PIN 5 // Can be any unused digital IO pin
- AD9833 gen(FNC_PIN); // Defaults to 25MHz internal reference frequency
- Adafruit_SSD1306 display(OLED_RESET); // create object for the OLED
- int rate = 100; //inital rate of sig gen
- int pot = 0; //initial value for potentiometer
- const int buttonPin = 8; //waveform switch on pin 8
- int buttonPushCounter = 0; //count how many times the button has been pushed
- int buttonState = 0; //is the button high or low
- int lastButtonState = 0; //remember the last state
- void setup() {
- gen.Begin(); // This MUST be the first command after declaring the AD9833 object
- // Apply a 1000 Hz sine wave using REG0 (register set 0). There are two register sets,
- // REG0 and REG1.
- // Each one can be programmed for:
- // Signal type - SINE_WAVE, TRIANGLE_WAVE, SQUARE_WAVE, and HALF_SQUARE_WAVE
- // Frequency - 0 to 12.5 MHz
- // Phase - 0 to 360 degress (this is only useful if it is 'relative' to some other signal
- // such as the phase difference between REG0 and REG1).
- // In ApplySignal, if Phase is not given, it defaults to 0.
- gen.ApplySignal(SINE_WAVE,REG0,rate); //start up the sig gen
- gen.EnableOutput(true); // Turn ON the output - it defaults to OFF
- pinMode(A0,INPUT); //set up analog0 to read the pot
- pinMode(8,INPUT_PULLUP); //set up dig 8 for input with internal pullup
- Serial.begin(9600); //start serial comms for debugging
- display.begin(SSD1306_SWITCHCAPVCC, 0x3C); //start up OLED
- display.display(); //show display buffer
- display.clearDisplay(); //clear OLED
- }
- void loop() {
- buttonState = digitalRead(buttonPin); //digital read of pin8
- pot = analogRead(A0); //analog read of A0
- rate = map(pot, 0, 1023, 1000, 10000); //map the value of the analog read to frequencies
- if (buttonState != lastButtonState) {
- // if the state has changed, increment the counter
- if (buttonState == HIGH) {
- // if the current state is HIGH then the button went from off to on:
- buttonPushCounter++;
- Serial.println("on");
- Serial.print("number of button pushes: ");
- Serial.println(buttonPushCounter);
- } else {
- // if the current state is LOW then the button went from on to off:
- Serial.println("off");
- }
- // Delay a little bit to avoid bouncing
- delay(50);
- }
- // save the current state as the last state, for next time through the loop
- lastButtonState = buttonState;
- if (buttonPushCounter > 2){
- buttonPushCounter = 0; //reset the number of button pushes
- }
- display.setTextSize(2); //send freq & wave type to OLED buffer
- display.setTextColor(WHITE);
- display.setCursor(0,0);
- display.clearDisplay();
- display.print("Freq:");
- display.println(rate);
- display.println("");
- display.print("Type:");
- switch (buttonPushCounter) { //determine which waveform to output
- case 1:
- display.print("Sine");
- gen.ApplySignal(SINE_WAVE,REG0,rate); //output desired waveform
- break;
- case 2:
- display.print("Tri");
- gen.ApplySignal(TRIANGLE_WAVE,REG0,rate); //output desired waveform
- break;
- case 3:
- display.print("Squar");
- gen.ApplySignal(SQUARE_WAVE,REG0,rate); //output desired waveform
- break;
- default:
- display.print("Squar");
- gen.ApplySignal(SQUARE_WAVE,REG0,rate); //if it gets out of sorts just do this
- break;
- }
- display.display(); //show whats in the OLED buffer
- //gen.ApplySignal(SINE_WAVE,REG0,rate);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement