Advertisement
pleasedontcode

"Sensor Control" rev_01

Dec 23rd, 2024
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: "Sensor Control"
  13.     - Source Code NOT compiled for: Arduino Uno
  14.     - Source Code created on: 2024-12-24 03:29:11
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* The project shall utilize Arduino libraries for */
  21.     /* efficient hardware control, ensuring modular code */
  22.     /* structure and ease of maintenance. It must support */
  23.     /* real-time data processing and user interaction */
  24.     /* through connected components. */
  25. /****** END SYSTEM REQUIREMENTS *****/
  26.  
  27. /* START CODE */
  28.  
  29. /****** DEFINITION OF LIBRARIES *****/
  30. #include <Arduino.h>
  31. #include <Wire.h>
  32. #include <SPI.h>
  33. #include <Servo.h>
  34.  
  35. /****** FUNCTION PROTOTYPES *****/
  36. void setup(void);
  37. void loop(void);
  38.  
  39. /****** GLOBAL VARIABLES *****/
  40. Servo myServo;  // Create a Servo object
  41. int sensorValue; // Variable to store sensor value
  42.  
  43. void setup(void)
  44. {
  45.     // Initialize serial communication for debugging
  46.     Serial.begin(9600);
  47.    
  48.     // Initialize the servo on pin 9
  49.     myServo.attach(9);
  50.    
  51.     // Initialize I2C communication
  52.     Wire.begin();
  53.    
  54.     // Initialize SPI communication
  55.     SPI.begin();
  56. }
  57.  
  58. void loop(void)
  59. {
  60.     // Read sensor value (for example, from an analog pin)
  61.     sensorValue = analogRead(A0);
  62.    
  63.     // Map the sensor value to a servo angle
  64.     int angle = map(sensorValue, 0, 1023, 0, 180);
  65.    
  66.     // Move the servo to the mapped angle
  67.     myServo.write(angle);
  68.    
  69.     // Print the sensor value and angle to the Serial Monitor
  70.     Serial.print("Sensor Value: ");
  71.     Serial.print(sensorValue);
  72.     Serial.print(" | Servo Angle: ");
  73.     Serial.println(angle);
  74.    
  75.     // Delay for a short period to allow the servo to move
  76.     delay(15);
  77. }
  78.  
  79. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement