Using programming language C++ and IF statements, can you help generate a code t
ID: 3871168 • Letter: U
Question
Using programming language C++ and IF statements, can you help generate a code to make this work on a MBED? Thank you! For this assignment you will use the mbed with its Digitalin and PwmOut objects to implement a simple musical instrument. Connect 4 pushbutton switches in active-high configuration to pins p5 through p8 of the mbed. Connect the piezo transducer between pin p21 and GND Pressing a switch should generate a particular musical note on the piezo transducer (at maximum volume) and light up a particular LED (at full brightness). When the switch is released, the note should fade out over a 0.1 second period with both the brightness of the LED and the volume of the piezo transducer perceptibly diminishing over time rather than immediately stopping. The following table shows the frequencies for the notes as well as the LED that should light up for each note Switch pin Frequency LED (Hz) 1046 1175 1319 1397 LED1 LED2 LED3 LED4 p6 p8 The piezo transducer will output a note at maximum value when the PWM duty cycle is set to 50% and at reduced volume as the duty cycle approaches either 0% or 100%. Note however that this extremely non- linear, so differences in volume will only be noticeable at the extremes (approximately 096-396 or 97% 100%).
Explanation / Answer
The answer contains three files
1. ioheader.h ( header file)
/*
* ioheader.h
*
* Created on: 03-Oct-2017
* Author: Tanmay
*/
#ifndef IOHEADER_H_
#define IOHEADER_H_
//modify these dummy LED registers with actual registers
//LED output registers
int RG_LED1;
int RG_LED2;
int RG_LED3;
int RG_LED4;
//Frequency output registers
int RG_FQ1046,RG_FQ1175,RG_FQ1319,RG_FQ1397;
//Switch pin input registers
int RG_P5,RG_P6,RG_P7,RG_P8;
// variable to remember release status
bool release; //
struct switch_pin{
int pin_p5;
int pin_p6;
int pin_p7;
int pin_p8;
}sw_pin,pre_pin;
enum LEDID{
LED1,
LED2,
LED3,
LED4
};
enum FREQ{
FREQ_1046,
FREQ_1175,
FREQ_1319,
FREQ_1397
};
// Functions prototypes
void InitializeVar(); // to initialise registers and input variables
void ReadSwitchPin(); //input function to read pin
void GlowLED(LEDID id); // output functions to glow LED
void PlayNote(FREQ fq); //output function to play note
void Delay(switch_pin pin); //delay function
#endif /* IOHEADER_H_ */
2. mbed.cpp (source file)
/*
* mbed.cpp
*
* Created on: 03-Oct-2017
* Author: Tanmay
*/
#include "ioheader.h"
#include <iostream>
using namespace std; //optional if debug print required
int main()
{
//This function will clear all register value
InitializeVar();
while(1)
{
ReadSwitchPin();
if(sw_pin.pin_p5==1)
{
GlowLED(LED1);
PlayNote(FREQ_1046);
release=true;
}
if(sw_pin.pin_p6==1)
{
GlowLED(LED2);
PlayNote(FREQ_1175);
release=true;
}
if(sw_pin.pin_p7==1)
{
GlowLED(LED3);
PlayNote(FREQ_1319);
release=true;
}
if(sw_pin.pin_p8==1)
{
GlowLED(LED4);
PlayNote(FREQ_1397);
release=true;
}
if(release==true)
{
Delay(pre_pin);
}
pre_pin=sw_pin;
}
return 0;
}
3. source.cpp (source file)
/*
* source.cpp
*
* Created on: 03-Oct-2017
* Author: Tanmay
*/
#include <string.h>
#include "ioheader.h"
// This function will glow the respective LED
void InitializeVar()
{
release=false;
RG_LED1=0;
RG_LED2=0;
RG_LED3=0;
RG_LED4=0;
RG_P5=0;
RG_P6=0;
RG_P7=0;
RG_P8=0;
memset(&sw_pin,0,sizeof(switch_pin));
memset(&pre_pin,0,sizeof(switch_pin));
}
void GlowLED(LEDID id)
{
if(id==LED1)
{
RG_LED1=100;
}
else if(id==LED2)
{
RG_LED2=100;
}
else if(id==LED3)
{
RG_LED3=100;
}
else if(id==LED4)
{
RG_LED4=100;
}
else
{
}
}
void PlayNote(FREQ fq)
{
if(fq==FREQ_1046)
{
RG_FQ1046=50;
}
else if(fq==FREQ_1175)
{
RG_FQ1175=50;
}
else if(fq==FREQ_1319)
{
RG_FQ1319=50;
}
else if(fq==FREQ_1397)
{
RG_FQ1397=50;
}
else
{
}
}
void ReadSwitchPin()
{
memset(&sw_pin,0,sizeof(switch_pin));
if(RG_P5>0) //modify this condition as per register value
{
sw_pin.pin_p5=1;
}
if(RG_P6>0) //modify this condition as per register value
{
sw_pin.pin_p6=1;
}
if(RG_P7>0) //modify this condition as per register value
{
sw_pin.pin_p7=1;
}
if(RG_P8>0) //modify this condition as per register value
{
sw_pin.pin_p8=1;
}
}
void Delay(switch_pin pin)
{
if(pin.pin_p5==1)
{
for(int i=50;i>0;i--)//modify the i value as per scheduling time
RG_FQ1046--;
}
if(pin.pin_p6==1)
{
for(int i=50;i>0;i--)//modify the i value as per scheduling time
RG_FQ1175--;
}
if(pin.pin_p7==1)
{
for(int i=50;i>0;i--)//modify the i value as per scheduling time
RG_FQ1319--;
}
if(pin.pin_p8==1)
{
for(int i=50;i>0;i--)//modify the i value as per scheduling time
RG_FQ1397--;
}
}
Note: Please let me know if you have any query.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.