Advertisement
Guest User

Arduino UNO code: blinking LEDS with Serial monitor user inp

a guest
Feb 7th, 2016
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.17 KB | None | 0 0
  1. /////////////////////////////////////////////////////////////////////////////////////
  2. - Arduino UNO code: blinking LEDS with Serial monitor user input
  3. /////////////////////////////////////////////////////////////////////////////////////
  4.  
  5. int greenLED=9; // Green led on pin 9
  6. int yellowLED=10; // Yellow led on pin 10
  7. int greenOnTime=25; // Amount of time that the green led is on
  8. int greenOffTime=100; // Amount of time that the green led is off
  9. int yellowOnTime=100; // Amount of time that the yellow led is on
  10. int yellowOffTime=100; // Amount of time that the yellow led is off
  11. int greenBlinks=10; // Amount of blinks for the green led
  12. int yellowBlinks=10; // Amount of blinks for the yellow led
  13. int randomReboot; // Simulate a fake reboot, just for the fun
  14. String greenMessage="The green LED is blinking!"; // String that will be printed if green led starts blinking
  15. String yellowMessage="The yellow LED is blinking!"; // String that will be printed if yellow led starts blinking
  16.  
  17. void setup() {
  18. pinMode(greenLED,OUTPUT); // Declare green pin function, it needs to blink, so its an output
  19. pinMode(yellowLED,OUTPUT); // Declare yellow pin function, it needs to blink, so its an output
  20.  
  21. Serial.begin(9600); // Opens a connection trough the USB cable
  22.  
  23. Serial.println("Made by Justin Manig"); // Welcome message
  24. Serial.println("Feel free to edit"); // Welcome message
  25. Serial.println(" "); // Welcome message
  26. }
  27.  
  28. void loop() {
  29.  
  30. Serial.println("How many times needs the green LED to blink?"); // Ask user for input green led
  31. while(Serial.available() == 0){}; // Wait untill user sends data
  32. greenBlinks=Serial.parseInt(); // If user sends data, parse the data to the greenBlinks int
  33.  
  34. Serial.println("And how long must the green led stay on?");
  35. while(Serial.available() == 0){};
  36. greenOnTime=Serial.parseInt();
  37.  
  38. Serial.println("How many times needs the yellow LED to blink?"); // Ask user for input yellow led
  39. while(Serial.available() == 0){}; // Wait until user sends data
  40. yellowBlinks=Serial.parseInt(); // If user sends data, parse the data to the yellowBlinks int
  41.  
  42. Serial.println("And how long must the yellow led stay on?");
  43. while(Serial.available() == 0){}
  44. yellowOnTime=Serial.parseInt();
  45.  
  46. Serial.println(greenMessage); // Show the string for the green led
  47. for(int j=1; j<greenBlinks; j++) // Loop trough this code until max blinks is reached
  48. {
  49. Serial.print("|"); // Print this text to the screen when green led blinks
  50. digitalWrite(greenLED,HIGH); // Let the green led blink
  51. delay(greenOnTime); // Time untill green led turns off
  52. digitalWrite(greenLED,LOW); // put the green led off
  53. delay(greenOffTime); // Time untill green or yellow led turns on
  54. }
  55.  
  56. Serial.println(" "); // If it's time to start with the yellow led, print a blank line
  57.  
  58. Serial.println(yellowMessage); // Show the string for the yellow led
  59. for(int j=1; j<yellowBlinks; j++) // Loop trough this code until max blinks is reached
  60. {
  61. Serial.print("|"); // Print this text to the screen when yellow led blinks
  62. digitalWrite(yellowLED,HIGH); // Let the yellow led blink
  63. delay(yellowOnTime); // Time untill yellow led turns off
  64. digitalWrite(yellowLED,LOW); // put the yellow led off
  65. delay(yellowOffTime); // Time untill yellow led turns on or rebooting simulation starts
  66. }
  67.  
  68. Serial.println(" "); // Print a new line when starting rebooting simulation
  69.  
  70. for(int j=10; j<100; j=j+10) // Simulate percentage, 10 to 100
  71. {
  72. randomReboot=random(10,500); // Random delay int to look more natural
  73.  
  74. Serial.print("Rebooting... "); // Print this text when simulating reboot
  75. Serial.print(j); // Print percentage when simulating reboot
  76. Serial.println("%"); // Print percentage char when simulating reboot
  77.  
  78. digitalWrite(greenLED,HIGH); // Turn the green led on
  79. digitalWrite(yellowLED,HIGH); // Turn the yellow led on
  80. delay(randomReboot); // When green and yellow leds are on, delay it with a random delay
  81. digitalWrite(greenLED,LOW); // Turn green led off
  82. digitalWrite(yellowLED,LOW); // Turn yellow led off
  83. delay(randomReboot); // hen green and yellow leds are off, delay it with a random delay
  84. }
  85.  
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement