top of page
Search

Audrino Programming

  • Writer: Aiden Lim
    Aiden Lim
  • Nov 25, 2024
  • 5 min read

TASK 1

Video link used:




I will describe 'Interface a LDR to Maker UNO board and measure/show its signal in serial monitor Arduino IDE'


Code i used:

int sensorPin = A0; // Sensor connected to analog pin A0

int ledPin = 13; // LED connected to digital pin 13

int sensorValue = 0; // Variable to store the sensor value


void setup() {

pinMode(ledPin, OUTPUT); // Set the LED pin as output

Serial.begin(9600); // Initialize Serial communication

}


void loop() {

int sensorVal = digitalRead(2); // Read digital value from pin 2

Serial.println(sensorVal); // Print digital value to Serial Monitor


sensorValue = analogRead(sensorPin); // Read analog value from the sensor

digitalWrite(ledPin, HIGH); // Turn the LED ON

delay(sensorValue); // Delay based on sensor value

digitalWrite(ledPin, LOW); // Turn the LED OFF

delay(sensorValue); // Delay based on sensor value

}



Explain of codes:

Variables;

  • sensorPin is assigned to A0, where the sensor is connected.

  • ledPin is assigned to pin 13, where an LED is connected.

  • sensorValue stores the analog sensor reading

Setup Function:

  • The LED pin is set as an output using pinMode.

  • Serial communication is started at 9600 baud rate for debugging and monitoring values.

Loop Function:

  • The code reads a digital signal from pin 2 using digitalRead and prints it to the Serial Monitor.

  • An analog sensor value is read using analogRead and assigned to sensorValue.

  • The LED is turned ON using digitalWrite, and the duration of the ON state is determined by the sensorValue.

  • The LED is then turned OFF, with the OFF duration also based on the sensor value.


This program adjusts the LED blinking rate according to the sensor input, allowing it to reflect changes in the sensor's value.


Used sources; Youtube video provided on top, shows the use of tinkerboard and how it is suppose to be connected. Used information on brightspace to complete coding and upload. CHatGPT was used to check for coding errors.


Problems Encountered and Fixes

Problem 1: LED Not Blinking

  • Cause: Incorrect wiring of the LED, such as reversing the anode and cathode.

  • Fix: Double-checked the circuit and ensured the longer leg (anode) was connected to the positive terminal and the cathode to GND via a resistor.

Problem 2: Unstable Sensor Readings

  • Cause: Noise in the analog sensor input.

  • Fix: Added a delay after each analogRead to stabilize readings and reduce rapid fluctuations.

Problem 3: Serial Monitor Not Displaying Values

  • Cause: Serial communication baud rate mismatch between code and Serial Monitor.

  • Fix: Ensured that the Serial Monitor baud rate was set to 9600 to match Serial.begin(9600) in the code.


    This video to prove that i have succeeded.


  • The LED blinks with a delay that changes based on the sensor's value.

  • The Serial Monitor displays the sensor's digital and analog readings in real-time.




TASK 2:


Code used;

// Pin assignments for LEDs

const int redLED = 9;

const int yellowLED = 11;

const int greenLED = 10;


// Pushbutton pin

const int buttonPin = 2;


// Variables to track button state and LED activity

int buttonState = 0; // Current state of the button

bool isFading = false; // Tracks if LEDs are fading


void setup() {

// Set LED pins as OUTPUT

pinMode(redLED, OUTPUT);

pinMode(yellowLED, OUTPUT);

pinMode(greenLED, OUTPUT);


// Set pushbutton pin as INPUT

pinMode(buttonPin, INPUT_PULLUP); // Using internal pull-up resistor


// Initialize Serial Monitor

Serial.begin(9600);

}


void loop() {

// Read the pushbutton state

buttonState = digitalRead(buttonPin);


// Toggle fading state if button is pressed

if (buttonState == LOW) { // Button pressed (active LOW)

delay(100); // Debounce delay

isFading = !isFading; // Toggle the fading state

Serial.println(isFading ? "LEDs Fading" : "LEDs Stopped");

}


// If fading is enabled, fade LEDs in sequence

if (isFading) {

fadeLED(redLED);

fadeLED(yellowLED);

fadeLED(greenLED);

}

}


// Function to fade an LED

void fadeLED(int ledPin) {

// Gradually increase brightness

for (int brightness = 0; brightness <= 255; brightness += 5) {

analogWrite(ledPin, brightness);

delay(20); // Delay to control fading speed

}

// Gradually decrease brightness

for (int brightness = 255; brightness >= 0; brightness -= 5) {

analogWrite(ledPin, brightness);

delay(20); // Delay to control fading speed

}

}


Explain of code:


Variables

  • LED Pins: The red, yellow, and green LEDs are assigned to specific pins on the Arduino for control.

  • Pushbutton Pin: A pin is assigned to the pushbutton input to detect presses.

  • State Variables:

    • Tracks the pushbutton's current state (pressed or not).

    • Determines whether the LEDs are fading or stationary.

setup() Function

  • Configures the LED pins as outputs to control the LEDs.

  • Sets the pushbutton pin as an input with a pull-up resistor to ensure reliable readings.

  • Initializes the Serial Monitor to print messages for debugging and monitoring.

loop() Function

  • Pushbutton State Detection: Continuously reads the pushbutton state to check if it has been pressed.

  • State Toggling: Switches between the fading mode and stopping the LEDs based on the button press.

  • LED Fading: When enabled, each LED fades in and out sequentially in a smooth pattern using a custom function.

  • Serial Output: Sends messages to the Serial Monitor indicating the current state of the LEDs (fading or stopped).


Used sources; Youtube video provided on top, shows the use of tinkerboard and how it is suppose to be connected. Used information on brightspace to complete coding and upload. ChatGPT was used to check for coding errors

Problem: Pushbutton Not Responding

  • Issue: The pushbutton did not toggle the fading state when pressed.

  • Cause: The pushbutton pin was floating, leading to unreliable readings.

  • Fix: Configured the pin with an internal pull-up resistor using INPUT_PULLUP in pinMode, ensuring stable HIGH (not pressed) and LOW (pressed) states.

Problem: Flickering During Fading

  • Issue: LEDs flickered or behaved inconsistently during fading transitions.

  • Cause: Insufficient delays in the fade function caused rapid changes in brightness, making it appear erratic.

  • Fix: Added appropriate delay (delay(15)) during the brightness transitions to smoothen the fade effect.

Problem: Button Presses Triggered Multiple Actions

  • Issue: A single button press caused multiple toggles of the fading state.

  • Cause: Button bouncing caused multiple quick state changes.

  • Fix: Implemented a debounce delay (delay(200)) after detecting a button press to ensure stable toggling.


This video to prove work:



In summary, Throughout the Arduino programming exercises, I received hands-on experience connecting and setting input devices like as potentiometers and pushbuttons, as well as understanding the need of reliable readings through the use of pull-up resistors and debounce techniques. I also enhanced my coding skills by utilizing functions, conditional expressions, and analog and digital I/O to control devices. Writing the LED fading software taught me how to use PWM and timing methods to achieve seamless transitions. Troubleshooting issues like as floating pins and button debounce helped me improve my problem-solving skills, and this experience underlined the necessity of proper hardware setup, debugging, and explicit software logic while developing interactive systems.





 
 
 

Comments


bottom of page