Question

Arduino PWM Example int Pin = 9; void setup() { pinMode (Pin, OUTPUT); } void loop() { analogWrite (Pin, 127); // Generate 50
TTT RESET DIGITAL Pata ICSP2 (UNO RX Arduino ICSP TERET POE ANALOG IN AS

Using the template provided in the lab details section, formulate a program for the Arduino to generate a PWM signal duty cycles between 0% and 100% at intervals of 10% where a button will increment the PWM signal. Remember the RC is 10 milliseconds. Provide a copy of the final code and explain how it works.


Template for Code: // Declare and Initialize the Variables #define Button 4 // Pin 4 is named Button #define PWMsignal 11 /
case 0: // Sets duty cycle to 0% analogWrite(PWMsignal,0); break; case 1: // Sets duty cycle to 10% analogWrite(WPWMsignal,25
0 0
Add a comment Improve this question Transcribed image text
Answer #1

//code:--
#define Button 4
#define PWMsignal 11
int DutyCycle=0;
boolean pressed=LOW;
void setCycle(int);
void setup()
{
Serial.begin(9600);
pinMode(Button,INPUT);
pinMode(PWMsignal, OUTPUT);
}

void loop()
{
pressed=digitalRead(Button);
if(pressed==HIGH)
{
DutyCycle++;//this statement will increment the dutycycle
if(DutyCycle==11)
{
DutyCycle=0;
}
setCycle(DutyCycle);
Serial.print("the duty cycle is ");
Serial.print(DutyCycle);
Serial.print("0%\n");
delay(500);
}
}

void setCycle(int Cycle)//function for to generate pwm signal
{
switch(Cycle)
{
case 0:
analogWrite(PWMsignal,0);
break;
case 1:
analogWrite(PWMsignal,25);//here 10% of 255 =25.5 (let us approximate it to 25
break; // so here we have to increment the analog value by 25
case 2:
analogWrite(PWMsignal,50);//incrementing analog value by 25
break;
case 3:
analogWrite(PWMsignal,75);//incrementing analog value by 25
break;
case 4:
analogWrite(PWMsignal,100);//incrementing analog value by 25
break;
case 5:
analogWrite(PWMsignal,125);//incrementing analog value by 25
break;
case 6:
analogWrite(PWMsignal,150);//incrementing analog value by 25
break;
case 7:
analogWrite(PWMsignal,175);//incrementing analog value by 25
break;
case 8:
analogWrite(PWMsignal,200);//incrementing analog value by 25
break;
case 9:
analogWrite(PWMsignal,225);//incrementing analog value by 25
break;
case 10:
analogWrite(PWMsignal,255);
break;
default :
analogWrite(PWMsignal,0);
break;
}
  
}
  
  
  
  
  
  
  
  

Simulator time: 00:00:10.470 in Code Stop Simulation Export Share 1 Text + 1 (Arduino Uno R3) { Serial.begin(9600); pinMode(B

working of the circuit:--
here the circuit will continuously monitor whether the button is pressed or not if the button is pressed then the duty cycle will be incremented by "1", then the setCycle() function is used to determine which value to be written in the analogWrite function to produce PWM signal at 11th pin.
if the duty cycle is reached 10 then it is again set to "0" by using a simple "if statement".
in setCycle() function a switch statement is used to produce different duty cycle of the PWM signal, if the duty cycle is 10% then an analog value 25 will be written at the 11th pin, if the duty cycle is 20% then the analog value 50 will be written at the 11th pin, so here different
analog value will be written at the 11th pin depending upon the duty cycle.
here we need to increment the duty cycle of the PWM signal by 10% when each time the button is pressed, the Arduino PWM signal value range is 0 to 255, so 10 percent of 255 is the value we have to increment in the analog write function as shown in the code.
so 10 percent of 255 will be
10*255/100=25.5
= let us take 25 as approximated value
so when the button is pressed then the analog value is incremented by a factor of 25 in the switch statement.

Add a comment
Know the answer?
Add Answer to:
Using the template provided in the lab details section, formulate a program for the Arduino to...
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
  • 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 have a program for Arduino written such that when I hold a button down, it...

    I have a program for Arduino written such that when I hold a button down, it will turn an LED off. I want to be able to print to the serial monitor how long the button was pressed for after I let go of the button. Does anybody have any ideas? Below is the code I have written thus far: Text of Code: #define LED RED_LED #define buttonPIN PUSH2 const int buttonPin = PUSH2; // the number of the pushbutton...

  • Lab 1-Lab Portion Part I Copy the Blink program from Figure 1-13 (page 17) in the textbook and run it on your Arduino. After verifying that the Blink program works, modify the program to make the...

    Lab 1-Lab Portion Part I Copy the Blink program from Figure 1-13 (page 17) in the textbook and run it on your Arduino. After verifying that the Blink program works, modify the program to make the on-board LED attached to pin #13 blink out "SOS" in Morse code. Use a 2 750 millisecond for long pulses. 50 millisecond for short pulses and a Part 2, Write a program that uses two pushbuttons to increase or decrease the brightness of the...

  • In the code below i am using a LS-3006 SERVO on the arduino uno and everytime...

    In the code below i am using a LS-3006 SERVO on the arduino uno and everytime i hit the pushbutton to input a time the servo comes on. Would anyone be able to tell me what a solution is to this problem? Thanks #include <Wire.h> #include <DS3231.h> #include <Servo.h> #include <LiquidCrystal.h> #include <Keypad.h> const byte ROWS = 4; // # of rows const byte COLS = 4; // # of columns // Define the Keymap char keys[ROWS][COLS] = { {'1','2','3','A'},...

  • Program is for an Arduino 1. Add the letters “A”, “b”, “c”, “d”, “E”, “F” to...

    Program is for an Arduino 1. Add the letters “A”, “b”, “c”, “d”, “E”, “F” to your homework program. Continually display “0123”, “4567”, “89Ab”, “cdEF” with a one second delay between each one. HINT: Expand pickNumber() to use numbers 10-15 (a-f in hex). Create new functions for a-f. (THIS IS THE CODE FROM THE HOMEWORK) /*************************************** name:Stopwatch function: you can see the number increases by one per second on the 4-digit 7-segment display. ***********************************/ //Email:[email protected] //Website:www.sunfounder.com /**************************************/ #include //the pins...

  • It's an Energia sketch using C language. We work with MSP430 Launchpad but you shouldn't need...

    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 :( Problem 2 (50 points) One common method of outputting an "effective" analog signal is to use pulse width modulation or PWM. In this modulation scheme, a high frequency square wave is switched on and off with a duty cycle determined by the desired output intensity, where Duty Cycle = Time spend "on" Total period 50%...

  • What is the delay time used in the program? (i.e., how long in seconds does the value of the puls...

    What is the delay time used in the program? (i.e., how long in seconds does the value of the pulsewidth remain constant before incrementing or decrementing?) 9int GREENPin- 10;I GREEN pin of the LED to PWM pin 10 10 it state = 0; 11 int delTime-50; 13 void dim (int gMin, int gMax, int dTime) 15 void setup 16 7 pinMode(GREENPin, OUTPUT) 18 Serial.begin(9600); 19 21 void loop 23 analogWrite (GREENPin, 0) start from Green LED OFF 25 / Dim...

  • Java BlackJack Game: Help with 1-4 using the provided code below Source code for Project3.java: import...

    Java BlackJack Game: Help with 1-4 using the provided code below Source code for Project3.java: import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.io.*; public class Project3 extends JFrame implements ActionListener { private static int winxpos = 0, winypos = 0; // place window here private JButton exitButton, hitButton, stayButton, dealButton, newGameButton; private CardList theDeck = null; private JPanel northPanel; private MyPanel centerPanel; private static JFrame myFrame = null; private CardList playerHand = new CardList(0); private CardList dealerHand = new CardList(0);...

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