What's new

C & C++ Pa help arduino code

Here is a sample code for creating a bouncing ball effect on an LED connected to an Arduino board:

Code:
const int LED_PIN = 9;
const int MAX_BOUNCE_HEIGHT = 9;
int currentHeight = 0;
int direction = 1;

void setup() {
  pinMode(LED_PIN, OUTPUT);
}

void loop() {
  digitalWrite(LED_PIN, HIGH);
  delay(100);

  // Move the ball down
  for (int i = 0; i < MAX_BOUNCE_HEIGHT; i++) {
    digitalWrite(LED_PIN, LOW);
    delay(100);
  }

  // Make the ball bounce back up
  for (int i = MAX_BOUNCE_HEIGHT; i > 0; i--) {
    digitalWrite(LED_PIN, LOW);
    delay(100);
  }

  // Adjust the bounce height
  if (currentHeight == 0) {
    direction = 1;
  } else if (currentHeight == MAX_BOUNCE_HEIGHT) {
    direction = -1;
  }
  currentHeight += direction;
}

In this code, we start by defining a few constants and variables. LED_PIN is the pin number on which the LED is connected, and MAX_BOUNCE_HEIGHT is the maximum height that the ball will bounce to. currentHeight and direction are used to keep track of the current height of the ball and the direction in which it is moving.

In the setup() function, we set the LED_PIN as an output pin.

In the loop() function, we first turn on the LED by setting the LED_PIN to HIGH, and then wait for a short period of time using the delay() function.

Next, we move the ball down by turning off the LED and waiting for a short period of time, using a for loop to control how far down the ball should move.

After the ball has reached the bottom, we use another for loop to make the ball bounce back up. We again use a for loop to control how far up the ball should bounce.

Finally, we adjust the bounce height by checking whether the ball has reached the top or the bottom of its bounce, and changing the direction accordingly. We then update the currentHeight variable to reflect the new height of the ball.

Overall, this code should create a bouncing ball effect on the LED, with the ball bouncing up and down a decreasing height with each bounce.
using arduino Make a code for bouncing ball effect by making the LED start at one end,
ʻdropʼ toward the other end, bounce back up, but to only go up 9
spaces, bounce, go up 8 spaces, then 7, then 6, etc. To give the
effect it is a bouncing ball, getting bouncing up to a lower height on
each bounce
 
Arduino code help c++

Make a bouncing ball effect by turning the LEDs so they are vertical, then make an LED start at the bottom, then “bounce” up to the top LED, then back to the bottom, then only up to the 9th LED, then back down, then up to the 8th, and so on to simulate a bouncing ball losing momentum after each bounce.
 
Here is an example code that demonstrates the bouncing ball effect with LEDs connected in a vertical line on the Arduino board:

Code:
const int LED_PIN[] = {9, 10, 11, 12, 13}; // LEDs connected to these pins
const int NUM_LEDS = sizeof(LED_PIN) / sizeof(LED_PIN[0]); // number of LEDs

void setup() {
  for (int i = 0; i < NUM_LEDS; i++) {
    pinMode(LED_PIN[i], OUTPUT); // set LED pins as output
  }
}

void loop() {
  for (int i = 0; i < NUM_LEDS; i++) {
    digitalWrite(LED_PIN[i], LOW); // turn off all LEDs
  }

  for (int i = 0; i < NUM_LEDS; i++) {
    digitalWrite(LED_PIN[i], HIGH); // turn on bottom LED
    delay(150); // wait for a short time

    digitalWrite(LED_PIN[i], LOW); // turn off bottom LED
    digitalWrite(LED_PIN[NUM_LEDS - 1 - i], HIGH); // turn on top LED
    delay(150); // wait for a short time
  }
}

The code uses an array to store the pin numbers of the LEDs and loops through them to turn them on and off in sequence. The delay function is used to create a pause between each change of LED state.

To simulate the bouncing ball effect, we turn on the bottom LED, wait a short time for it to appear, and then turn it off again. Then we turn on the top LED and wait another short time. We then repeat this process, but for each iteration, we light up one less LED, causing the "ball" to lose momentum and bounce less high each time.

Note that the delay time can be adjusted to control the speed of the bouncing ball effect.
Here is an example code that demonstrates the bouncing ball effect with LEDs connected in a vertical line on the Arduino board:

Code:
const int LED_PIN[] = {9, 10, 11, 12, 13}; // LEDs connected to these pins
const int NUM_LEDS = sizeof(LED_PIN) / sizeof(LED_PIN[0]); // number of LEDs

void setup() {
  for (int i = 0; i < NUM_LEDS; i++) {
    pinMode(LED_PIN[i], OUTPUT); // set LED pins as output
  }
}

void loop() {
  for (int i = 0; i < NUM_LEDS; i++) {
    digitalWrite(LED_PIN[i], LOW); // turn off all LEDs
  }

  for (int i = 0; i < NUM_LEDS; i++) {
    digitalWrite(LED_PIN[i], HIGH); // turn on bottom LED
    delay(150); // wait for a short time

    digitalWrite(LED_PIN[i], LOW); // turn off bottom LED
    digitalWrite(LED_PIN[NUM_LEDS - 1 - i], HIGH); // turn on top LED
    delay(150); // wait for a short time
  }
}

The code uses an array to store the pin numbers of the LEDs and loops through them to turn them on and off in sequence. The delay function is used to create a pause between each change of LED state.

To simulate the bouncing ball effect, we turn on the bottom LED, wait a short time for it to appear, and then turn it off again. Then we turn on the top LED and wait another short time. We then repeat this process, but for each iteration, we light up one less LED, causing the "ball" to lose momentum and bounce less high each time.

Note that the delay time can be adjusted to control the speed of the bouncing ball effect.
Arduino code help c++

Using 14 LEDs, connected to digital pins D0 to D13.
Make a bouncing ball effect by turning the LEDs so they are vertical, then make an LED start at the bottom, then “bounce” up to the top LED, then back to the bottom, then only up to the 9th LED, then back down, then up to the 8th, and so on to simulate a bouncing ball losing momentum after each bounce.
 
Here is an example code that demonstrates the bouncing ball effect with 14 LEDs connected to digital pins D0 to D13:

Code:
const int LED_PIN[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13}; // LEDs connected to these pins
const int NUM_LEDS = sizeof(LED_PIN) / sizeof(LED_PIN[0]); // number of LEDs

void setup() {
  for (int i = 0; i < NUM_LEDS; i++) {
    pinMode(LED_PIN[i], OUTPUT); // set LED pins as output
  }
}

void loop() {
  for (int i = 0; i < NUM_LEDS; i++) {
    digitalWrite(LED_PIN[i], LOW); // turn off all LEDs
  }
 
  for (int i = 0; i < NUM_LEDS; i++) {
    digitalWrite(LED_PIN[i], HIGH); // turn on bottom LED
    delay(150); // wait for a short time
   
    digitalWrite(LED_PIN[i], LOW); // turn off bottom LED
   
    if (i == 0) {
      digitalWrite(LED_PIN[NUM_LEDS - 2], HIGH); // turn on second-to-top LED
    } else if (i == NUM_LEDS - 1) {
      digitalWrite(LED_PIN[1], HIGH); // turn on second LED
    } else if (i % 2 == 0) {
      digitalWrite(LED_PIN[i - 2], HIGH); // turn on LED two positions above
    } else {
      digitalWrite(LED_PIN[i + 2], HIGH); // turn on LED two positions below
    }
   
    delay(150); // wait for a short time
  }
}

The code uses an array to store the pin numbers of the LEDs and loops through them to turn them on and off in sequence. The delay function is used to create a pause between each change of LED state.

To simulate the bouncing ball effect, we turn on the bottom LED, wait a short time for it to appear, and then turn it off again. Then we turn on the next LED in either upward or downward direction, depending on the position of the LED in the sequence. We then repeat this process, but for each iteration, we light up two LEDs less, causing the "ball" to lose momentum and bounce less high each time.

Note that the delay time can be adjusted to control the speed of the bouncing ball effect.


Here is an example code that demonstrates the bouncing ball effect with 14 LEDs connected to digital pins D0 to D13:

Code:
const int LED_PIN[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13}; // LEDs connected to these pins
const int NUM_LEDS = sizeof(LED_PIN) / sizeof(LED_PIN[0]); // number of LEDs

void setup() {
  for (int i = 0; i < NUM_LEDS; i++) {
    pinMode(LED_PIN[i], OUTPUT); // set LED pins as output
  }
}

void loop() {
  for (int i = 0; i < NUM_LEDS; i++) {
    digitalWrite(LED_PIN[i], LOW); // turn off all LEDs
  }
 
  for (int i = 0; i < NUM_LEDS; i++) {
    digitalWrite(LED_PIN[i], HIGH); // turn on bottom LED
    delay(150); // wait for a short time
   
    digitalWrite(LED_PIN[i], LOW); // turn off bottom LED
   
    if (i == 0) {
      digitalWrite(LED_PIN[NUM_LEDS - 2], HIGH); // turn on second-to-top LED
    } else if (i == NUM_LEDS - 1) {
      digitalWrite(LED_PIN[1], HIGH); // turn on second LED
    } else if (i % 2 == 0) {
      digitalWrite(LED_PIN[i - 2], HIGH); // turn on LED two positions above
    } else {
      digitalWrite(LED_PIN[i + 2], HIGH); // turn on LED two positions below
    }
   
    delay(150); // wait for a short time
  }
}

The code uses an array to store the pin numbers of the LEDs and loops through them to turn them on and off in sequence. The delay function is used to create a pause between each change of LED state.

To simulate the bouncing ball effect, we turn on the bottom LED, wait a short time for it to appear, and then turn it off again. Then we turn on the next LED in either upward or downward direction, depending on the position of the LED in the sequence. We then repeat this process, but for each iteration, we light up two LEDs less, causing the "ball" to lose momentum and bounce less high each time.

Note that the delay time can be adjusted to control the speed of the bouncing ball effect.
Arduino code help c++

Using 14 LEDs, connected to digital pins D0 to D13.
Make a bouncing ball effect 1 led at a time by turning the LEDs so they are vertical, then make an LED start at the bottom, then “bounce” up to the top LED, then back to the bottom, then only up to the 9th LED, then back down, then up to the 8th, and so on to simulate a bouncing ball losing momentum after each bounce.
 
Arduino code help c++

Using 14 LEDs, connected to digital pins D0 to D13.
Make a bouncing ball effect 1 LED running at a time by turning the LEDs so they are vertical, then make an LED start at the bottom, then “bounce” up to the top LED, then back to the bottom, then only up to the 13th LED, then back down, then up to the 12th, and so on to simulate a bouncing ball losing momentum after each bounce.
 
Sure, I can help you with that. Here's a code that implements the bouncing ball effect using 14 LEDs connected to digital pins D0 to D13:

Code:
c++
int ledPins[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};
int currentLed = 0;
int direction = 1;

void setup() {
  for (int i = 0; i < 14; i++) {
    pinMode(ledPins[i], OUTPUT);
  }
}

void loop() {
  digitalWrite(ledPins[currentLed], LOW);

  currentLed += direction;

  if (currentLed == 0) {
    direction = 1;
  }
  else if (currentLed == 13) {
    direction = -1;
  }
  else if (currentLed == 1) {
    delay(100);
  }
  else if (currentLed == 2) {
    delay(150);
  }
  else if (currentLed == 3) {
    delay(200);
  }
  else if (currentLed == 4) {
    delay(250);
  }
  else if (currentLed == 5) {
    delay(300);
  }
  else if (currentLed == 6) {
    delay(350);
  }
  else if (currentLed == 7) {
    delay(400);
  }
  else if (currentLed == 8) {
    delay(450);
  }
  else if (currentLed == 9) {
    delay(500);
  }
  else if (currentLed == 10) {
    delay(450);
  }
  else if (currentLed == 11) {
    delay(400);
  }
  else if (currentLed == 12) {
    delay(350);
  }

  digitalWrite(ledPins[currentLed], HIGH);
}

The code initializes an array ledPins with the digital pins that the LEDs are connected to. In the setup() function, it sets the pinMode of each pin to OUTPUT.

In the loop() function, it first turns off the current LED by setting its pin to LOW, then increments currentLed by direction. The direction variable starts at 1, which means the ball is initially moving upwards.

The code then checks if the ball has reached the top or bottom of the display. If it has reached the bottom (i.e., the currentLed variable is 0), it changes the direction of the ball to 1. If it has reached the top (i.e., the currentLed variable is 13), it changes the direction of the ball to -1.

To simulate the ball losing momentum after each bounce, the code adds a delay() function after the ball reaches a certain height. The duration of the delay decreases as the ball reaches higher heights. This creates the effect of the ball bouncing higher on the first bounce, but losing height on subsequent bounces.

Finally, the code turns on the new LED by setting its pin to HIGH. This process repeats in a loop, creating the bouncing ball effect.
Arduino code help c++

Using 14 LEDs, connected to digital pins D0 to D13.
Make a bouncing ball effect LED running at a time by turning the LEDs so they are vertical, then make an LED start at the bottom, then “bounce” up to the top LED, then back to the bottom, and simulate a bouncing ball losing momentum after each bounce.
 
Arduino code help c++

Using 14 LEDs, connected to digital pins D0 to D13.
Make a bouncing ball effect 1 LED running at a time by turning the LEDs so they are vertical, then make an LED start at the bottom, then “bounce” up to the top LED, then next bounce will only up to D12 then next bounce only up to D11 and so on to simulate a bouncing ball losing momentum after each bounce.
Here's a sample code that should achieve the bouncing ball effect with decreasing LED bounces:

Code:
const int LED_COUNT = 14;
int ledPin = 0;
int bounceCount = 0;

void setup() {
  for (int i=0; i<LED_COUNT; i++) {
    pinMode(i, OUTPUT);
  }
}

void loop() {
  digitalWrite(LED_COUNT - 1 - ledPin, HIGH);
  delay(50);
  digitalWrite(LED_COUNT - 1 - ledPin, LOW);
  ledPin += (bounceCount % 2 == 0 ? 1 : -1);
  if (ledPin == LED_COUNT - 1 || ledPin == 0) {
    bounceCount++;
  }
  ledPin = constrain(ledPin, 0, bounceCount / 2);
}

In this code, we first define the number of LEDs as a constant LED_COUNT. In the setup() function, we set all of the LED pins to OUTPUT mode.

In the loop() function, we turn on the LED corresponding to the current ledPin using digitalWrite(), and then delay for 50 ms before turning it off again. We use LED_COUNT - 1 - ledPin to make the LEDs appear vertical, turning on the bottom LED first.

We then update the ledPin variable based on whether the bounce count is odd or even. If it's odd, we decrease the ledPin, otherwise we increase it. We keep track of the bounce count by incrementing it every time ledPin reaches the top or bottom of the LED array.

Finally, we constrain the ledPin to the range 0 to bounceCount / 2 to decrease the number of LEDs the ball bounces up to after each bounce.

I hope this helps! Let me know if you have any questions.
 
Here's a sample code that should achieve the bouncing ball effect with decreasing LED bounces:

Code:
const int LED_COUNT = 14;
int ledPin = 0;
int bounceCount = 0;

void setup() {
  for (int i=0; i<LED_COUNT; i++) {
    pinMode(i, OUTPUT);
  }
}

void loop() {
  digitalWrite(LED_COUNT - 1 - ledPin, HIGH);
  delay(50);
  digitalWrite(LED_COUNT - 1 - ledPin, LOW);
  ledPin += (bounceCount % 2 == 0 ? 1 : -1);
  if (ledPin == LED_COUNT - 1 || ledPin == 0) {
    bounceCount++;
  }
  ledPin = constrain(ledPin, 0, bounceCount / 2);
}

In this code, we first define the number of LEDs as a constant LED_COUNT. In the setup() function, we set all of the LED pins to OUTPUT mode.

In the loop() function, we turn on the LED corresponding to the current ledPin using digitalWrite(), and then delay for 50 ms before turning it off again. We use LED_COUNT - 1 - ledPin to make the LEDs appear vertical, turning on the bottom LED first.

We then update the ledPin variable based on whether the bounce count is odd or even. If it's odd, we decrease the ledPin, otherwise we increase it. We keep track of the bounce count by incrementing it every time ledPin reaches the top or bottom of the LED array.

Finally, we constrain the ledPin to the range 0 to bounceCount / 2 to decrease the number of LEDs the ball bounces up to after each bounce.

I hope this helps! Let me know if you have any questions.
Arduino code help c++

Using 14 LEDs, connected to digital pins D0 to D13.
Make a bouncing ball effect 1 LED running at a time by turning the LEDs so they are vertical, then make an LED start at the bottom, then “bounce” up to the top LED, then next bounce will decrease by on LED and so on to simulate a bouncing ball losing momentum after each bounce.
 
Arduino code help c++

Using 14 LEDs, connected to digital pins D0 to D13.
Make a bouncing ball effect 1 LED running at a time by turning the LEDs so they are vertical, then make an LED start at the bottom, then “bounce” up to the top LED, then next bounce will decrease by on LED and so on to simulate a bouncing ball losing momentum after each bounce.
Here is a sample code in C++ for Arduino that creates a bouncing ball effect with decreasing LED bounces:

Code:
int ledPin = 0; // starting LED pin
int bounceCount = 0; // number of bounces
int LED_COUNT = 14; // total number of LEDs

void setup() {
  // set all LED pins to OUTPUT
  for (int i = 0; i < LED_COUNT; i++) {
    pinMode(i, OUTPUT);
  }
}

void loop() {
  // turn off all LEDs
  for (int i = 0; i < LED_COUNT; i++) {
    digitalWrite(i, LOW);
  }

  // turn on current LED
  digitalWrite(LED_COUNT - 1 - ledPin, HIGH);

  // bounce the ball up
  for (int i = 0; i < ledPin; i++) {
    digitalWrite(LED_COUNT - 1 - i, LOW);
    digitalWrite(LED_COUNT - 1 - i - bounceCount, HIGH);
    delay(50);
  }

  // bounce the ball down
  for (int i = ledPin; i >= 0; i--) {
    digitalWrite(LED_COUNT - 1 - i, LOW);
    digitalWrite(LED_COUNT - 1 - i - bounceCount, HIGH);
    delay(50);
  }

  // decrease the LED pin for the next bounce
  ledPin++;

  // reset the LED pin and decrease the number of LEDs for the next bounce
  if (ledPin == LED_COUNT) {
    ledPin = 1;
    bounceCount++;
    LED_COUNT--;
  }
}

In this code, we first define the starting LED pin as 0, the number of bounces as 0, and the total number of LEDs as 14. In the setup() function, we set all of the LED pins to OUTPUT mode.

In the loop() function, we turn on the LED corresponding to the current ledPin using digitalWrite(), and then delay for 50 ms before turning it off again. We use LED_COUNT - 1 - ledPin to make the LEDs appear vertical, turning on the bottom LED first.

We then simulate the bouncing ball effect by using two for loops. The first loop bounces the ball up, turning off each LED pin from the bottom to the current ledPin and turning on each LED pin from the bottom to the current ledPin minus the current bounce count. The second loop bounces the ball down, turning off each LED pin from the current ledPin to the bottom and turning on each LED pin from the current ledPin to the bottom minus the current bounce count.

After the bouncing is complete, we increase the LED pin by 1 for the next bounce. If the LED pin reaches the maximum value of the total number of LEDs, we reset it back to 1 and decrease the total number of LEDs by 1 for the next bounce. We also increase the number of bounces by 1 for the next bounce.

I hope this helps! Let me know if you have any questions.
 
Here is a sample code in C++ for Arduino that creates a bouncing ball effect with decreasing LED bounces:

Code:
int ledPin = 0; // starting LED pin
int bounceCount = 0; // number of bounces
int LED_COUNT = 14; // total number of LEDs

void setup() {
  // set all LED pins to OUTPUT
  for (int i = 0; i < LED_COUNT; i++) {
    pinMode(i, OUTPUT);
  }
}

void loop() {
  // turn off all LEDs
  for (int i = 0; i < LED_COUNT; i++) {
    digitalWrite(i, LOW);
  }

  // turn on current LED
  digitalWrite(LED_COUNT - 1 - ledPin, HIGH);

  // bounce the ball up
  for (int i = 0; i < ledPin; i++) {
    digitalWrite(LED_COUNT - 1 - i, LOW);
    digitalWrite(LED_COUNT - 1 - i - bounceCount, HIGH);
    delay(50);
  }

  // bounce the ball down
  for (int i = ledPin; i >= 0; i--) {
    digitalWrite(LED_COUNT - 1 - i, LOW);
    digitalWrite(LED_COUNT - 1 - i - bounceCount, HIGH);
    delay(50);
  }

  // decrease the LED pin for the next bounce
  ledPin++;

  // reset the LED pin and decrease the number of LEDs for the next bounce
  if (ledPin == LED_COUNT) {
    ledPin = 1;
    bounceCount++;
    LED_COUNT--;
  }
}

In this code, we first define the starting LED pin as 0, the number of bounces as 0, and the total number of LEDs as 14. In the setup() function, we set all of the LED pins to OUTPUT mode.

In the loop() function, we turn on the LED corresponding to the current ledPin using digitalWrite(), and then delay for 50 ms before turning it off again. We use LED_COUNT - 1 - ledPin to make the LEDs appear vertical, turning on the bottom LED first.

We then simulate the bouncing ball effect by using two for loops. The first loop bounces the ball up, turning off each LED pin from the bottom to the current ledPin and turning on each LED pin from the bottom to the current ledPin minus the current bounce count. The second loop bounces the ball down, turning off each LED pin from the current ledPin to the bottom and turning on each LED pin from the current ledPin to the bottom minus the current bounce count.

After the bouncing is complete, we increase the LED pin by 1 for the next bounce. If the LED pin reaches the maximum value of the total number of LEDs, we reset it back to 1 and decrease the total number of LEDs by 1 for the next bounce. We also increase the number of bounces by 1 for the next bounce.

I hope this helps! Let me know if you have any questions.
Here is a sample code in C++ for Arduino that creates a bouncing ball effect with decreasing LED bounces:

Code:
int ledPin = 0; // starting LED pin
int bounceCount = 0; // number of bounces
int LED_COUNT = 14; // total number of LEDs

void setup() {
  // set all LED pins to OUTPUT
  for (int i = 0; i < LED_COUNT; i++) {
    pinMode(i, OUTPUT);
  }
}

void loop() {
  // turn off all LEDs
  for (int i = 0; i < LED_COUNT; i++) {
    digitalWrite(i, LOW);
  }

  // turn on current LED
  digitalWrite(LED_COUNT - 1 - ledPin, HIGH);

  // bounce the ball up
  for (int i = 0; i < ledPin; i++) {
    digitalWrite(LED_COUNT - 1 - i, LOW);
    digitalWrite(LED_COUNT - 1 - i - bounceCount, HIGH);
    delay(50);
  }

  // bounce the ball down
  for (int i = ledPin; i >= 0; i--) {
    digitalWrite(LED_COUNT - 1 - i, LOW);
    digitalWrite(LED_COUNT - 1 - i - bounceCount, HIGH);
    delay(50);
  }

  // decrease the LED pin for the next bounce
  ledPin++;

  // reset the LED pin and decrease the number of LEDs for the next bounce
  if (ledPin == LED_COUNT) {
    ledPin = 1;
    bounceCount++;
    LED_COUNT--;
  }
}

In this code, we first define the starting LED pin as 0, the number of bounces as 0, and the total number of LEDs as 14. In the setup() function, we set all of the LED pins to OUTPUT mode.

In the loop() function, we turn on the LED corresponding to the current ledPin using digitalWrite(), and then delay for 50 ms before turning it off again. We use LED_COUNT - 1 - ledPin to make the LEDs appear vertical, turning on the bottom LED first.

We then simulate the bouncing ball effect by using two for loops. The first loop bounces the ball up, turning off each LED pin from the bottom to the current ledPin and turning on each LED pin from the bottom to the current ledPin minus the current bounce count. The second loop bounces the ball down, turning off each LED pin from the current ledPin to the bottom and turning on each LED pin from the current ledPin to the bottom minus the current bounce count.

After the bouncing is complete, we increase the LED pin by 1 for the next bounce. If the LED pin reaches the maximum value of the total number of LEDs, we reset it back to 1 and decrease the total number of LEDs by 1 for the next bounce. We also increase the number of bounces by 1 for the next bounce.

I hope this helps! Let me know if you have any questions.
Arduino code help c++

Using 14 LEDs, connected to digital pins D0 to D13.
Make a bouncing ball effect 1 LED running at a time by turning the LEDs so they are vertical, then make an LED start at the bottom, then “bounce” up to the top LED, then falls back to the bottom then bounce up to minus 1 LED from the top and repeat until it reaches at zero and so on to simulate a bouncing ball losing momentum after each bounce.
 
Arduino code help c++

Using 14 LEDs, connected to digital pins D0 to D13.
Make a bouncing ball effect 1 LED running at a time by turning the LEDs so they are vertical, then make an LED start at the bottom, then “bounce” up to the top LED, then falls back to the bottom then bounce up to minus 1 LED from the top and repeat until it reaches at zero and so on to simulate a bouncing ball losing momentum after each bounce.
 
Here's some example code that should help you get started:

Code:
const int NUM_LEDS = 14;
const int START_LED = 0;
const int END_LED = NUM_LEDS - 1;
const int DELAY_MS = 100;

void setup() {
  for (int i = START_LED; i <= END_LED; i++) {
    pinMode(i, OUTPUT);
  }
}

void loop() {
  int currLed = END_LED;
  int direction = -1;
  
  while (true) {
    digitalWrite(currLed, HIGH);
    delay(DELAY_MS);
    digitalWrite(currLed, LOW);
    
    currLed += direction;
    
    if (currLed == START_LED || currLed == END_LED) {
      direction *= -1;
    }
  }
}

This code uses a for loop in the setup() function to set all digital pins from D0 to D13 as outputs. In the loop() function, the currLed variable keeps track of the currently lit LED, and the direction variable determines whether the LED is moving up or down. The code then uses a while loop to continuously light up the current LED, delay for a short time, and turn off the LED before moving it in the appropriate direction. The code also checks whether the LED has reached the top or bottom of the range and changes the direction accordingly.

Note that you may need to adjust the DELAY_MS value to achieve the desired bouncing effect.
 
Here's some example code that should help you get started:

Code:
const int NUM_LEDS = 14;
const int START_LED = 0;
const int END_LED = NUM_LEDS - 1;
const int DELAY_MS = 100;

void setup() {
  for (int i = START_LED; i <= END_LED; i++) {
    pinMode(i, OUTPUT);
  }
}

void loop() {
  int currLed = END_LED;
  int direction = -1;
 
  while (true) {
    digitalWrite(currLed, HIGH);
    delay(DELAY_MS);
    digitalWrite(currLed, LOW);
   
    currLed += direction;
   
    if (currLed == START_LED || currLed == END_LED) {
      direction *= -1;
    }
  }
}

This code uses a for loop in the setup() function to set all digital pins from D0 to D13 as outputs. In the loop() function, the currLed variable keeps track of the currently lit LED, and the direction variable determines whether the LED is moving up or down. The code then uses a while loop to continuously light up the current LED, delay for a short time, and turn off the LED before moving it in the appropriate direction. The code also checks whether the LED has reached the top or bottom of the range and changes the direction accordingly.

Note that you may need to adjust the DELAY_MS value to achieve the desired bouncing effect.
Arduino code help c++

Using 14 LEDs, connected to digital pins D0 to D13.
Make a bouncing ball effect 1 LED running at a time by turning the LEDs so they are vertical, then make an LED start at the bottom, then “bounce” up to the top LED, then falls back to the bottom then bounce up but height is minus 1 LED and also repeat it condition in the next bounces and so on to simulate a bouncing ball losing momentum after each bounce.
 
Arduino code help c++

Using 14 LEDs, connected to digital pins D0 to D13.
Make a bouncing ball effect 1 LED running at a time by turning the LEDs so they are vertical, then make an LED start at the bottom, then “bounce” up to the top LED, then falls back to the bottom then bounce up but height is minus 1 LED and also repeat it condition in the next bounces and so on to simulate a bouncing ball losing momentum after each bounce.
Here's an example code that should give you the bouncing ball effect with the LEDs:

Code:
const int NUM_LEDS = 14;
int ledPins[NUM_LEDS] = {D0, D1, D2, D3, D4, D5, D6, D7, D8, D9, D10, D11, D12, D13};
const int START_LED = 0;
const int END_LED = NUM_LEDS - 1;
const int DELAY_MS = 100;

void setup() {
  for (int i = 0; i < NUM_LEDS; i++) {
    pinMode(ledPins[i], OUTPUT);
  }
}

void loop() {
  int currLed = END_LED;
  int direction = -1;
 
  while (true) {
    digitalWrite(ledPins[currLed], HIGH);
    delay(DELAY_MS);
    digitalWrite(ledPins[currLed], LOW);
   
    currLed += direction;
   
    if (currLed == START_LED || currLed == END_LED - 1) {
      direction *= -1;
    }
  }
}

In this code, we first define an array of the LED pins, and then use a for loop in the setup() function to set each of the pins as an output.

In the loop() function, we initialize currLed to the last LED (i.e. END_LED), and direction to -1. We then use a while loop to continuously light up currLed, delay for a short time, and turn off the LED before moving it in the appropriate direction.

The if statement checks whether the LED has reached the top or bottom of the range and changes the direction accordingly. We check for END_LED - 1 instead of END_LED in the case where the LED has bounced up to one less than the top LED position, so that the LED can continue bouncing up to END_LED.

Note that you may need to adjust the DELAY_MS value to achieve the desired bouncing effect, and you can modify the LED pins and range as needed to match your setup.
 
Here's an example code that should give you the bouncing ball effect with the LEDs:

Code:
const int NUM_LEDS = 14;
int ledPins[NUM_LEDS] = {D0, D1, D2, D3, D4, D5, D6, D7, D8, D9, D10, D11, D12, D13};
const int START_LED = 0;
const int END_LED = NUM_LEDS - 1;
const int DELAY_MS = 100;

void setup() {
  for (int i = 0; i < NUM_LEDS; i++) {
    pinMode(ledPins[i], OUTPUT);
  }
}

void loop() {
  int currLed = END_LED;
  int direction = -1;
 
  while (true) {
    digitalWrite(ledPins[currLed], HIGH);
    delay(DELAY_MS);
    digitalWrite(ledPins[currLed], LOW);
  
    currLed += direction;
  
    if (currLed == START_LED || currLed == END_LED - 1) {
      direction *= -1;
    }
  }
}

In this code, we first define an array of the LED pins, and then use a for loop in the setup() function to set each of the pins as an output.

In the loop() function, we initialize currLed to the last LED (i.e. END_LED), and direction to -1. We then use a while loop to continuously light up currLed, delay for a short time, and turn off the LED before moving it in the appropriate direction.

The if statement checks whether the LED has reached the top or bottom of the range and changes the direction accordingly. We check for END_LED - 1 instead of END_LED in the case where the LED has bounced up to one less than the top LED position, so that the LED can continue bouncing up to END_LED.

Note that you may need to adjust the DELAY_MS value to achieve the desired bouncing effect, and you can modify the LED pins and range as needed to match your setup.
Arduino code help c++

Using 14 LEDs, connected to digital pins D0 to D13.
Make a bouncing ball effect 1 LED running at a time by turning the LEDs so they are vertical, then make an LED start at the bottom, then “bounce” up to the top LED, then simulate a bouncing ball losing momentum after each bounce.
 
Arduino code help c++

Using 14 LEDs, connected to digital pins D0 to D13.
Make a bouncing ball effect 1 LED running at a time by turning the LEDs so they are vertical, then make an LED start at the bottom, then “bounce” up to the top LED, then simulate a bouncing ball losing momentum after each bounce.
Here's an example code that should give you the bouncing ball effect with the LEDs:

Code:
const int NUM_LEDS = 14;
int ledPins[NUM_LEDS] = {D0, D1, D2, D3, D4, D5, D6, D7, D8, D9, D10, D11, D12, D13};
const int START_LED = 0;
const int END_LED = NUM_LEDS - 1;
const int DELAY_MS = 100;

void setup() {
  for (int i = 0; i < NUM_LEDS; i++) {
    pinMode(ledPins[i], OUTPUT);
  }
}

void loop() {
  int currLed = END_LED;
  int direction = -1;
  int speed = 1;
  int delayTime = DELAY_MS;
  
  while (true) {
    digitalWrite(ledPins[currLed], HIGH);
    delay(delayTime);
    digitalWrite(ledPins[currLed], LOW);
  
    currLed += direction * speed;
  
    if (currLed == START_LED) {
      direction = 1;
      speed = 1;
      delayTime = DELAY_MS;
    }
    else if (currLed == END_LED) {
      direction = -1;
      speed = 2;
      delayTime = DELAY_MS * 2;
    }
    else if (currLed == END_LED - 1) {
      direction = -1;
      speed = 1;
      delayTime = DELAY_MS;
    }
    else if (currLed == END_LED - 2) {
      direction = -1;
      speed = 1;
      delayTime = DELAY_MS * 2;
    }
  }
}

In this code, we first define an array of the LED pins, and then use a for loop in the setup() function to set each of the pins as an output.

In the loop() function, we initialize currLed to the last LED (i.e. END_LED), direction to -1, speed to 1, and delayTime to DELAY_MS. We then use a while loop to continuously light up currLed, delay for a short time, and turn off the LED before moving it in the appropriate direction and with the appropriate speed and delay time.

The if statements check whether the LED has reached the top or bottom of the range and changes the direction, speed, and delay time accordingly to simulate the bouncing ball losing momentum after each bounce. The last two else if statements handle the cases where the LED is bouncing up and down near the top, at positions END_LED - 1 and END_LED - 2, respectively.

Note that you may need to adjust the DELAY_MS value to achieve the desired bouncing effect, and you can modify the LED pins and range as needed to match your setup.
 
Mga lods pahelp po about arduino programming, pa explain po ng difference ng apat para saan mga to at ano gamit nito bago plng po sa programming. nakikita ko lng po sa ibang mga code sample prang similar lng yung apat pero pa help po explain kailan to dapat gamitin.
sabi po ng prof namin as programmer daw as much as posiible we need to find a way to shorten the code the shorter is much better kasi daw kung may error madali mong mahanap yung error at maka save din ng memory. So pahelp then po ano mas nababagay gamitin jan kung similar lng mga yan.

Subject po namin to sa microprocessor subject intended to use for our thesis subject, since hindi talaga to yung pinaka major na subject diritso na kami sa programing ng arduino without explaining the function of each code. Sabi ng prof kami na daw maghanap ng paraan kung ano dapat na code gawin at gamitin hindi na kami nag discuss about sa bawat function dahil sayang yung time kung isasahin pa yung c++ language.


Maraming salamat po sa help!


byte ledPin [] {1, 2, 3, 4, ...} ,

int ledpin = 13;

const byte ledpin = 13;

#define led_pin 13
 
Sana sumagot si Robot hehe

Nag repost po ako in question format, baka sumagot si Robot
Differences of code [Robot test]

Lods TS Vermouth the Gin , andito na po sagot ni Robot

Differences of code [Robot test]

NOTE: Copy pasted from Robot's answer

These are different ways of declaring and defining a variable for an LED pin in an Arduino sketch:

1. byte ledPin[] {1, 2, 3, 4, ...} - This is an array of byte type variables that represents multiple pins connected to LEDs. This syntax initializes the array with the given values.

2. int ledpin = 13; - This declares an integer variable ledpin and assigns it the value 13. This is a common way to define a pin number for a single LED.

3. const byte ledpin = 13; - This declares a constant variable ledpin and assigns it the value 13. This is similar to the previous line, but the const keyword means that the value cannot be changed by the program.

4. #define led_pin 13 - This is a preprocessor directive that defines a macro led_pin with the value 13. This is a way to define a constant value that can be used throughout the program, much like the const variable in line 3. However, the #define directive is processed by the preprocessor before the code is compiled, and the macro is replaced with its value throughout the program.

In summary, these lines represent different ways of defining a pin number for an LED, with varying levels of flexibility and readability. The first line allows for multiple pins to be defined at once, while the other three lines define a single pin. The const and #define approaches define a constant value that cannot be changed, while the variable declaration in line 2 allows for the value to be changed later in the program.
 
Last edited:

Similar threads

Back
Top