Question

In: Computer Science

Write the following in C language for Arduino: Write a program that increases the LED brightness...

Write the following in C language for Arduino: Write a program that increases the LED brightness in five steps with each press of the button. Remember you are going to using a digital input and the "digitalRead" command. You are going to need to use a PWM pin and use the "analogWrite" command. The maximum value for our Arduino R3 boards is 255 and you need five steps (25%, 50%, 75%, 100%, and 0%) so you will need to determine the values for each step.

Solutions

Expert Solution

Source Code:

const int buttonPin = 2;           // number of the button pin
const int ledPin = 9;              // number of the LED pin

float brightness = 0;              // brightness of LED starts at 0
int fadeAmount = 51;               // fadeAmount is equal to 20% of 255(max LED brightness)
float buttonPressedCounter = 0.0;  // timer set to 0 whe depressing button
int buttonState = 0;


void setup() {
 // set button as input
 pinMode(buttonPin, INPUT);
 // set LED as output
 pinMode(ledPin, OUTPUT);
 // init serialize communication at 9600 bits per second
 Serial.begin(9600);
}

void loop() {
 // activate when button is being held down
 if (buttonState == HIGH) {
   // on press, when counter is greater than 33, assume button is being held down
   if (buttonPressedCounter > 33) {
     // button was held down for longer than 33
     if (brightness < 255) {
       Serial.print("increasing brightness: ");
       Serial.println(brightness);
       brightness = brightness + 5;
       analogWrite(ledPin, brightness);
     }
   } else {
     buttonPressedCounter ++;
     Serial.print("holding: ");
     Serial.println(buttonPressedCounter);
   }
 } else if (buttonState == LOW) {
   // on release if button pressed counter was less than 33, assume it was a short press
   if ((buttonPressedCounter < 33) & (buttonPressedCounter != 0)) {
     Serial.print("lower LED: ");
     if (brightness - fadeAmount < 0.0) {
       brightness = 0;
     } else {
       brightness = brightness - fadeAmount;
     }
     Serial.println(brightness);
     analogWrite(ledPin, brightness);
   }
   buttonPressedCounter = 0;
 }
}
Let me know if you have any doubts or if you need anything to change. 

If you are satisfied with the solution, please leave a +ve feedback : ) Let me know for any help with any other questions.

Thank You!
===========================================================================

Related Solutions

Write the following in C language for Arduino: Write a program that turns on the LED...
Write the following in C language for Arduino: Write a program that turns on the LED at 25%, 50%, 75%, 100%, and then 0% brightness with a one second delay in between each change. Remember you are going to need to use a PWM pin and use the "analogWrite" command. The maximum value for our Arduino R3 boards is 255 and you need five steps (25%, 50%, 75%, 100%, and 0%) so you will need to determine the values for...
1. IN C LANGUAGE: Write a code that makes the MSP430 blink the red LED and...
1. IN C LANGUAGE: Write a code that makes the MSP430 blink the red LED and the green LED at the same time. The two LEDs should be on at the same time and then off at the same time
Write an assembly language program that corresponds to the following C program ****Please give correct answer...
Write an assembly language program that corresponds to the following C program ****Please give correct answer using Pep/9 machine**** int num1; int num2; ;int main () { scanf("%d", &num1); num2 = -num1; printf("num1 = %d\n", num1); printf("num2 = %d\n", num2); return 0; }
Write code in C language in the Arduino IDE ADC data using Serial plotter Serial plot...
Write code in C language in the Arduino IDE ADC data using Serial plotter Serial plot : raw data, delay data (int) Purpose: Delay Block (**Using Class**) Input : u(t) Output : o(t)=u(t-h) sample time=0.02 Delay (h) = 0.4
Write a C Program that uses file handling operations of C language. The Program should perform...
Write a C Program that uses file handling operations of C language. The Program should perform following operations: 1. The program should accept student names and students’ assignment marks from the user. 2. Values accepted from the user should get saved in a .csv file (.csv files are “comma separated value” files, that can be opened with spreadsheet applications like MS-Excel and also with a normal text editor like Notepad). You should be able to open and view this file...
C# Programming Language Write a C# program ( Console or GUI ) that prompts the user...
C# Programming Language Write a C# program ( Console or GUI ) that prompts the user to enter the three examinations ( test 1, test 2, and test 3), homework, and final project grades then calculate and display the overall grade along with a message, using the selection structure (if/else). The message is based on the following criteria: “Excellent” if the overall grade is 90 or more. “Good” if the overall grade is between 80 and 90 ( not including...
C LANGUAGE ONLY Write a C program to count the frequency of each element in an...
C LANGUAGE ONLY Write a C program to count the frequency of each element in an array. Enter the number of elements to be stored in the array: 3 Input 3 elements of the array: element [0]: 25 element [1]: 12 element [2]: 43 Expected output: The frequency of all elements of an array: 25 occurs 1 times 12 occurs 1 times 3 occurs 1 times
In C programming language, write the program "3x3" in size, calculating the matrix "c = a...
In C programming language, write the program "3x3" in size, calculating the matrix "c = a * b" by reading the a and b matrices from the outside and writing on the screen?
ONLY IN C LANGUAGE Write a C program to print all the unique elements of an...
ONLY IN C LANGUAGE Write a C program to print all the unique elements of an array. Print all unique elements of an array Enter the number of elements to be stored in the array: 4 Input 4 elements in the arrangement: element [0]: 3 element [1]: 2 element [2]: 2 element [3]: 5 Expected output: The only items found in the array are: 3 5
arduino programing Question 1: write an APL program that will print the following output on the...
arduino programing Question 1: write an APL program that will print the following output on the serial monitor 10 times only.   Output:  1 2 3 4 5 Question 2: write an APL program that will turn the traffic lights on and off by taking input from the Serial port working at 11500 bits processing rate. Use r or R for RED LIGHTS, y or Y for YELLOW LIGHTS and g or G for GREEN LIGHTS.    Question 3: write an APL...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT