Week 3

microcontroller

For the microcontroller week, I decided to explore lighting up the led with button press.

I wanted to make a RGB button press system to light up the onboard led to the respective color. Since I am a very novice programmer, I first, as advised in the recitation, asked chatgpt to write me a code with the following prompt:

And this is the generated code:


									
								
#include <Adafruit_NeoPixel.h>

#define PIXEL_PIN 25  // Onboard LED pin
#define PIXEL_COUNT 1

Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, NEO_GRB + NEO_KHZ800);

const int redButtonPin = 2;
const int greenButtonPin = 3;
const int blueButtonPin = 4;

void setup() {
	strip.begin();
	strip.show(); // Initialize the onboard LED to 'off'

	pinMode(redButtonPin, INPUT_PULLUP);
	pinMode(greenButtonPin, INPUT_PULLUP);
	pinMode(blueButtonPin, INPUT_PULLUP);
}

void loop() {
	if (digitalRead(redButtonPin) == LOW) {
		// Red button is pressed
		setColor(255, 0, 0); // Set RGB LED to red
	} else if (digitalRead(greenButtonPin) == LOW) {
		// Green button is pressed
		setColor(0, 255, 0); // Set RGB LED to green
	} else if (digitalRead(blueButtonPin) == LOW) {
		// Blue button is pressed
		setColor(0, 0, 255); // Set RGB LED to blue
	} else {
		// No button is pressed, turn off the RGB LED
		setColor(0, 0, 0);
	}
}

void setColor(int red, int green, int blue) {
	strip.setPixelColor(0, strip.Color(red, green, blue));
	strip.show();
}

I tried to be specific so that the AI does not overlook any details. I connected the pins as proposed by the AI and tested the system. It did not work.

After consulting some experts, we discovered that the code written by chatgpt wrote const int (the pins) as 1, 2, 3 for each button input but it seems Seeed rp2040 needs to label analog and digital so it must be written as: D1,D2,D3.


this is the revised code.


									
								
#include 

#define PIXEL_PIN 12
#define PIXEL_COUNT 1

Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, NEO_GRB + NEO_KHZ800);

const int redButtonPin = D1;
const int greenButtonPin = D2;
const int blueButtonPin = D3;

void setup() {
strip.begin();
strip.show(); // Initialize all pixels to 'off'

pinMode(11,OUTPUT);
digitalWrite(11,HIGH);


pinMode(D1, INPUT_PULLUP);
pinMode(greenButtonPin, INPUT_PULLUP);
pinMode(blueButtonPin, INPUT_PULLUP);

Serial.begin(9600);
}

void loop() {
if (digitalRead(D1) == LOW) {
//   // Red button is pressed


setColor(255, 0, 0); // Set RGB LED to red
} else if (digitalRead(greenButtonPin) == LOW) {
// Green button is pressed
setColor(0, 255, 0); // Set RGB LED to green
} else if (digitalRead(blueButtonPin) == LOW) {
// Blue button is pressed
setColor(0, 0, 255); // Set RGB LED to blue
} else {
// No button is pressed, turn off the RGB LED
setColor(0, 0, 0);
}
Serial.println(digitalRead(D1));

}

void setColor(int red, int green, int blue) {
strip.setPixelColor(0, strip.Color(red, green, blue));
strip.show();
}

After resolving this problem, the program works as expected: