Question

Problem 2 (50 points) One common method of outputting an effective analog signal is to use pulse width modulation or PWM. Ifrequency must be high enough that the switching is imperceptible. Suggested values for the total period are on the order o

It's an Energia sketch using C language. We work with MSP430 Launchpad but you shouldn't need it to write the code.

I'm really stuck :(

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please check the code. It is compiling successfully. I hope it works fine on Arduino or MSP430 launchpad. The code has been written with Arduino in mind. All the necessary comments are there for easy understanding. And Modify it accordingly if anything needs to be changed. Thanks :)

CODE:


//PIN declarations for input buttons and output LED. Change these according to your circuit connections
int Button1 = 8;   
int Button2 = 9;
int ledPin = 11;

//VARIABLES REQUIRED FOR DEBOUNCING
boolean last1=LOW;
boolean last2=LOW;
boolean current1 = LOW;
boolean current2 = LOW;

boolean state = HIGH; //TO know ON/OFF state of led

//LED LEVELS FOR 3 DIFFERENT INTENSITIES
int low = 50;
int med = 150;
int high = 255;

int ledLevel = med; // Default level on starting is medium
int lastLevel = med; //To store the last ON-level of LED before switching it OFF by button2

String str = ""; //To check Keyboard Control and Button Control

void setup(){
Serial.begin(9600);
pinMode(Button1,INPUT);
pinMode(Button2,INPUT);
pinMode(ledPin,OUTPUT);
analogWrite(ledPin,ledLevel);
}

void changeBrightness(){
if(ledLevel == low){
ledLevel = med;
}
else if(ledLevel == med){
ledLevel = high;
}
else if(ledLevel == high){
ledLevel = low;
}
}

void buttonControl(){
current1=digitalRead(Button1);
current2=digitalRead(Button2);
  
if(last1 != current1){ //CHECKING BUTTON-1
delay(5); //DEBOUNCING DELAY
current1=digitalRead(Button1);
if(state == HIGH && current1== HIGH && last1 == LOW)
{
changeBrightness();
analogWrite(ledPin,ledLevel);
last1 = current1;
}
}
  
if(last2 != current2){ //CHECKING BUTTON-2
delay(5); //DEBOUNCING DELAY
current2=digitalRead(Button2);

if(current2== HIGH && last2 == LOW){
if(state == HIGH){
lastLevel = ledLevel;
ledLevel = 0;
state = LOW;
}
else{
ledLevel = lastLevel;
state = HIGH;
}
analogWrite(ledPin,ledLevel);
last2 = current2;
}
}
}

void keyboardControl(){
String s = "";
while(Serial.available()>0){
s = Serial.readString();
}
  
if(state == HIGH && s == "Change Brightness"){   
changeBrightness();
}
  
else if(state == HIGH && s == "TURN OFF"){   
lastLevel = ledLevel;
ledLevel = 0;
state = LOW;
}
else if(state == LOW && s == "TURN ON"){
ledLevel = lastLevel;
state = HIGH;
}
analogWrite(ledPin,ledLevel);   
  
}

void loop(){
while(!Serial.available()){
buttonControl();
}
str = Serial.readString();
while(str == "Keyboard Control"){
keyboardControl();
if(Serial.available()){
if(Serial.readString()== "Button Control"){
str = "Button Control";
}
}
}
}

Add a comment
Know the answer?
Add Answer to:
It's an Energia sketch using C language. We work with MSP430 Launchpad but you shouldn't need...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Could you please show me how I can drw those circuits. please using (NI Multisim 14)...

    Could you please show me how I can drw those circuits. please using (NI Multisim 14) Optocoupler Objectives Use an ohmmeter to determine the condition of the optoisolator. Observe the operation of an optocoupler. Determine the maximum frequency response of the optocoupler. Required Materials (1) Dual DC power supply (1) Function generator (1) Oscilloscope (2) Multimeters (1) Optocoupler (ECG3040) (1) 3.9ΚΩ resistor (1) 220 resistor Introduction An optoisolator is a hybrid integrated circuit that contains an LED on one side...

  • I am doing an Arduino Uno project where I made a "Simon says" memory game with 3 neopixel LED str...

    I am doing an Arduino Uno project where I made a "Simon says" memory game with 3 neopixel LED strips and 3 - ultrasonics. I have them working independently but I need to combine the code so they work together. Here is what I have: Memory Game #define PLAYER_WAIT_TIME 2000 // The time allowed between button presses - 2s byte sequence[100]; // Storage for the light sequence byte curLen = 0; // Current length of the sequence byte inputCount =...

  • I need Summary of this Paper i dont need long summary i need What methodology they used , what is the purpose of this...

    I need Summary of this Paper i dont need long summary i need What methodology they used , what is the purpose of this paper and some conclusions and contributes of this paper. I need this for my Finishing Project so i need this ASAP please ( IN 1-2-3 HOURS PLEASE !!!) SPECIAL ARTICLES tole of Monetary Policy C Rangarajan What should be the objectives of monetary policy? Does the objective of price stability conflict with the goal of achieving...

  • Summary should briefly analyze the central problems and issues of the case and provide some analysis...

    Summary should briefly analyze the central problems and issues of the case and provide some analysis and suggestions. Thank you. Lean Initiatives and Growth at Orlando Metering Company It was late August 2002 and Ed Cucinelli, vice president of Orlando Metering Company (OMC), sat in his office on a late Saturday morning. He had come in to prepare for some strategic planning meetings that were scheduled for the upcoming week. As he noticed the uncommon silence in the building, Ed...

  • How can we assess whether a project is a success or a failure? This case presents...

    How can we assess whether a project is a success or a failure? This case presents two phases of a large business transformation project involving the implementation of an ERP system with the aim of creating an integrated company. The case illustrates some of the challenges associated with integration. It also presents the obstacles facing companies that undertake projects involving large information technology projects. Bombardier and Its Environment Joseph-Armand Bombardier was 15 years old when he built his first snowmobile...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT