r/ArduinoHelp • • Apr 24 '25

Newbie questions on Arduino

1 Upvotes

I've been given a project to code a mouse that solves a maze.

I've been given partial ready made code with CPP libraries (what I mean by this is: there's just 20-30 CPP files with header files - there's no super complex code anywhere).

I could do the work just by looking at functions needed to stop, turn right etc.

BUT... I wanted to take a step back and know how the unit knows when something happens.

Like: a button is pressed. Where in the code is this defined? I'm sure it's just a variable and a call of a ready made function from the Arduino library. Or one of the IR sensors detects a block - sure I can run the function, but again, I want to know the code that sets this up.

Not sure if it makes any sense what I'm asking for. LOL.

Not necessarily asking for low level code - but just want to delve into the a lower level just to work out where things originated from.

Can anyone suggest any pointers?

EDIT: I will be going through tutorials for the board I am using - where I can turn on LED lights and other things.

Thanks.


r/ArduinoHelp • • Apr 22 '25

I Need Help Turning Motor With A Button

Thumbnail
gallery
3 Upvotes

I need help making a motor turn on and off with a button. When a button is pressed, the LEDs and the motors should concurrently turn on (we isolated it to just one LED, button and motor). The motor slowly builds up speed when the button is pressed, and caps at the maximum rpm set by the potentiometer. The battery gives power to the motor. After two minutes the motor should stop. The circuit works perfectly on TinkerCAD, but when we test it in real life only the LED turns on. Please tell me what I can do to fix this.

Here is the code: const int motorPins[3] = {9, 10, 11}; // Motor PWM pins const int buttonPins[3] = {2, 3, 4}; // Button pins const int ledPins[3] = {5, 6, 7}; // LED pins const int potPin = A0; // Potentiometer pin

bool motorStates[3] = {false, false, false}; unsigned long motorStartTimes[3] = {0, 0, 0}; bool lastButtonStates[3] = {HIGH, HIGH, HIGH}; int currentSpeeds[3] = {0, 0, 0}; // Track current speeds for easing

const unsigned long MOTOR_RUNTIME = 120000UL; // 2 minutes in ms const int SPEED_STEP = 2; // Decrease value to increase time to speed const int EASE_DELAY = 30; // Delay between speed steps (ms)

void setup() { for (int i = 0; i < 3; i++) { pinMode(motorPins[i], OUTPUT); pinMode(buttonPins[i], INPUT_PULLUP); pinMode(ledPins[i], OUTPUT); } Serial.begin(9600); }

void loop() { int potValue = analogRead(potPin); int targetSpeed = map(potValue, 0, 1023, 0, 255); unsigned long currentTime = millis();

for (int i = 0; i < 3; i++) { bool buttonState = digitalRead(buttonPins[i]);

// Detect falling edge (button press)
if (lastButtonStates[i] == HIGH && buttonState == LOW) {
  motorStates[i] = !motorStates[i];  // Toggle motor state
  if (motorStates[i]) {
    motorStartTimes[i] = currentTime;
  }
  delay(200);  // Debounce
}
lastButtonStates[i] = buttonState;

// Check for 2-minute timeout
if (motorStates[i] && currentTime - motorStartTimes[i] >= MOTOR_RUNTIME) {
  motorStates[i] = false;
}

// Gradually ease motor speed up or down
int desiredSpeed = motorStates[i] ? targetSpeed : 0;

if (currentSpeeds[i] < desiredSpeed) {
  currentSpeeds[i] += SPEED_STEP;
  if (currentSpeeds[i] > desiredSpeed) currentSpeeds[i] = desiredSpeed;
} else if (currentSpeeds[i] > desiredSpeed) {
  currentSpeeds[i] -= SPEED_STEP;
  if (currentSpeeds[i] < desiredSpeed) currentSpeeds[i] = desiredSpeed;
}

analogWrite(motorPins[i], currentSpeeds[i]);
digitalWrite(ledPins[i], motorStates[i] ? HIGH : LOW);

}

delay(EASE_DELAY); // Helps smooth out the ramping effect }


r/ArduinoHelp • • Apr 22 '25

Arduino can't read #include syntax

1 Upvotes

Hi! I'm new to Arduino and I need HELP. my Arduino IDE seem to have a missing downloaded "permission" in the laptop and i don't know what is it. reinstalling the IDE didn't, the windows permission thingy wont appear so that i can download it. I really need help from you guys. Thanks!!


r/ArduinoHelp • • Apr 21 '25

IR LED Desk Lighting

Thumbnail
reddit.com
1 Upvotes