Created
November 26, 2025 14:51
-
-
Save psychemist/d8e89fa7e733d84ad3dde0ce7c869873 to your computer and use it in GitHub Desktop.
Arduino Starter Project 4
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // set up constants for input and output pins | |
| const int redLEDPin = 9; | |
| const int greenLEDPin = 10; | |
| const int blueLEDPin = 11; | |
| const int redSensorPin = A0; | |
| const int greenSensorPin = A1; | |
| const int blueSensorPin = A2; | |
| // add variables for incoming sensor and output values | |
| int redValue = 0; | |
| int greenValue = 0; | |
| int blueValue = 0; | |
| int redSensorValue = 0; | |
| int greenSensorValue = 0; | |
| int blueSensorValue = 0; | |
| void setup() { | |
| // begin communication at 9600bps | |
| Serial.begin(9600); | |
| // define LED pins as outputs | |
| pinMode(redLEDPin, OUTPUT); | |
| pinMode(greenLEDPin, OUTPUT); | |
| pinMode(blueLEDPin, OUTPUT); | |
| } | |
| void loop() { | |
| redSensorValue = analogRead(redSensorPin); | |
| delay(5); | |
| greenSensorValue = analogRead(greenSensorPin); | |
| delay(5); | |
| blueSensorValue = analogRead(blueSensorPin); | |
| // print out sensor values on one line | |
| Serial.print("Raw Sensor Values \t RED: "); | |
| Serial.print(redSensorValue); | |
| Serial.print("\t GREEN: "); | |
| Serial.print(greenSensorValue); | |
| Serial.print("\t BLUE "); | |
| Serial.print(blueSensorValue); | |
| // convert sensor reading from value between 0-1023 to value between 0-255 | |
| redValue = redSensorValue / 4; | |
| greenValue = greenSensorValue / 4; | |
| blueValue = blueSensorValue / 4; | |
| // print mapped sensor values to serial monitor | |
| Serial.print("Mapped Sensor Values \t RED: "); | |
| Serial.print(redValue); | |
| Serial.print("\t GREEN: "); | |
| Serial.print(greenValue); | |
| Serial.print("\t BLUE: "); | |
| Serial.print(blueValue); | |
| // change LED's brightness via PWM | |
| analogWrite(redLEDPin, redValue); | |
| analogWrite(greenLEDPin, greenValue); | |
| analogWrite(blueLEDPin, blueValue); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment