text:
Help with Stair Lighting using HC-SR04 sensor and WS2812 LED Strip
Excuse my naivete in this as I am very new to Arduino and the programming of. I am trying to put together a project for stair lighting triggered by a ultrasonic sensor at the bottom of the stairs and turned off by an ultrasonic sensor at the top of the stairs and vise versa. I am using an WS2812 LED strip and have the lights travel up the stairs when going up and down when going down. I'm sure this is a project many of you have seen. I have searched the net and youtube and have found many examples of this project, none that I can find using the HC-SR04 sensor. I have managed to cobble together a code which works fine in both direction but it is intermittent in working. I'm sure it is something to do with timings as sometimes it will work fine first time but then others the sensor needs to be blocked for a couple of seconds.
I would also like to add an LDR to the project so it is only active at night. If any of you can point me in the right direction I would be really grateful. I have attached the code so you can see where I am at.
This is just a test code and the final project will have around 200 LEDS.
Thanks

#include <FastLED.h>
#define trigPin_up 2
#define echoPin_up 3
#define trigPin_down 4
#define echoPin_down 5
#define LED_PIN     7
#define NUM_LEDS    13
CRGB leds[NUM_LEDS];

void setup() {

    //Set Serial
  Serial.begin(9600);

  FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);

  pinMode(trigPin_up, OUTPUT);
  pinMode(echoPin_up, INPUT);
  pinMode(trigPin_down, OUTPUT);
  pinMode(echoPin_down, INPUT);

}
 void loop(){
 if (trigPin_up, HIGH && trigPin_down, LOW);
 {
   short duration, distance;
  digitalWrite(trigPin_up, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin_up, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin_up, LOW);
  duration = pulseIn(echoPin_up, HIGH);
  distance = (duration/2) / 29.1;

  if (distance <= 50) {
  leds[0] = CRGB(255, 225, 255);
  FastLED.show();
  delay(100);
  leds[1] = CRGB(255, 255, 255);
  FastLED.show();
  delay(100);
  leds[2] = CRGB(255, 255, 255);
  FastLED.show();
  delay(100);
  leds[3] = CRGB(255, 255, 255);
  FastLED.show();
  delay(100);
  leds[4] = CRGB(255, 255, 255);
  FastLED.show();
  delay(100);
  leds[5] = CRGB(255, 255, 255);
  FastLED.show();
  delay(100);
  leds[6] = CRGB(255, 255, 255);
  FastLED.show();
   delay(100);
   leds[7] = CRGB(255, 255, 255);
  FastLED.show();
   delay(100);
   leds[8] = CRGB(255, 255, 255);
  FastLED.show();
   delay(100);
   leds[9] = CRGB(255, 255, 255);
  FastLED.show();
   delay(100);
   leds[10] = CRGB(255, 255, 255);
  FastLED.show();
   delay(100);
   leds[11] = CRGB(255, 255, 255);
  FastLED.show();
   delay(100);
   leds[12] = CRGB(255, 255, 255);
  FastLED.show();
   delay(100);
   leds[13] = CRGB(255, 255, 255);
  FastLED.show();
  delay(100);
  }

{    short duration, distance;
  digitalWrite(trigPin_down, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin_down, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin_down, LOW);
  duration = pulseIn(echoPin_down, HIGH);
  distance = (duration/2) / 29.1;

  if (distance <= 50) {
  leds[0] = CRGB(0, 0, 0);
  FastLED.show();
  delay(100);
  leds[1] = CRGB(0, 0, 0);
  FastLED.show();
  delay(100);
  leds[2] = CRGB(0, 0, 0);
  FastLED.show();
  delay(100);
  leds[3] = CRGB(0, 0, 0);
  FastLED.show();
  delay(100);
  leds[4] = CRGB(0, 0, 0);
  FastLED.show();
  delay(100);
  leds[5] = CRGB(0, 0, 0);
  FastLED.show();
  delay(100);
  leds[6] = CRGB(0, 0, 0);
  FastLED.show();
   delay(100);
   leds[7] = CRGB(0, 0, 0);
  FastLED.show();
   delay(100);
   leds[8] = CRGB(0, 0, 0);
  FastLED.show();
   delay(100);
   leds[9] = CRGB(0, 0, 0);
  FastLED.show();
   delay(100);
   leds[10] = CRGB(0, 0, 0);
  FastLED.show();
   delay(100);
   leds[11] = CRGB(0, 0, 0);
  FastLED.show();
   delay(100);
   leds[12] = CRGB(0, 0, 0);
  FastLED.show();
   delay(100);
   leds[13] = CRGB(0, 0, 0);
  FastLED.show();
  delay(100);
  }

else
if (trigPin_up, LOW && trigPin_down, HIGH);
  {

     short duration, distance;
  digitalWrite(trigPin_down, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin_down, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin_down, LOW);
  duration = pulseIn(echoPin_down, HIGH);
  distance = (duration/2) / 29.1;

  if (distance <= 50) {
  leds[13] = CRGB(255, 225, 255);
  FastLED.show();
  delay(100);
  leds[12] = CRGB(255, 255, 255);
  FastLED.show();
  delay(100);
  leds[11] = CRGB(255, 255, 255);
  FastLED.show();
  delay(100);
  leds[10] = CRGB(255, 255, 255);
  FastLED.show();
  delay(100);
  leds[9] = CRGB(255, 255, 255);
  FastLED.show();
  delay(100);
  leds[8] = CRGB(255, 255, 255);
  FastLED.show();
  delay(100);
  leds[7] = CRGB(255, 255, 255);
  FastLED.show();
   delay(100);
   leds[6] = CRGB(255, 255, 255);
  FastLED.show();
   delay(100);
   leds[5] = CRGB(255, 255, 255);
  FastLED.show();
   delay(100);
   leds[4] = CRGB(255, 255, 255);
  FastLED.show();
   delay(100);
   leds[3] = CRGB(255, 255, 255);
  FastLED.show();
   delay(100);
   leds[2] = CRGB(255, 255, 255);
  FastLED.show();
   delay(100);
   leds[1] = CRGB(255, 255, 255);
  FastLED.show();
   delay(100);
   leds[0] = CRGB(255, 255, 255);
  FastLED.show();
  delay(100);
  }
{
  short duration, distance;
  digitalWrite(trigPin_up, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin_up, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin_up, LOW);
  duration = pulseIn(echoPin_up, HIGH);
  distance = (duration/2) / 29.1;

    if (distance <= 50) {
  leds[13] = CRGB(0, 0, 0);
  FastLED.show();
  delay(100);
  leds[12] = CRGB(0, 0, 0);
  FastLED.show();
  delay(100);
  leds[11] = CRGB(0, 0, 0);
  FastLED.show();
  delay(100);
  leds[10] = CRGB(0, 0, 0);
  FastLED.show();
  delay(100);
  leds[9] = CRGB(0, 0, 0);
  FastLED.show();
  delay(100);
  leds[8] = CRGB(0, 0, 0);
  FastLED.show();
  delay(100);
  leds[7] = CRGB(0, 0, 0);
  FastLED.show();
   delay(100);
   leds[6] = CRGB(0, 0, 0);
  FastLED.show();
   delay(100);
   leds[5] = CRGB(0, 0, 0);
  FastLED.show();
   delay(100);
   leds[4] = CRGB(0, 0, 0);
  FastLED.show();
   delay(100);
   leds[3] = CRGB(0, 0, 0);
  FastLED.show();
   delay(100);
   leds[2] = CRGB(0, 0, 0);
  FastLED.show();
   delay(100);
   leds[1] = CRGB(0, 0, 0);
  FastLED.show();
   delay(100);
   leds[0] = CRGB(0, 0, 0);
  FastLED.show();
  delay(100);
  }
}

  }
}
 }
 }

There are a couple things with your code which you might need to look into.
Note: I'm doing this ontop of my head, so please correct me if I say something incorrect. ]
First, to check if values are equal, with an if-statement you'd have to use:
//if (trigPin_up, HIGH && trigPin_down, LOW); not like this.
if (trigPin_up== HIGH && trigPin_down== LOW) {.....} //But like this

Do not use the ';' because that would be then end of the statement.
Second, there seems to be a random '{' without any statement beforehand, at line 88.
Extra tip
You can use for loops!
So:
leds[0] = CRGB(255, 225, 255);
  FastLED.show();
  delay(100);
  ....
  leds[13] = CRGB(255, 255, 255);
  FastLED.show();
  delay(100);

becomes:
for (int i = 0; i <14; i++){
    leds[i] = CRGB(255, 225, 255);
  FastLED.show();
  delay(100);
  }

As a good exercise, could you try to clean up your code, edit it in the OP, and try if it works then?

The for loop at the end should only go up to 12 instead of 13, since there are only 13 elements in the array

I don't think so. At the top there are 13 elements defined, but there should be 14, since he wants to do 0 until 13, which are 14 different numbers.

But then the array needs to be define with 14 elements. NUM_LEDS is currently 13. If you are assuming, that the OP has 14 LEDs, you should include that assumption and also say, that the definition would have to be changed for that. People tend to copy&paste from the internet a lot, so it would be better to explain this, instead of just quietly assuming it.

Calvin Bootsman is right with the errors, that he described, but there are more problems in your code.

Look at these two lines, that I got from your sketch. I already corrected, what Calvin had to say about the if statement.
#define trigPin_up 2
if (trigPin_up == HIGH && trigPin_down == LOW){

You define trigPin_up as the number two. A define is read by the preprocessor. It will replace all occurances of trigPin_up with the number 2. HIGH and LOW are also defines (defined in the Arduino core) with the values 1 and 0. They also get replaced. After that the code goes to the actual compiler. So the compiler sees this if statement:
if( 2 == 1 && 4 == 0)

2 isn't equal 1 and 4 isn't equal 0, so the result is always false. That corrupts your complete logic. I'm sure you want to read the state of the pin. trigPin_up and siblings are only the pin numbers. For getting the real electrical state of the pin you need to actually read it via digitalRead():
if(digitalRead(trigPin_up) == HIGH && digitalRead(trigPin_down) == LOW){

You can simplify this even more, when you know, that zero is treated as false and every other value as true:
if(digitalRead(trigPin_up) && !digitalRead(trigPin_down)){

The ! before the second digitalRead() does a negation, so that the part after the && will become true, if digitalRead(trigPin_down) evaluates to false.
Now look at these lines:
#define NUM_LEDS    13
CRGB leds[NUM_LEDS];
leds[13] = CRGB(0, 0, 0);

You create an array named leds with NUM_LEDS (13) elements. The first index is 0 (as we start to count from zero in most programming languages). But that means, that the last index in the array is 12, not 13. When doing leds[13]=... you actually write to a memory address outside of your array. It could be everything, what is saved there. And you overwrite it. This often is a hard to diagnose problem, since it can have very weird consequences. Calvin Bootsman also did this wrong in his answer. For the for loop you only should count to 12, meaning using i < 13 or i < NUM_LEDS in the for loop.
Then I guess you don't really understand how the logic of interfacing the HC-SR04 works. The trigger pins, that you define, are outputs. Their state will only change, if you yourself change it via digitalWrite(). But that cannot happen, as you enclosed the statements doing it into the if statement. It does not make sense to read these pins either. Instead you should write if statements with the calculated distance. You could go with the following flow in you loop() function: Trigger first HC-SR04, read the echo back, trigger second HC-SR04, read the echo back, if statement to compare the calculated distances to a trigger value, in there if statements to decide, which one was triggered, execute the corresponding code in it.

Honestly I'm suprised, that your code even compiled. You should look into some basic C/C++ tutorials, so that you get the programming syntax right. Also you should carefully think about the logic of your program. Often it is better to first write down short notes, what the logic of the program should be, without really writing the code. That helps with focussing on the logic instead of the syntax. That can be done afterwards.

chrisl thanks for the feedback I will have a look into some basic C/C++ tutorials.


text:
How to set current position for the DC motor to zero + store current positions in an array and run it?
I would like to know how to set the current position for the DC motors to zero and store the current position in an array. after that I would like it to loop in the array to run all steps that was stored in the array?
Am using Genuino UNO board, I have 4 DC motors and each 2 wheels are connected to an H-bridge.
I have an application that have a "save" button and a "Run" button.
For the save button, I would like it to set the current position for each wheel to zero first then store the positions of each wheel in an array.
For the run button, I would like it to loop in the array that was made by the save button and show the output on each wheels.
basically what am trying to achieve is to make the robotic car that can move any direction I tell it to move in the same time  I can control the wheel speed.
all that move are being sent via Bluetooth as numbers.
I hope it's clear now
here is my code
#include <SoftwareSerial.h>

SoftwareSerial Bluetooth(0, 1); // Arduino(RX, TX) - HC-05 Bluetooth (TX, RX)

// Define the motors and the pins
// Motor A1

int enA1 = 2;
int in11 = 3;
int in21 = 4;

// Motor B1

int enB1 = 5;
int in31 = 6;
int in41 = 7;

//other H-bridge

// Motor A2

int enA2 = 8;
int in12 = 9;
int in22 = 10;

// Motor B2

int enB2 = 11;
int in32 = 12;
int in42 = 13;

int wheelSpeed = 125;
int dataIn, m;
//lbw left back wheel / lfw left fron wheel / rbw right back wheel / rfw right front wheel
int lbw[50], lfw[50], rbw[50], rfw[50]; // for storing positions/steps
int index = 0;

void setup() {
  // put your setup code here, to run once:

  Bluetooth.begin(9600); // Default baud rate of the Bluetooth module
  Bluetooth.setTimeout(1);
  Serial.print("connected");
  Serial.print("-------------------");
  delay(20);

  pinMode(enA1, OUTPUT);
  pinMode(enB1, OUTPUT);
  pinMode(in11, OUTPUT);
  pinMode(in21, OUTPUT);
  pinMode(in31, OUTPUT);
  pinMode(in41, OUTPUT);
  pinMode(enA2, OUTPUT);
  pinMode(enB2, OUTPUT);
  pinMode(in12, OUTPUT);
  pinMode(in22, OUTPUT);
  pinMode(in32, OUTPUT);
  pinMode(in42, OUTPUT);
}

void loop() {
  // Check for incoming data

 if (Bluetooth.available() > 0) {
    dataIn = Bluetooth.read();  // Read the data

    if (dataIn == 0) {
      m = 0;
    }
    if (dataIn == 1) {
      m = 1;
    }
    if (dataIn == 2) {
      m = 2;
    }
    if (dataIn == 3) {
      m = 3;
    }
    if (dataIn == 4) {
      m = 4;
    }
    if (dataIn == 5) {
      m = 5;
    }
    if (dataIn == 6) {
      m = 6;
    }
    if (dataIn == 7) {
      m = 7;
    }
    if (dataIn == 8) {
      m = 8;

    }
    if (dataIn == 9) {
      m = 9;
    }
    if (dataIn == 10) {
      m = 10;
    }
    if (dataIn == 11) {
      m = 11;
    }

    if (dataIn == 12) {
      m = 12;
    }
    if (dataIn == 14) {
      m = 14;
    }
    // Set speed
    if (dataIn >= 16) {
      wheelSpeed = dataIn * 10;
      Serial.println(wheelSpeed);
    }
  }
  if (m == 4) {
    moveSidewaysLeft();
  }
  if (m == 5) {
    moveSidewaysRight();
  }
  if (m == 2) {
    moveForward();
  }
  if (m == 7) {
    moveBackward();
  }
  if (m == 3) {
    moveRightForward();
  }
  if (m == 1) {
    moveLeftForward();
  }
  if (m == 8) {
    moveRightBackward();
  }
  if (m == 6) {
    moveLeftBackward();
  }
  if (m == 9) {
    rotateLeft();
  }
  if (m == 10) {
    rotateRight();
  }

  if (m == 0) {
    stopMoving();
  }
  //Serial.println(dataIn);
  // If button "SAVE" is pressed
  if (m == 12) {
    if (index == 0) {
        digitalWrite(in11, LOW);
        digitalWrite(in21, LOW);
        digitalWrite(in31, LOW);
        digitalWrite(in41, LOW);
        digitalWrite(in12, LOW);
        digitalWrite(in22, LOW);
        digitalWrite(in32, LOW);
        digitalWrite(in42, LOW);

    }

    // save position into the array
  }
  //if (m == 14) {
   // runSteps();
    //if (dataIn != 14) {
      //stopMoving();
      // Clear the array data to 0
   // }
  //}

}

//method to read the array from what was saved by the save button and run it step by step
//void runSteps() {}

void moveForward() {
        digitalWrite(in11, HIGH);
        digitalWrite(in21, LOW);
        digitalWrite(in31, HIGH);
        digitalWrite(in41, LOW);
        digitalWrite(in12, HIGH);
        digitalWrite(in22, LOW);
        digitalWrite(in32, HIGH);
        digitalWrite(in42, LOW);
        Serial.println("Motor: move Forward");
}

void moveBackward() {
        digitalWrite(in11, LOW);
        digitalWrite(in21, HIGH);
        digitalWrite(in31, LOW);
        digitalWrite(in41, HIGH);
        digitalWrite(in12, LOW);
        digitalWrite(in22, HIGH);
        digitalWrite(in32, LOW);
        digitalWrite(in42, HIGH);
        Serial.println("Motor: move Backward");
}

void moveSidewaysLeft() {
        digitalWrite(in11, HIGH);
        digitalWrite(in21, LOW);
        digitalWrite(in31, LOW);
        digitalWrite(in41, HIGH);
        digitalWrite(in12, LOW);
        digitalWrite(in22, HIGH);
        digitalWrite(in32, HIGH);
        digitalWrite(in42, LOW);
        Serial.println("Motor: move Sideways Left");
}
void moveSidewaysRight() {
        digitalWrite(in11, LOW);
        digitalWrite(in21, HIGH);
        digitalWrite(in31, HIGH);
        digitalWrite(in41, LOW);
        digitalWrite(in12, HIGH);
        digitalWrite(in22, LOW);
        digitalWrite(in32, LOW);
        digitalWrite(in42, HIGH);
        Serial.println("Motor: move Sideways right");
}

void moveRightForward() {
        digitalWrite(in11, LOW);
        digitalWrite(in21, LOW);
        digitalWrite(in31, HIGH);
        digitalWrite(in41, LOW);
        digitalWrite(in12, HIGH);
        digitalWrite(in22, LOW);
        digitalWrite(in32, LOW);
        digitalWrite(in42, LOW);
        Serial.println("Motor: move Right Forward");
}

void moveLeftBackward() {
        digitalWrite(in11, LOW);
        digitalWrite(in21, LOW);
        digitalWrite(in31, LOW);
        digitalWrite(in41, HIGH);
        digitalWrite(in12, LOW);
        digitalWrite(in22, HIGH);
        digitalWrite(in32, LOW);
        digitalWrite(in42, LOW);
        Serial.println("Motor: move left Back ward");
}

void moveLeftForward() {
        digitalWrite(in11, LOW);
        digitalWrite(in21, LOW);
        digitalWrite(in31, LOW);
        digitalWrite(in41, HIGH);
        digitalWrite(in12, LOW);
        digitalWrite(in22, HIGH);
        digitalWrite(in32, LOW);
        digitalWrite(in42, LOW);
        Serial.println("Motor: move Left Forward");
}

void moveRightBackward() {
        digitalWrite(in11, LOW);
        digitalWrite(in21, HIGH);
        digitalWrite(in31, LOW);
        digitalWrite(in41, LOW);
        digitalWrite(in12, LOW);
        digitalWrite(in22, LOW);
        digitalWrite(in32, LOW);
        digitalWrite(in42, HIGH);
        Serial.println("Motor: move Right Backward");
}

void rotateLeft() {
        digitalWrite(in11, HIGH);
        digitalWrite(in21, LOW);
        digitalWrite(in31, LOW);
        digitalWrite(in41, HIGH);
        digitalWrite(in12, HIGH);
        digitalWrite(in22, LOW);
        digitalWrite(in32, LOW);
        digitalWrite(in42, HIGH);
        Serial.println("Motor: rotate Left");
  }

void rotateRight() {
        digitalWrite(in11, LOW);
        digitalWrite(in21, HIGH);
        digitalWrite(in31, HIGH);
        digitalWrite(in41, LOW);
        digitalWrite(in12, LOW);
        digitalWrite(in22, HIGH);
        digitalWrite(in32, HIGH);
        digitalWrite(in42, LOW);
        Serial.println("Motor: rotate Right");
  }

void stopMoving() {
        digitalWrite(in11, LOW);
        digitalWrite(in21, LOW);
        digitalWrite(in31, LOW);
        digitalWrite(in41, LOW);
        digitalWrite(in12, LOW);
        digitalWrite(in22, LOW);
        digitalWrite(in32, LOW);
        digitalWrite(in42, LOW);
        Serial.println("Motor: off");
  }

A DC motor does not know steps, unless you introduce them by using something like a rotary encoder

Could it be, that you mean the current state (i.e. the contents of the variables inXX and enXX of the DC motors instead of the "position"?  I guess so because you said, you want to reset the position to zero on a save event, and what you do then is setting the inXX all to LOW. That would be easy to achieve. But saving the current position of the wheels is not that easy. Then you do define what position means. Do your wheels turn constantly, like car wheels, or are they more like servos and have a well defined position within 360°? (I guess they are like car wheels when I see the prog.)

This is the type DC motor am using:
https://www.amazon.com/Electric-Magnetic-Gearbox-Plastic-Yeeco/dp/B07DQGX369/ref=sr_1_fkmr0_1?keywords=OLatus+DC+BO+Motor+Dual+shaft+Smart+Car+Robot+Gear+Motor+for+Arduino+%282+Pieces%29&qid=1573914307&sr=8-1-fkmr0
What I mean by positions are that  if I press the buttons to make car move forward ,side way left then backward --> all these I want it to be save in the array so when I press the run button it will redo all these position manually by looping in the array.
I hope you understood me

that does not explain what positions means ... you basically repeated what you already said in your post

As I understood it from your comments, your aim is not really to store the actual positions of the wheels or your robot, but to save and repeat the actions, that the robot did. (Meaning you don't have a "position" here, but only a measurement of wheel speed)
If that is true, then you can save this in a 2-dimensional array with the element form {speed_wheel_1, speed_wheel2, duration}. So each action is defined by the speeds of both wheels and the duration of the action, for example:
Driving forward at full speed for 3s, turn on the spot to the left with half speed for 1.5s, drive a curve with motor left full speed and motor right half speed for 5s

This would translate to the following elements:
{100, 100, 3000}
{-50, 50, 1500}
{100, 50, 5000}

The speeds have to be read in percent here and the durations in milliseconds.

Obviously you are controlling the robot over serial. I don't completely understand your code (also as you didn't show us the full code), so I will give some hints, how you can go on from here:
You are obtaining the speeds from serial into the variable wheelSpeed. We cannot see, how you are using this variable later, but you can use it in some way to set the speed fields of a new array element.
Then you need the duration. The Arduino framework has a function for that: millis(). It will return the number of milliseconds since the startup, much like a clock. When an action starts (when the corresponding serial command is received), you have to set a timestamp:
unsigned long timestamp;
...
timestamp = millis();

When the action ends (either because it actually only had a limited duration, or because another command is received), you calculate the difference between the current millis value and the timestamp. This is the duration.
So the corresponding code parts will look like this:
unsigned long timestamp = 0; // Define timestamp at global scope
unsigned int current_element = 0; // A variable to keep track of the current array element
int actions[10][3]; // Array to hold up to 10 actions

void start_action(byte speed_left, byte speed_right){
    actions[current_element][0] = speed_left;
    actions[current_element][1] = speed_right;
    timestamp = millis();
    // Start the action with the provided speeds here
}

void end_action(unsigned int element_index){
    actions[current_element][2] = millis() - timestamp;
    current_element++;
    if(current_element >= 10){
        // Do something to prevent more actions. Maybe just disable recording.
}

For repeating the actions, you just have to cycle over the array and set the corresponding speeds for the corresponding time.
If you want to go fancy, you can ditch the 2-dimensional array and instead define a struct for an action:
struct Action{
    int speed_left;
    int speed_right;
    unsigned long duration;
};

Then you can create a simple array of the Action objects and reference the fields by name:
Action actions[10];
...
actions[1].duration = millis() - timestamp;

thank you so much
I've updated my question + the code.
but I  have another problem if you can help me with which is: when my phone send data(any number 1-14) via Bluetooth it doesn't show the output on the wheels!!
example: when I pressed button move forward which send number 2 to the Bluetooth module, the wheels doesn't move.

I don't know what exactly you are sending, but I guess you are sending an ASCII "2" over bluetooth. In your code you only check for the decimal values of the read data. An ASCII-encoded "2" equals a decimal 50. Now you can either change the sending code to send raw decimal values instead of ASCII-encoded data, or you an change the receiving code to react to ASCII encoded data. The last can be done by changing the numbers in the if statements to the appropriate numbers of the ASCII characters. Or just simply writing them as characters: if(dataIn == '2')

source: Stack Exchange

license: Creative Commons - Attribution Share-Alike - https://creativecommons.org/licenses/by-sa/4.0/

sort: votes


text:
Why are embedded C codes are ending with "while(1)"?
I always see while(1); at the end of the C codes such as:
#include <stdio.h>

int main(void)
{

    //Some code here...

    while(1);

}

If I'm not wrong that it always exists, what is the function of that?
edit:
#include <stdio.h>

int main(void)
{

     while(1);
    //Some code here...

}

; is empty command. it is the nothing repeated  with while (1)

Well, to be pedantic, you are not allowed to have such cycles in a C or C++ program. In C and C++ an infinite cycle is required to have observable behavior (input/output, atomic access, volatile access etc.). If an infinite cycle does not have any such behavior (e.g. an empty cycle), the behavior of your code is undefined. In practice compilers may (and will) completely discard such cycles from the code, meaning that the program will not stop there. The only reason it "works" in AVR-GCC is that it is somewhat of an idiom in embedded programming, so they are allowed as an exception.

It's because in non-embedded systems, when main() finishes it returns to the operating system and the program is removed from memory. In embedded systems there's no operating system to return to, and the program can't be removed from memory.  So... what do you do when the program is finished?  Simple: you do nothing. Forever.  And that's what while(1); does.
Some compilers and C libraries automatically add while(1); in the exit() routine, but not all do - so it's better to do it explicitly than rely on something that may or may not be there.

Its very confusing syntax-wise as well. The "while" is used with curly brackets. But here at the end there is while(1);. How can we articulate this to understand how it functions on the code?

while, like if, can be used with curly brackets. It doesn't have to be. while(something) dosomething(); and while(something) { dosomething(); } are the same. You only need the brackets if you want to do more than one thing.

Does that mean(for forever loop) while(1) can be on the top of the code instead of at the end?

You can use it anywhere you like. You also sometimes see for (;;); which means the same thing... "for ever..."

To be more specific. Please see my edit. I added another code to my question. Are these two codes will act the same way?

The while(1) will do nothing forever. The key here is: forever. It won't ever stop doing nothing. Any code after it will never be executed, because it's still doing nothing.

while(1);

should actually be read as
while(true)
{
  // No statements
}

// Statements following here will never be executed

The result is: while the condition is true, it performs no statements. Since the condition true is always true, it keeps performing the statements within the { and }, which are no statements, resulting that no statement is executed but it will also not executing statements after the while.

The "while" is normally used with curly brackets. But here at the end there is while(1);. How can we articulate this to understand how it functions on the code?

Majenko already answered this, in about similar words: while (true)  where x can be a single statement like while (true) a = 4; (thus a = 4; is the statement). Or it can be a compound (multiple statements) in the form of while (true) { a = 4; b = 5; }. In the case of your question a single (but empty) statement is used: while (true) /* empty statement */ ;

Can while(1) be on the top of the code instead of at the end? Would it work same way? I dont get why they put it at very end instead of at very beginning.

It can be anywhere in the code, however, note that code after such while statement, will not be performed anymore as the MCU keeps executing … nothing.

You wrote "the code after such while statement, will not be performed anymore ". If the code after while(1)  will not be executed so how come putting while(1); at the beginning will let the following code to be looped forever?

@Genzo I meant after the complete while statement... the statements inside the brackets (while (…) { … }) all belong to the while statement; the while statement ends after the ; (in while (1);) or after the } (in while (1) {…}). Statements after the ; or } will not be executed.

To be more specific. Please see my edit. I added another code to my question. Are these two codes will act the same way?

No, they are not the same. The first executes , and then waits indefinitely. The second waits indefinitely, and then executes  (which is never).

They will not perform the same way: in the first example, first the statements are executed (some code here), and the MCU keeps doing nothing (while), in the second example, the MCU starts doing nothing (while), never reaching the statements (some code here).

The line while(1) in a C program creates an infinite loop- this is a loop that never stops executing. It executes over and over and over again, unless the program is intentionally stopped or there is some condition under this loop that gets met that takes the program out of this infinite loop.
Let's take the following C program.
include
void main()
{
printf("Hello World");
while(1);
}
This program prints the words "Hello World" to the standard output, which is most likely a serial port.
After it prints it, it goes to execute the next line, while(1). This line makes the microcontroller sit and wait, forever or until the microcontroller is reset.

all_licenses: Creative Commons - Attribution Share-Alike - https://creativecommons.org/licenses/by-sa/4.0/


text:
Using ESP32 Cam internal RAM
I am new to the ESP32 series of boards.I powered up my ESP32 Cam board using the Vcc pin supplying about 7.4V using two 18650 batteries only to find out later that was a completely incorrect way to power up the board.
Now I cannot get my board to flash. I am guessing that the SPI SRAM on my board is burnt because when I measure the Vcc at the SPI SRAM chip it doesn't read 3.3V as its supposed to.
Also there is continuity between the ground and Vcc of the SPI SRAM chip. And it heats up excessively when powered up using 5V.
However I read in the documents that the ESP32 has a few bytes of internal RAM in it.
I was planning to use the ESP32 Cam as an IP Camera for one of my projects.
I want to know if its possible to desolder the SPI SRAM chip (IPS1704) and somehow use the internal ram to get my job done or else if I have to buy a new board.
Thanks in advance

It's dead. You killed it. Well done.  The ESP32 has lots of internal RAM. However it has no internal flash, and requires external flash to operate. External PSRAM is optional. If you toasted the flash chip you will have to replace it (it's inside the metal can). If you toasted the PSRAM chip you can remove it. If you toasted the ESP32 itself then you will have to replace that too. If you toasted all three then all three will have to be replaced. By the time you've done all that you will be asking yourself "was it worth it?" - just get a new one.

If there is continuity between 3.3V and GND your board is dead. Most likely (from my experience) the ESD protection diode on the ESP module is fried which leaves you no chance to repair it. Even if you could remove the shielding to change the diode, you have no chance to find out which components are damaged. Considering the cost of the whole device, repairing it doesn't seem to be worth the effort.
For your understanding:

reading less than 3.3V on the external RAM means you would also read the same voltage at the ESP's Vcc. It's not an indicator for the RAM being dead
the ESP32 has plenty of internal ram (some hundreds of kB remain for use of your sketch after subtracting what the RTOS uses)
the ESP32 (chip) has no internal flash, so there is always an external flash on the ESP module
the ESPcam has an additional SPI RAM assembled externaly to the module. The internal RAM is by far too small to do any useful image processing at any desired resolution, so using only the internal RAM does not work

It appears you have some paper weights. I would suggest before you make more read the instructions and be sure you understand them. It is much less costly that way. There may be some light at the end of the tunnel, if the ESP32 and it is mounted on another board, and it possibly has a voltage regulator, you might be OK, but I have two neighbors chances, Slim and None. Read the data or as they say RTFM.


text:
Driving an enable pin low via arduino
I have the following V regulator, that has an enable pin that i'd like to control via an arduino:  https://www.pololu.com/product/2582
I tried using a relay (ELEGOO Relay Module 4 Channel DC 5V with Optocoupler for Arduino) with the following pins:
Relay pin1: 0v of 24v battery
Relay pin2: enable pin of pololu v regulator
I then enabled the above relay via an arduino digital out pin.
However it seemed to have no effect, the regulator never shut off.
If i manually join the two relay pins, pin1 and pin2 the regulator does shut off though ..  A and B in picture.
Any ideas why the relay is not working via the arduino?  or should it work? am i doing something wrong?  I am not too familiar with pull up or pull downs..
I have since disconnected everything and put it away, i am just looking for theory help, confirmation it should wotk.   I managed to get the relay working for leds at the time .. would the fact that current flows in the other direction for this pull down relay setup, be my issue and that i should swap around wires A and B?
(As a workaround i used a relay to switch on or enable the 24v Vin line out from the battery instead, that seemed to work okay..)

A circuit diagram and details of the arduino code used would help in diagnosing the problem

what is relay pin1? ... for all we know, it may be an unused pin

Unless you require galvanic isolation (and it's highly unlikely that you do) you don't need the relay.
The EN pin only needs to be above 1.3V to turn the unit on, or below 0.9V to turn it off. The voltage range is anything up to 30V.
Just connect the EN pin directly to an Arduino IO pin. Write a HIGH for on and a LOW for off.  And of course connect the grounds together.

thanks for the tip! I will use this next time, but still wondering what was my issue.


text:
Trying to get a servo motor to move to certain setpoints when a control potentiometer shows certain values
Here's my Code so far:

void setup() {
  Serial.begin(9600);
}

void loop() {
  float a;
  float angle;
  int setpoint;
  a = analogRead(A1);
  angle = map(a, 0, 1023, 0, 180);

  if ( (angle>0) && (angle<70) )
  {
    setpoint=50;
  }
  if ( (angle>70) && (angle<110) )
  {
    setpoint=90;

  }
  if ( (angle>110) && (angle<180) )
  {
    setpoint=130;

  }

  Serial.print(setpoint);
  Serial.print(",");
  Serial.println(angle);
}

I'm using an Arduino Genuino.
How can I get the servo motor to move to the setpoints and stop when the angle is between the eligible range?
just wanted to update showing how i solved this problem using the following code.
void setup() {
pinMode(5,OUTPUT);
pinMode(6,OUTPUT);
Serial.begin(9600);
}
void loop() {
float a;
float b;
float desired_angle;
float actual_angle;
float error;
float motor;
float setpoint;

a=analogRead(A1);
desired_angle=map(a,0,1023,0,180);

b=analogRead(A2);
actual_angle=map(b,125,920,0,180);

 if ( (desired_angle>0) && (desired_angle<70) )
{
  setpoint=50;
}
if ( (desired_angle>70) && (desired_angle<110) )
{
  setpoint=90;

}
if ( (desired_angle>110) && (desired_angle<180) )
{
  setpoint=130;

}

error=(setpoint-actual_angle);
motor=desired_angle/270*255;

if (error>0)
{
  digitalWrite(5,LOW);
  analogWrite(6,motor);
}
if (error<0)
{
  digitalWrite(6,LOW);
  analogWrite(5,-1*motor);
}

Serial.print(actual_angle);
Serial.print(",");
Serial.println(setpoint);

}

Have you seen the Servo library, that comes with the Arduino IDE? With it you can set the servo to specific angles

You can refer to a similar project - Arduino - Servo Motor controlled by Potentiometer


text:
How would i add PID control to this servo motor control program. potentiometer
I have installed an AutoPID library (by ryan downing version 1.0.3) to my arduino IDE, but im not sure which parts i will have remove to get the PID to control the servo angle.
Thanks for the reply, but i know i have not used the library in the sketch but i am unsure how to combine it, i have an example code for how to use the AutoPID, i will add it below.
void setup() {
  pinMode(5,OUTPUT);
  pinMode(6,OUTPUT);
  Serial.begin(9600);
}
void loop() {
  float a;
  float b;
  float desired_angle;
  float actual_angle;
  float error;
  float motor;
  float setpoint;

  a=analogRead(A1);
  desired_angle=map(a,0,1023,0,180);

  b=analogRead(A2);
  actual_angle=map(b,125,920,0,180);

   if ( (desired_angle>0) && (desired_angle<70) )
  {
    setpoint=50;
  }
  if ( (desired_angle>70) && (desired_angle<110) )
  {
    setpoint=90;

  }
  if ( (desired_angle>110) && (desired_angle<180) )
  {
    setpoint=130;

  }

  error=(setpoint-actual_angle);
  motor=desired_angle/270*255;

  if (error>0)
  {
    digitalWrite(5,LOW);
    analogWrite(6,motor);
  }
  if (error<0)
  {
    digitalWrite(6,LOW);
    analogWrite(5,-1*motor);
  }

  Serial.print(actual_angle);
  Serial.print(",");
  Serial.println(setpoint);

}

and here is the example code
#include <AutoPID.h>

// PID settings and gains
#define OUTPUT_MIN 0
#define OUTPUT_MAX 255
#define KP 0.12
#define KI 0.8
#define KD 0.003

// Variables
double analogReadValue, setPoint, outputVal;

// Input/output variables passed by reference, so they are updated automatically
AutoPID myPID(&analogReadValue, &setPoint, &outputVal, OUTPUT_MIN, OUTPUT_MAX, KP, KI, KD);

// Setup timeStep
void setup() {
  myPID.setTimeStep(500);
}

void loop() {
  setPoint = analogRead(A3);          // Set our desired point (read the POT)
  analogReadValue = analogRead(A1);   // Read our output voltage
  myPID.run();                        // Run the PID
  analogWrite(6, outputVal); // Write the PID result to the output
}

you may have installed the library, but you do not use it in your sketch

Am I seeing it correctly, that you use pin 6 to drive the motor forward and pin 5 to drive it backwards?

How can you have a negative duty cycle to PWM? Your code doesn't make much sense.  Are you driving a real servo motor, or are you driving a DC motor through a driver which you are trying to turn into a servo motor? Servo motors have their own pot and PID feedback built into them.


text:
How can I combine this two codes in one program? And use it in javafx application
The first one reads from sensors and displays it, the second code receives an incoming byte to control LEDs. I tried to combine it but i don't know why it doesn't work. Please help me out. I need it for my project to run both of them.
In my java netbeans i have two GUIs. For monitoring(first code) and controlling(second). I want it both to run with only one combined code in arduino.
I tried to use if else statement for the arduino to know what to do. Not working also. I think i just missed something
const int model = 0;

void setup() {
Serial.begin(9600);

}

float sensitivity[] ={
      0.185,// for ACS712ELCTR-05B-T
      0.100,// for ACS712ELCTR-20A-T
      0.066// for ACS712ELCTR-30A-T

     } ;

void loop() {
static unsigned long count=0;
static float Vsum=0;
float Vave=0;
int volt = analogRead(A1);

float voltage= map(volt,0,1023, 0, 2500)/100.0;
Vsum=Vsum+voltage;

static float Csum=0;
float Cave=0;
 float milli = pow(10, -3);
 float current = (voltage / sensitivity[model]);
 float current1 = current*milli;

Csum=Csum+current1;
count++;
Vave=Vsum/count;
Cave=Csum/count;
float watts = Vave*Cave;

long milisec = millis();
long time=milisec/1000;

 float energy = (watts*time)/3600;
Serial.print(energy,6);
Serial.println("Wh");
delay(1000);

}

And this is the second code. This receives byte from java to control LEDs
int incomingByte;

void setup() {
  Serial.begin(9600);
  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(13, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(11, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(2, OUTPUT);
  pinMode(1, OUTPUT);
  pinMode(0, OUTPUT);

}

void loop() {
 // if (Serial.available()>0) {
    incomingByte = Serial.read();

    if(incomingByte == 0x01){
      digitalWrite(13, HIGH);
     Serial.println("ON");

    }else if(incomingByte == 0x00){
      digitalWrite(13, LOW);
       Serial.println("OFF");
    }

    if(incomingByte == 0x02){
      digitalWrite(12, HIGH);
    }else if(incomingByte == 0x03){
      digitalWrite(12, LOW);
    }

    if(incomingByte == 0x04){
      digitalWrite(11, HIGH);
    }else if(incomingByte == 0x05){
      digitalWrite(11, LOW);
    }

    if(incomingByte == 0x06){
      digitalWrite(10, HIGH);
    }else if(incomingByte == 0x07){
      digitalWrite(10, LOW);
    }

    if(incomingByte == 0x08){
      digitalWrite(9, HIGH);
    }else if(incomingByte == 0x09){
      digitalWrite(9, LOW);
    }

    if(incomingByte == 0x10){
      digitalWrite(8, HIGH);
    }else if(incomingByte == 0x11){
      digitalWrite(8, LOW);
    }

    if(incomingByte == 0x12){
      digitalWrite(7, HIGH);
    }else if(incomingByte == 0x13){
      digitalWrite(7, LOW);
    }

    if(incomingByte == 0x14){
      digitalWrite(6, HIGH);
    }else if(incomingByte == 0x15){
      digitalWrite(6, LOW);
    }

    if(incomingByte == 0x16){
      digitalWrite(5, HIGH);
   }else if(incomingByte == 0x17){
      digitalWrite(5, LOW);
    }

    if(incomingByte == 0x18){
      digitalWrite(4, HIGH);
    }else if(incomingByte == 0x19){
      digitalWrite(4, LOW);
    }

    if(incomingByte == 0x20){
      digitalWrite(3, HIGH);
    }else if(incomingByte == 0x21){
      digitalWrite(3, LOW);
    }

    if(incomingByte == 0x22){
      digitalWrite(2, HIGH);
    }else if(incomingByte == 0x23){
      digitalWrite(2, LOW);
    }

    if(incomingByte == 0x24){
      digitalWrite(1, HIGH);
    }else if(incomingByte == 0x25){
      digitalWrite(1, LOW);
    }

    if(incomingByte == 0x026){
      digitalWrite(0, HIGH);
    }else if(incomingByte == 0x027){
      digitalWrite(0, LOW);
    }

  delay(100);
}

This is my sample code from javafx application
    try {
        if (tbLED12.isSelected()) {
            if (arduinoPort != null) {
                arduinoPort.writeByte((byte) 0x02);
                System.out.println("LED 12 ON");
            } else {
                System.out.println("arduinoPort not connected!");
            }
        } else {
            if (arduinoPort != null) {
                arduinoPort.writeByte((byte) 0x03);
                System.out.println("LED 12 OFF");
            } else {
                System.out.println("arduinoPort not connected!");
            }
        }
    } catch (SerialPortException ex) {
        Logger.getLogger(JavaFXApplication11.class.getName())
                .log(Level.SEVERE, null, ex);
    }

You didn't show us your combined code, so we cannot say, what is wrong with it. There are plenty of question like "Need to combine these two codes" on this site. Have you looked into them?

sorry but i cant add my combined code coz its too long but i just add my first code to the second one and it became faulty, but if i run them separately they work well.

don't set pinmode for 0 and 1. it is Serial RX and TX.

Even if you simply put both codes in one file, that shouldn't be too long for the question editor. Otherwise you should create a minimal complete example. Asking how to combine 2 codes without the problematic combined code is a very broad and general question, that was asked and answered many times here. Without your combined code and descriptions about how it is faulty, every answer to prone to a long comment discussion to get to the bottom of the problem (as we see with Peters answer below). This site is not good for this; it's not a forum.

Combining this code is all about synchronizing the reading of the sensors with the control sequences that are sent from the Java program.
The second program reads the Serial port continuously in the loop method. A better way would be to use a serial interrupt for this. Let me give you an example.
My code is written without an IDE so it is just an untested example. But hopefully I did not include errors ;-).
I use the first program as a base:
const int model = 0;
float sensitivity[] = {
  0.185,// for ACS712ELCTR-05B-T
  0.100,// for ACS712ELCTR-20A-T
  0.066// for ACS712ELCTR-30A-T
} ;

unsigned long count = 0;
float         Vsum  = 0;
float         Csum  = 0;

long startTime = 0;

void setup()
{
  Serial.begin(9600);
  // setting the LED pins to output
  // I use a loop
  for ( int i = 2; i < 14; i++ )
  {
    pinMode( i, OUTPUT );
  }
}

void loop()
{
  float Vave = 0;
  float Cave = 0;
  // pow is to expensive for the loop
  // I use a constant
  float milli = 0.001;

  if ( startTime = 0 ) startTime = millis();

  float voltage = map( analogRead( A1 ), 0, 1023, 0, 2500 ) / 100.0;
  Vsum = Vsum + voltage;

  float current  = ( voltage / sensitivity[ model ] );
  float current1 = current * milli;

  Csum = Csum + current1;

  count++;

  Vave = Vsum / count;
  Cave = Csum / count;
  float watts = Vave * Cave;

  // you did not use a time delta here,
  // but energiy it work over a time period
  long timeDelta = ( millis() - startTime ) / 1000;

  float energy = ( watts * timeDelta ) / 3600;
  Serial.print( energy, 6 );
  Serial.println( "Wh" );

  serialHandler();

  delay( 1000 );
}

void serialHandler()
{
   delay( 200 );
   while ( Serial.available() )
   {
     // get the new byte:
     int inByte = Serial.read();

     if(inByte == 0x01)
     {
       digitalWrite(13, HIGH);
       //Serial.println("ON");
     }
     else if(inByte == 0x00)
     {
       digitalWrite(13, LOW);
       //Serial.println("OFF");
     }
     else if(inByte == 0x02) digitalWrite(12, HIGH);
     else if(inByte == 0x03) digitalWrite(12, LOW);
     else if(inByte == 0x04) digitalWrite(11, HIGH);
     else if(inByte == 0x05) digitalWrite(11, LOW);
     else if(inByte == 0x06) digitalWrite(10, HIGH);
     else if(inByte == 0x07) digitalWrite(10, LOW);
     else if(inByte == 0x08) digitalWrite(9,  HIGH);
     else if(inByte == 0x09) digitalWrite(9,  LOW);
     else if(inByte == 0x10) digitalWrite(8,  HIGH);
     else if(inByte == 0x11) digitalWrite(8,  LOW);
     else if(inByte == 0x12) digitalWrite(7,  HIGH);
     else if(inByte == 0x13) digitalWrite(7,  LOW);
     else if(inByte == 0x14) digitalWrite(6,  HIGH);
     else if(inByte == 0x15) digitalWrite(6,  LOW);
     else if(inByte == 0x16) digitalWrite(5,  HIGH);
     else if(inByte == 0x17) digitalWrite(5,  LOW);
     else if(inByte == 0x18) digitalWrite(4,  HIGH);
     else if(inByte == 0x19) digitalWrite(4,  LOW);
     else if(inByte == 0x20) digitalWrite(3,  HIGH);
     else if(inByte == 0x21) digitalWrite(3,  LOW);
     else if(inByte == 0x22) digitalWrite(2,  HIGH);
     else if(inByte == 0x23) digitalWrite(2,  LOW);
     //else if(inByte == 0x24) digitalWrite(1,  HIGH);
     //else if(inByte == 0x25) digitalWrite(1,  LOW);
     //else if(inByte == 0x26) digitalWrite(0,  HIGH);
     //else if(inByte == 0x27) digitalWrite(0,  LOW);
   }
}

EDIT:
I changed the code a little bit according to Juraj comments. I also
commented the `Serial.println' s that had reported the LED 13 state.

serialEvent is not an interrupt. outside the loop() the core tests Serial.available() and if is not 0 it calls serialEvent() .

I cannot control the LEDs. One LED only will light up when i run the program even though i switched them all on. But the monitoring works

@Juraj And again, i learned something. Meanwhile I'm at home and could try out my program, and it didn't worked as I expected. And you gave the explanation. And now I feel sad and thinking about the ice cream in my refrigerator and if I should eat it. Hmmm yes I'll do that. But before I find a better solution for the problem. Perhaps I renmae the funtion, wait a little bit after sending the values to java and then trigger the former event callback. Then I wait one second and once again.

@RicaJacutina Which LED does work, 13 perhaps? Thats the built in LED. Did you connect the others correctly. Are you sending the right values? Are you aware that for LED 13, you have to send an odd value (1) to switch it on. The others are switched on by even values ( 2,4,6,8,10,12,). This functionallity should work I tried it a few minutes ago with my own setup.

@PeterPaulKiefer I tried your code, The monitoring works but when i control LED it does not work properly on my java gui. Yes the 13 switched on, but does not switch off. When i tried to run the program again, no LED worked. :( I am aware that 13 light up to odd value and others on even values. And i am sure that i connected it all the right way.

@RicaJacutina There is one thing I'm unsure about and that's: Can the Java GUI handle the String "ON" and "OFF" you send with Serial.println("OFF"); or Serial.println("ON");? (in the LED 13 cases) Perhaps your GUI crashes because it does not receive values, And then, will not not send the other control values? Have the send backs some meaning or can you savely removed them? Then, please try removing them. Or do not control LED 13 only the other ones, to see if that works.  BTW: Is the Java GUI a Processing application?

I am using javafx application. I added my my java code above. The third one

The interesting part is the receiving part. Not the sending part. What happens if the your program receives the string "ON" when the LED 13 is set on? Can that lead to an exception that closes the Serial port for example. Have you seen @Juraj comment to your Question. That's something I've complete missed. As pins 0 and 1 are used for the serial connection, you can not use it to drive LEDs. I will change my code a littlle bit. Perhaps then it works for your setup.

@RicaJacutina So; I changed the code. But the LEDs 0 and 1 won't work with that version.

void serialEvent();
const int model = 0;
float sensitivity[] = {
  0.185,// for ACS712ELCTR-05B-T
  0.100,// for ACS712ELCTR-20A-T
  0.066// for ACS712ELCTR-30A-T
} ;

unsigned long count = 0;
float         Vsum  = 0;
float         Csum  = 0;
float         DecPowerRate = 9.8623 ;//kWh
long          startTime = 0;
float         no_voltage = 0.00;
//float           address = 0;
float         no_CURRENT = 0.000;
float energy;
long timeDelta;float consumption;float Kwatts;
void setup()
{
  Serial.begin(9600);
  // setting the LED pins to output
  // I use a loop
  for ( int i = 2; i < 14; i++ )
  {
    pinMode( i, OUTPUT );
  }
}

void loop()
{
  float Vave = 0;
  float Cave = 0;
  float milli = 0.001;// pow is to expensive for the loop

  if ( startTime = 0 ) startTime = millis();

  float voltage = map( analogRead( A1 ), 0, 1023, 0, 2500 ) / 100.0;

  if( voltage >no_voltage){
  Vsum = Vsum + voltage;

  float current  = ( voltage / sensitivity[ model ] );
  float current1 = current * milli;

  Csum = Csum + current1;

  count++;

  Vave = Vsum / count;
  Cave = Csum / count;
  Kwatts = (Vave * Cave)/1000;
  }
    if (Cave >no_CURRENT){
  // you did not use a time delta here,
  // but energiy it work over a time period
  timeDelta = ( millis() - startTime ) / 1000;
  energy = ( Kwatts * timeDelta ) / 3600;
  consumption = energy * DecPowerRate;
}

  //Serial.print( energy, 6 );
 // Serial.println( "kWh" );

  Serial.print( "P " );
  Serial.print( consumption, 6 );
  Serial.println( "/kWh" );
  delay( 1000 );

}
// naming convention for a serial interrupt
void serialEvent()
{
   while (Serial.available())
   {
 // get the new byte:
 int inByte = Serial.read();

 if(inByte == 0x01)
 {
   digitalWrite(13, HIGH);
   //Serial.println("ON");
 }
 else if(inByte == 0x00)
 {
   digitalWrite(13, LOW);
   //Serial.println("OFF");
 }
 else if(inByte == 0x02) digitalWrite(12, HIGH);
 else if(inByte == 0x03) digitalWrite(12, LOW);
 else if(inByte == 0x04) digitalWrite(11, HIGH);
 else if(inByte == 0x05) digitalWrite(11, LOW);
 else if(inByte == 0x06) digitalWrite(10, HIGH);
 else if(inByte == 0x07) digitalWrite(10, LOW);
 else if(inByte == 0x08) digitalWrite(9,  HIGH);
 else if(inByte == 0x09) digitalWrite(9,  LOW);
 else if(inByte == 0x10) digitalWrite(8,  HIGH);
 else if(inByte == 0x11) digitalWrite(8,  LOW);
 else if(inByte == 0x12) digitalWrite(7,  HIGH);
 else if(inByte == 0x13) digitalWrite(7,  LOW);
 else if(inByte == 0x14) digitalWrite(6,  HIGH);
 else if(inByte == 0x15) digitalWrite(6,  LOW);
 else if(inByte == 0x16) digitalWrite(5,  HIGH);
 else if(inByte == 0x17) digitalWrite(5,  LOW);
 else if(inByte == 0x18) digitalWrite(4,  HIGH);
 else if(inByte == 0x19) digitalWrite(4,  LOW);
 else if(inByte == 0x20) digitalWrite(3,  HIGH);
 else if(inByte == 0x21) digitalWrite(3,  LOW);
 else if(inByte == 0x22) digitalWrite(2,  HIGH);
 else if(inByte == 0x23) digitalWrite(2,  LOW);

   }
}

This worked! Thanks for the help! <3


text:
How do I adjust this?
We're supposed to create a Simon Says game for our final. My professor is telling me I shouldn't need to modify much with what I already have. but I'm so confused because it feels like starting over.
The way I have the Simon Says game now, it generates random numbers for each round but increments by one as the rounds increase. I need it to instead repeat the same sequence as the last and add a new random LED and so forth. Why is it so hard? What am I missing?
this is Arduino BTW
    int ledArray[10];
int buttonArray[10];
int loopCounter;
int ledCounter;
int inputCounter;
int game;
int waiting;
int pass;
int wait;
int timeUp;
int levelCounter[10];

//ALL THE BUTTONS & LEDS ARE GIVEN NAMES HERE SO THEY'RE EASIER TO REFERENCE
const int buttonRed = 6;
const int buttonYellow = 7;
const int buttonGreen = 9;
const int buttonBlue = 8;
const int r = 2;
const int y = 3;
const int b = 4;
const int g = 5;
const int x = 10;//LED ON PIN 10 IS THE WHITE LED USED FOR PROMPTS

///USED FOR PROCESSING AND COMPARING INPUTS
int randNumber;
int array_Cmp;

void youWin ()
{ digitalWrite(2, HIGH);
  delay(100);
  digitalWrite(2, LOW);
  delay(100);
  digitalWrite(3, HIGH);
  delay(100);
  digitalWrite(3, LOW);
  delay(100);
  digitalWrite(4, HIGH);
  delay(100);
  digitalWrite(4, LOW);
  delay(100);
  digitalWrite(5, HIGH);
  delay(100);
  digitalWrite(5, LOW);
  delay(100);
}

void ledPrompt()
//prompts player for inout during game after
//the LEDs have been picked randomly each round
{ digitalWrite(x, HIGH);
  delay(1000);
  digitalWrite(x, LOW);
  delay(50);
}

void gameHasStarted()
//LED prompt after first button
//is pushed to start the game
{
  digitalWrite(10, HIGH);
  delay(1000);
  digitalWrite(10, LOW);
  delay(100);
}

void fail() //LED sequence when the wrong input is entered
{ digitalWrite(2, HIGH);
  delay(1000);
  digitalWrite(3, HIGH);
  delay(1000);
  digitalWrite(4, HIGH);
  delay(1000);
  digitalWrite(5, HIGH);
  delay(1000);
  digitalWrite(2, LOW);
  delay(50);
  digitalWrite(3, LOW);
  delay(50);
  digitalWrite(4, LOW);
  delay(50);
  digitalWrite(5, LOW);
  delay(50);
}

void waitToStart() //cycles through LEDs while waiting to start the game
{ digitalWrite(2, HIGH);
  delay(250);
  digitalWrite(2, LOW);
  delay(250);
  digitalWrite(3, HIGH);
  delay(250);
  digitalWrite(3, LOW);
  delay(250);
  digitalWrite(4, HIGH);
  delay(250);
  digitalWrite(4, LOW);
  delay(250);
  digitalWrite(5, HIGH);
  delay(250);
  digitalWrite(5, LOW);
  delay(250);
}

int initiate()//FUNCTION WAITS FOR ANY OF 4 BUTTONS TO BE PUSHED TO START THE GAME
{ game = 0;
  waiting = 0;
  loopCounter = 1;
  randNumber = 0;
  ledCounter = 0;
  inputCounter = 0;
  pass = 2;
  //FUNCTION INCLUDES ALL OF THE ASSUMPTIONS NEEDED TO EXECUTE THE 1ST ROUND PROPERLY
  do {
    if (digitalRead(buttonRed) == LOW)
    { game = 2;
      gameHasStarted();
    }
    if (digitalRead(buttonGreen) == LOW)
    { game = 2;
      gameHasStarted();
    }
    if (digitalRead(buttonBlue) == LOW)
    { game = 2;
      gameHasStarted();
    }
    if (digitalRead(buttonYellow) == LOW)
    { game = 2;
      gameHasStarted();
    }
    else {
      waitToStart();
    }
  }
  while (game <= 1);
  delay(1000);
}//END OF GAME INITIATOR FUNCTION

int pickLed ()//FUNCTION WILL PICK A RANDOM LED
{
  do {
    randNumber = random(2, 6);
    digitalWrite(randNumber, HIGH);
    delay(500);
    digitalWrite(randNumber, LOW);
    delay(100);
    if (randNumber == 2)
    { ledArray[loopCounter] = 0;
      ledCounter++;
    }
    if (randNumber == 3)
    { ledArray[loopCounter] = 1;
      ledCounter++;
    }
    if (randNumber == 4)
    { ledArray[loopCounter] = 2;
      ledCounter++;
    }
    if (randNumber == 5)
    { ledArray[loopCounter] = 3;
      ledCounter++;
    }
  }
  while (ledCounter < loopCounter);
  ledPrompt();//signals for input after it's done picking the LEDs for each round
  wait == 1;
}//END OF PICKLED FUNCTION

int waitForInput ()//FUNCTION WILL WAIT FOR USER INPUT OR END GAME AFTER 10 SECONS OF NO INPUT
{
  do
  {
    if (digitalRead(buttonRed) == LOW)
    { digitalWrite(r, HIGH);
      inputCounter++;
      delay(150);
      digitalWrite(r, LOW);
      delay(150);
      buttonArray[loopCounter] = 0;
    }
    if (digitalRead(buttonYellow) == LOW)
    { digitalWrite(y, HIGH);
      inputCounter++;
      delay(150);
      digitalWrite(y, LOW);
      delay(150);
      buttonArray[loopCounter] = 1;
    }
    if (digitalRead(buttonBlue) == LOW)
    { digitalWrite(b, HIGH);
      inputCounter++;
      delay(150);
      digitalWrite(b, LOW);
      delay(150);
      buttonArray[loopCounter] = 2;
    }
    if (digitalRead(buttonGreen) == LOW)
    { digitalWrite(g, HIGH);
      inputCounter++;
      delay(150);
      digitalWrite(g, LOW);
      delay(150);
      buttonArray[loopCounter] = 3;
    }
    else {
      wait++; //the wait counter is incremented each time the loop reads all 4 input and no input has been made
      delay(250);
    }//the delay here adds 1/4th of a second each time no input is made (every 4 loop here is 1 second);

    if (wait > 40) //40 divided by 0.25 = 10 seconds of wait
    {
      pass = 0;
      fail();
    } //after waiting 10 seconds for input the game automatically fails.
  }
  while (inputCounter < loopCounter);//WAIT FOR INPUT
}   //END OF WAIT_FOR_INPUT FUNCTION

int scoreInput ()//START OF SCORE_INPUT FUNCTION
{ if (memcmp(ledArray, buttonArray, sizeof(ledArray)) == 0)
  {
    delay(1000);
    loopCounter++;
    ledCounter = 0;
    inputCounter = 0;
    game++;
    pass = 2;
  }
  else
  {
    loopCounter = 0;
    ledCounter = 0;
    inputCounter = 0;
    pass = 0;
  }
}
//END OF SCORE_INPUT FUNCTION

void setup ()
{ randomSeed(analogRead(0));
  pinMode (r, OUTPUT);
  pinMode (y, OUTPUT);
  pinMode (b, OUTPUT);
  pinMode (g, OUTPUT);
  pinMode (buttonRed, INPUT_PULLUP);
  pinMode (buttonYellow, INPUT_PULLUP);
  pinMode (buttonGreen, INPUT_PULLUP);
  pinMode (buttonBlue, INPUT_PULLUP);
  pinMode (buttonRed, INPUT);
  pinMode (buttonYellow, INPUT);
  pinMode (buttonGreen, INPUT);
  pinMode (buttonBlue, INPUT);
  pinMode (x, OUTPUT);
  game = 0;
  waiting = 0;
  loopCounter = 1;
  randNumber = 0;
  ledCounter = 0;
  inputCounter = 0;
  pass = 2;
  timeUp = 2;
  initiate();
}

void loop ()//by using functions, the void loop section here is very short.
{
  do {
    pickLed();
    waitForInput();
    scoreInput();
    levelCounter++;
    if (loopCounter > 4)//states the game should end after round 10
    { youWin();
      pass = 0;
    }
  }
  while (pass > 1); // keeps the game going until round 10 or fail
  initiate();
}

sorry I could't post the code properly.

why would you generate numbers at the begining of each round?

It generates a random number for the 1st round and then the next round it generates 2 numbers and so forth. That's how I have it now. The problem is the numbers are different for each round which is wrong. My professor wants the numbers to be same as the last round but with a new random LED generated

that describes what the program does, which you already decribed in your post ... i did not ask for a description .... think about what i asked ... hint: why do you generate only one number before the first round?

Idk how to generate 10 randomly. I've tried the for loop but I don't know and it just sucks but I gave up

you have the right idea ... what does this mean? ... I've tried the for loop but I don't know  .... there are several lines in your code that should give you some ideas ... here is one of them ... buttonArray[loopCounter] = 3;

The thing is that the game works but it doesn't repeat the same sequence each round. It gives a nee random set with each round

i was wrong, you do not understand ... another hint: it doesn't repeat the same sequence each round ... then do not generate the sequence at each round


text:
Troubles with motors for arduino
Why won't my motors work?
My setup is as followed:

arduino mega 2560 -> L296N motor controller -> Gikfun 3-6v dc motors.

To power the motors the setup is as followed:

arduino mega 2560 5v -> L296N motor controller.
9v battery -> LM2596 buck converter(7-8v) -> L296N motor controller.

I have 2 issues.

Issue 1: that no matter the code, the motors seem to only want to run at maximum speed. The code can be the simple example code or my code see bottom.
Issue 2: When given a code that changes the speed, it only works for one motor and the other motor will not run regardless if the first motor is hooked up or not, it will not work with the example code provided nor will it work in my code see bottom.

Note that this is not a simple wiring fix. Either the code is wrong, my hardware setup is wrong, or my hardware may not be functioning properly. Thank you for taking the time to review.
// ex code:
int a = 2;
int b = 3;
int c = 22;
int d = 23;
int e = 24;
int f = 25;

void setup(){
  pinMode(a, OUTPUT);
  pinMode(b, OUTPUT);
  pinMode(c, OUTPUT);
  pinMode(d, OUTPUT);
  pinMode(e, OUTPUT);
  pinMode(f, OUTPUT);

void loop() {
  digitalWrite(c, HIGH);
  digitalWrite(d, LOW);
  digitalWrite(e, HIGH);
  digitalWrite(f, LOW);
  analogWrite(a, 0);
  analogWrite(b, 0);

// ex code:
for (int i = 0; i > 150; i++){
  analogWrite(a, i);
  analogWrite(b, i);
  delay(1000);
}

for (int i = 150; i <= 0; --i){
  analogWrite(a, i);
  analogWrite(b, i);
  delay(1000);
}

/* my code. Note that the timed function is not finished.
* I also x'd out 4 motors for easy debugging purposes.
*/
// Motor 1
int a = 2;
int a1 = 22;
int a2 = 23;
// Motor 2
int b = 3;
int b1 = 24;
int b2 = 25;
// Motor 3
int c = 4;
int c1 = 26;
int c2 = 27;
// Motor 4
int d = 5;
int d1 = 28;
int d2 = 29;
// Motor 5
int e = 6;
int e1 = 30;
int e2 = 31;
// Motor 6
int f = 7;
int f1 = 32;
int f2 = 33;
// Motor halt
unsigned int i;
int x;
int x1;
int x2;
void setup(){
  Serial.begin(9600);
  pinMode(a, OUTPUT);
  pinMode(a1, OUTPUT);
  pinMode(a2, OUTPUT);
  pinMode(b, OUTPUT);
  pinMode(b1, OUTPUT);
  pinMode(b2, OUTPUT);
  pinMode(x, OUTPUT);
  pinMode(x1, OUTPUT);
  pinMode(x2, OUTPUT);
  pinMode(x, OUTPUT);
  pinMode(x1, OUTPUT);
  pinMode(x2, OUTPUT);
  pinMode(x, OUTPUT);
  pinMode(x1, OUTPUT);
  pinMode(x2, OUTPUT);
  pinMode(x, OUTPUT);
  pinMode(x1, OUTPUT);
  pinMode(x2, OUTPUT);
}
void motor1(){
  digitalWrite(a1, HIGH);
  digitalWrite(a2, LOW);
  analogWrite(a, 0);
  if (a = 1){
    Serial.println(" MotorA not working");
  }
  delay(1000);
  digitalWrite(a1, LOW);
  digitalWrite(a2, LOW);
  delay(250);
  digitalWrite(a1, LOW);
  digitalWrite(a2, HIGH);
  analogWrite(a, 0);
  if (a = 1){
    Serial.println(" MotorAr not working");
  }
  delay(1000);
  digitalWrite(a1, LOW);
  digitalWrite(a2, LOW);
  delay(250);
}
void motor2(){
  digitalWrite(b1, HIGH);
  digitalWrite(b2, LOW);
  analogWrite(b, 0);
  if (b = 1){
    Serial.println(" MotorB not working");
  }
  delay(1000);
  digitalWrite(b1, LOW);
  digitalWrite(b2, LOW);
  delay(250);
  digitalWrite(b1, LOW);
  digitalWrite(b2, HIGH);
  analogWrite(b, 0);
  if (b = 1){
    Serial.println(" MotorBr not working");
  }
  delay(1000);
  digitalWrite(b1, LOW);
  digitalWrite(b2, LOW);
  delay(250);

}
void motor3(){
  digitalWrite(x1, HIGH);
  digitalWrite(x2, LOW);
  analogWrite(x, 0);
  if (c = 1){
    Serial.println(" MotorC not working");
  }
  delay(1000);
  digitalWrite(x1, LOW);
  digitalWrite(x2, LOW);
  delay(250);
  digitalWrite(x1, LOW);
  digitalWrite(x2, HIGH);
  analogWrite(x, 0);
  if (c = 1){
    Serial.println(" MotorCr not working");
  }
  delay(1000);
  digitalWrite(x1, LOW);
  digitalWrite(x2, LOW);
  delay(250);

}
void motor4(){
  digitalWrite(x1, HIGH);
  digitalWrite(x2, LOW);
  analogWrite(x, 0);
  if (d = 1){
    Serial.println(" MotorD not working");
  }
  delay(1000);
  digitalWrite(x1, LOW);
  digitalWrite(x2, LOW);
  delay(250);
  digitalWrite(x1, LOW);
  digitalWrite(x2, HIGH);
  analogWrite(x, 0);
  if (d = 1){
    Serial.println(" MotorDr not working");
  }
  delay(1000);
  digitalWrite(x1, LOW);
  digitalWrite(x2, LOW);
  delay(250);

}
void motor5(){
  digitalWrite(x1, HIGH);
  digitalWrite(x2, LOW);
  analogWrite(x, 0);
  if (e = 1){
    Serial.println(" MotorE not working");
  }
  delay(1000);
  digitalWrite(x1, LOW);
  digitalWrite(x2, LOW);
  delay(250);
  digitalWrite(x1, LOW);
  digitalWrite(x2, HIGH);
  analogWrite(x, 0);
  if (e = 1){
    Serial.println(" MotorEr not working");
  }
  delay(1000);
  digitalWrite(x1, LOW);
  digitalWrite(x2, LOW);
  delay(250);

}
void motor6(){
  digitalWrite(x1, HIGH);
  digitalWrite(x2, LOW);
  analogWrite(x, 0);
  if (f = 1){
    Serial.println(" MotorF not working");
  }
  delay(1000);
  digitalWrite(x1, LOW);
  digitalWrite(x2, LOW);
  delay(250);
  digitalWrite(x1, LOW);
  digitalWrite(x2, HIGH);
  analogWrite(x, 0);
  if (f = 1){
    Serial.println(" MotorFr not working");
  }
  delay(1000);
  digitalWrite(x1, LOW);
  digitalWrite(x2, LOW);
  delay(250);

}
void timed(){
  analogWrite(a, i);
  digitalWrite(a1, HIGH);
  digitalWrite(a2, LOW);
  analogWrite(b, i);
  digitalWrite(b1, HIGH);
  digitalWrite(b2, LOW);
  delay(2000);
  digitalWrite(a1, LOW);
  digitalWrite(a2, LOW);
  digitalWrite(b1, LOW);
  digitalWrite(b2, LOW);
  analogWrite(b, i);
  digitalWrite(b1, LOW);
  digitalWrite(b2, HIGH);
  analogWrite(a, i);
  digitalWrite(a1, LOW);
  digitalWrite(a2, HIGH);
  delay(2000);
  digitalWrite(a1, LOW);
  digitalWrite(a2, LOW);
  digitalWrite(b1, LOW);
  digitalWrite(b2, LOW);
}
void loop(){
  motor1();
  motor2();
  motor3();
  motor4();
  motor5();
  motor6();
  if (i = 0){
    i++;
  }
  if (i >=100){
    --i;
  }
  timed();

}

Want to narrow this down to one specific question?

@Ghetti - Please edit your question and add the actual sketch(s) you are using. A picture, or preferably a "wiring diagram" (schematic) of the entire set-up would also help us to help you :)

@VE7JRO The code doesn't matter. However I have added the code I've been using. The setup is as simple as what is explained. No breadboards in the way, not complicated. Everything is directly connected. So wiring is not an issue. Please go through and explain what could be going wrong.

I cannot say much about the example code, that you mentioned. The posted example code is not complete, also the second part with the for loop is plain wrong. A for loop looks like this
for(<Variable initialization>; <Condition for running the loop>; <Action done for every loop iteration>)

So the condition in the middle is the condition for the loop running. If the condition evaluates to false, the loop will not run anymore. You have the line
for (int i = 0; i > 150; i++)

where i has the value zero at the start of the first iteration. zero is not bigger than 150, so the condition will evaluate to false. Thus the for loop will never run. The situation is similar with the second for loop of the example code. Where did you get that code? I think you should not trust this source very much, if it gives you this code.

Your own code also has some errors:

You are doing the if statement for equality wrong. If you want to check for equality, you need to use double equal signs. A single equal sign means an assertion (so you are actually changing the variable instead of check it's value), which will always evaluate to true. An example of a correct if statement is this:
if(i == 0)

You are doing things like
analogWrite(a, 0);

all over your code. but nearly always you are doing a
if(a == 1)

directly after it. This does not make sense. The first parameter of analogWrite() is the pin number, where you want to output the PWM voltage. A pin number is constant, why should it ever change, unless you are plugging the motor into other pins. So the above if statement will not check, if the motor runs or not. It will compare the pin number to 1, which is always false, since none of the pin numbers, that you used, is 1.
Again with the line
analogWrite(a, 0);

You are never providing a different second parameter as zero in your code (except for the timed() function, but I will get to this later). So the corresponding pin (here pin a aka 2) is turned off aka outputs LOW. With H-bridge controllers like the L296N, you use 2 digital pins to set the direction and then provide a PWM voltage (mostly at the enable pin) to change the speed. But as described, you never output a PWM voltage, so the motors cannot run, if you connected them corrected.
Connected the the previous sentence. Your code will not run any motors. But since you wrote, that they actually do run, this means, that the hardware is also a problem. Please revisit tutorials on how to use the L296N with an Arduino or provide information about your wiring by editing your question.
In your loop() function you have this passage:
if (i == 0){
     i++;
}
if (i >=100){
    --i;
}

(I already corrected the double equal signs as described above). If iis exactly zero, i will be incremented. If i is greater or equal to 100, it will decrement. It only changes in this code portion and it will be initialized implicitly with a value of zero at the startup of the program. So the first if statement will trigger exactly once. Then i is not equal to zero, so it will not increment or even ever change again. Also i is not greater or equal to 100 (it is 1), so it will never decrement. Your code sets i to 1 once, and then never changes it again. That cannot be, what you tried to do. If you want a sweep from 0 to 100 and then from 100 to 0, you need an extra variable for the direction. Something like this:
boolean direction=false; // define this at global scope
...
// the following code would replace your `i` related code in your loop function
if(direction){
    i++;
} else {
    i--;
}
if(i==0) direction = true;
if(i>=100) direction = false;

Connected to the previous issue: Since i stays 1 forever, you also only write a PWM duty cycle of 1 out of 255 to the motors a and b inside the timed() function. A value this small is most likely not enough to even move the rotor of the motors (since motors also experience friction). Again, these motors should not run with your current code. If you change the i related code like I suggested above, the code should run the motors with changing speed. But also note, that this would still be a very slow change. A full run of the timed() function takes 4s (since the two 2s delays). With 100 steps, a full sweep from 0 to 100 will take 400s (aka 6.7 minutes) and the same time back to 0. (And this isn't accounting for the huge delays from the previous motor functions. With this you won't be able to see the change, until you wait really long time for it)
You wrote, that you are using a 9V block battery as power source. If it is a 9V block battery, then you should know, that these batteries are not made for providing much current. They are made for low power applications. For driving motors for a longer time over battery or driving more motors at the same time, you should consider using a different battery (for example multiple AA batteries in series). As your motors currently run from it, you seem to get away with it, which is just fine. I just wanted to provide the information, in the case you reach this limit at some point.

All in all I can only agree with VE7JRO, that you should start with code for 1 single motor. Then, if it works like it should, you can move on and add the next motor. This will ensure, that you are getting things right early in the process and that you don't generate a big mountain of problems to cope with at once.


text:
Adafruit ImageReader Library 2.3.5 added several library dependencies
Why has Adafruit ImageReader Library v2.3.5 recently added six dependencies on other libraries for hardware I'm not using?
I used the v2.3.2 of the Adafruit ImageReader Library successfully two months ago. An example sketch worked fine with my ILI9341 3.2" display and its SD card.
Now there is an updated v2.3.5 available but the library manager wants to add six more "dependency" libraries:
- Adafruit ST7735 and ST7789 Library
- Adafruit seeswaw Library
- Adafruit HX8357 Library
- Adafruit SSD1351 library
- Adafruit SSD1331 OLED Driver Library for Arduino
- Adafruit EPD
As far as I know, the ILI9341 uses none of the hardware that corresponds to the six libraries it's trying to drag in. I don't want unneeded dependencies and don't want the code bloat.
If I don't accept the additional libraries, my sketch fails to compile. The error message is "C:\Users\barry\Documents\Arduino\libraries\Adafruit_ImageReader_Library\Adafruit_ImageReader_EPD.h:18:10: fatal error: Adafruit_EPD.h: No such file or directory"
However, EPD code is for "e-paper displays" which does not apply to my ILI9341 which is a TFT display. I don't think EPD code should be a real dependency.
Kindly redirect me if there's a better place to ask.

If you don't use the functions of the ImageReader library, which use the additional libraries, your compiled code will not contain code from the additional libraries.
The Adafruit ImageReader library may have functions which use other libraries and the libraries are required for compilation. But at linking the compiled units which are not used by your code and even parts of the compiled units which are not used by your code are not put in the resulting hex or bin file.

I sincerely hope that the linker is smart enough to leave out compiled units that are not used. However, linkers can only operate upon external references between components and is not able to detect run-time usage.

So in my case, if the ImageReader library has a "new EPD()" statement anywhere (I don't know if it does or not), such that the instantiation occurs at run-time, the linker must include the EPD library code, regardless of if it's ever actually called or not, right?
Bottom line, I think I need to get a clean compile, find the .elf file, and generate a memory map to see what components are included. Thanks!

yes there are things where the compiler can't evaluate the dependencies (like virtual methods of classes), but the new EPD() is not one of them. here it is clear at compile time that the class EPD will be used.

I hope to get an answer to the original question "Why has Adafruit ImageReader Library v2.3.5 recently added six dependencies on other libraries for hardware I'm not using?"
On GitHub, the library description only says "Requires Adafruit_GFX library and one of the SPI color graphic display libraries, e.g. Adafruit_ILI9341." However the current version depends on six additional libraries for other hardware.
link

@Barry, the reason is CI tests of examples https://groups.google.com/a/arduino.cc/forum/#!topic/developers/kMS80BNOLW8

Juraj, with your link I've learned about "Travis CI", an automated continuous integration system and followed it for tips about build-checking my Arduino projects for various processors. Good stuff. This will be useful for my Feather M4 (SAMD51) projects.
I'll pursue the issue of code bloat separately. I need to write sample programs and examine the resulting memory map. Then I can tell how many unused libraries for unused hardware finally end up in the compiled object code. Anyway, that's a separate issue and this question is well answered.

@Barry, if the additional libraries are used only in examples then there is no way to your code for them.


text:
Unable to find arduino on device manager
I have just got a new Arduino robot for Christmas and I have plugged my robot into my computer and I have read the instructions from the manual that the robot was provided with and told me to go to the device manager in the control panel and look under unknown devices, unfortunately I was unable to find it. However I looked under ports and I found that there were 3 ports, I eventually found out that if I took the Arduino plug in and out of my PC that port COM5 was the Arduino, the problem I am having now is to know how to get this port to recognise my Arduino

one error that always comes up when trying to upload the code onto COM5 is this:
avrdude: stk500_getsync() attempt 9 of 10: not in sync: resp=0xa1
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 10 of 10: not in sync: resp=0xa1

This is what the arduino looks like

Link for where I brought the arduino kit from:
https://www.amazon.co.uk/ELEGOO-Tracking-Ultrasonic-Intelligent-Educational/dp/B01M6YHRJ4/ref=sr_1_1_sspa?keywords=elegoo+robot&qid=1577982862&sr=8-1-spons&psc=1&spLa=ZW5jcnlwdGVkUXVhbGlmaWVyPUEyVjVXTk9VMDlUTTg1JmVuY3J5cHRlZElkPUEwMjc4NjYwOEVGNEoxM1Y0QzIwJmVuY3J5cHRlZEFkSWQ9QTA4OTA1NzAzTTNBVFJMV09aVjJRJndpZGdldE5hbWU9c3BfYXRmJmFjdGlvbj1jbGlja1JlZGlyZWN0JmRvTm90TG9nQ2xpY2s9dHJ1ZQ==

This is what shows up on the arduino IDE when I select COM5 (which should be my arduino)

@jsotola how come when I try to load it onto my arduino it comes up with the error: (shown above)

what device do you have? ... add a link to your post .... upvote for adding info about avrdude to the post instead of putting in comments

Try a few random things. Unplug your arduino and reboot your computer. Plug it back in before starting the Arduino interface, look in the menu to be sure the right Arduino device is selected. Look for the serial port and be sure the right one is selected - it might have changed.

It is likely that this is a driver issue. I have encountered similar errors, and by updating/installing the drivers, it fixes itself

It seems your computer recognize the serial port of the Arduino device, but doesn't know how to 'talk to it'.
In most USB to TTL devices (that are included in Arduino and ESP8266 boards) you need to install their driver (like CH341 driver) BEFORE you plug in your device.
Please try to find/identify the 'USB to Serial' chip you have on the board, and install its drivers.
Good Luck!

I am only a teenager and do not have much knowledge on arduino itself. Can you possibly guide me on how I can do this please.

You're totally welcome (and after I upvoted your question, you have upvote privileges too...). Let's start by finding the exact model number of the Arduino you have. Can you put a link to where it was bought? or a picture of the board itself?

I can firstly start off by saying that it is an Arduino Uno. I got it as a christmas present, so I am not 100% sure about it fully.(A picture of the arduino is shown above). The links for where I brought it from is also shown above as well

sorry if my webcam is not very clear

Good you've updated more details about the board. The picture is good. This is not a solution, but just found this: https://forum.arduino.cc/index.php?topic=521632.0 about the board you're using. Also - This search brings many answers and tutorials that make sense to me: https://www.google.com/search?q=elegoo+uno+r3+usb+driver . Would you try to look into this available information and update us what makes sense to you? GoodLuck!

Oh yeah, there was one more thing I forgot to mention was that the arduino IDE does see COM5 but doesn't see it as being an arduino though. (shown above)

Totally understood. It was clear from your first screen capture, and my answer stays the same. Please make some more investigation using the resources I've mentioned, and let's know what have you discovered.


text:
Arduino serial coming up with weird characters
I am trying to create a basic snake game using the arduino UNO along with the MAX72XX module, the LCD module and the analog joystick module. I am wanting to program my program using object orientated programming. I have no experience with C++ so I am struggling quite a bit to understand why the serial comes up with unusual characters whenever I am trying to print off variable values. The serial is able to print text, but whenever I try to print variable values the unusual characters come up. I had tried changing the baud to a higher one and that makes no difference. The two variables that I am trying to print off both have an integer data type. I have tried using Serial.write, Serial.print and Serial.println, none of these seem to work. Here is the code if it helps solve the problem:
#include "LedControl.h"
#include <LiquidCrystal.h>
LedControl lc=LedControl(12,10,11,1);
LiquidCrystal lcd(8, 13, 3, 5, 6, 7);

class Snake_body
{

  public:

    int following_direction;
    int x;
    int y;
    int previous_x;
    int previous_y;
    bool enabled;
    Snake_body()
    {
      this->enabled = false;
    }

    void configure(int x_value, int y_value)
    {
      this->enabled = true;
      this->following_direction = 1;
      this->x = x_value;
      this->y = y_value;
      this->previous_y = y_value;
      this->previous_x = x_value;
    }

};

class Power_up
{
  private:

  public:
    virtual void init()
    {
      this->x = 0;
      this->y = 0;
    }

    int x;
    int y;
};

Snake_body *snake[64];//this is the snake array that contains the snake objects

int snakes_length;

Power_up *power_up;//define the power up object

int score;

void setup() {

  Serial.begin(9600);

  lcd.begin(16, 2);

  snakes_length = 1;

  for(int z = 1; z < 64; z++)
  {
    snake[z] = new Snake_body();//create 63 un-enabled snake objects
  }

  snake[0] = new Snake_body();//the 1st snake object is defined
  snake[0]->configure(0,0);//here it is initialised
  power_up = new Power_up();//create the power_up object

  lc.shutdown(0,false);
  lc.setIntensity(0,8);
  lc.clearDisplay(0);
  lcd.setCursor(0, 0);
  score = 0;
  lcd.print("score: ");
  lcd.setCursor(6, 0);
  lcd.print(score);
  pinMode(2, INPUT);
  digitalWrite(2, HIGH);

  power_up->x = random(0, 8);
  power_up->y = random(0, 8);
}

int snake_direction;

void loop()
{
  int x_axis = analogRead(A0);
  int y_axis = analogRead(A1);
  int switcher = digitalRead(2);

   if(x_axis <= 400) //&& x < 7)//each of the if statements are for the direction of the snake head (snake[0])
   {
     snake[0]->following_direction = 0;
     //x += 1;
     //Serial->println("right");
   }

   else if(x_axis >= 600) //&& x > 0)
   {
     snake[0]->following_direction = 1;
     //x -= 1;
     //Serial->println("left");
   }

   else if(y_axis <= 400) //&& y > 0)
   {
     snake[0]->following_direction = 2;
     //y -= 1;
     //Serial->println("up");
   }

   else if(y_axis >= 600) //&& y < 7)
   {
     snake[0]->following_direction = 3;
     //y += 1;
     //Serial->println("down");
   }

  if(snake[0]->following_direction == 0 && snake[0]->x < 7)//used for snake movement
  {
    snake[0]->previous_x = snake[0]->x;
    snake[0]->x += 1;
  }

  else if(snake[0]->following_direction == 1 && snake[0]->x > 0)//used for snake movement
  {
    snake[0]->previous_x = snake[0]->x;
    snake[0]->x -= 1;
  }

  else if(snake[0]->following_direction == 2 && snake[0]->y > 0)//used for snake movement
  {
    snake[0]->previous_y = snake[0]->y;
    snake[0]->y -= 1;
  }

  else if(snake[0]->following_direction == 3 && snake[0]->y < 7)//used for snake movement
  {
    snake[0]->previous_y = snake[0]->y;
    snake[0]->y += 1;
  }

  if(snake[0]->x == power_up->x && snake[0]->y == power_up->y)//used for collision with power up
  {
    score += 1;
    lcd.setCursor(6,0);
    lcd.print(score);

    power_up->x = random(0, 8);
    power_up->y = random(0, 8);

    for(int j = 0; j < 64; j++)
    {
      if(snake[j]->enabled == false)//enables the next un-enabled snake object in the array
      {
         //Serial.println(true);
         Snake_body *snake[j];

         //next_snake_body = new Snake_body();
         //Snake_body next_snake_body;
         snake[j] = new Snake_body();
         snake[j]->configure(snake[j-1]->previous_x, snake[j-1]->previous_y);
         //Serial.print("x: " + snake[j-1]->previous_x);
         //Serial.print("y: " + snake[j-1]->previous_y);
         snake[j]->following_direction = NULL;
         break;
      }
    }
  }

  for(int i = 1; i < 64; i++)
  {
    if(snake[i]->enabled == true)//updates each snake object in the array
    {

      snake[i]->x = snake[i-1]->previous_x;
      snake[i]->y = snake[i-1]->previous_y;
    }
  }

  lc.setLed(0,power_up->x,power_up->y,true);//draws the power up
  for(int w = 0; w < 64; w++)
  {
    if(snake[w]->enabled == true)//draws each snake object that is enabled
    {
      Serial.print("x: " + snake[w]->x);
      Serial.print("y: " + snake[w]->y);
      lc.setLed(0,snake[w]->x,snake[w]->y,true);
    }
  }

  /*Serial->print("x: ");
  Serial->print(snake->x);
  Serial->println();

  Serial->print("y: ");
  Serial->print(snake->y);
  Serial->println();*/

  delay(100);

  lc.clearDisplay(0);
}

the weird characters that show up are characters like this:

with the follow picture y is just simply text to show which axis it is, the weird characters are the integer y axis value of the snake object that is being looked at.

Make a copy of your sketch and cut out everything but printing one text message and one variable value. Do you still have a problem? Add back a little bit at a time until the problem reappears. What you just added back is the cause.

everything can be important information when debugging code, including the weird characters ... please add the weird character printout to your post

There are some weird statements in the code, like the line Snake_body *snake[j];.. this does nothing and looks like a variable declaration, I'd remove that. Also in the beginning you snake[z] = new Snake_body(); but don't initialize the values (like y, x)  in them via a constructor or initialize() function -- those will be random then and possibly garbage. You calso call Serial->print(snake->x); but snake is an 64-element array of Snake pointers, so that won't work. You need an index here.

Oh, and you also have a memory leak when you snake[j] = new Snake_body();. There already is a Snake_body element in there and you've overwritten the pointer to it with a newly allocated object without delete-ing the previous on -- reevaluate whether you really need to allocate a new object here of want to modify the existing one. If the latter, cleanup the memory.

You are using quite a bit of dynamic memory allocation. Note that this is a bad idea on an Arduino in 99% of cases. You can easily fill up all RAM without knowing beforehand and even when you correctly delete objects, that aren't needed anymore, you are getting your heap fragmented

@MaximilianGerhardt Serial->print(snake->x); was something I comment out and don't need anymore in my code. I forgot to take it out. Additionally, each object that I have created is needed because these are the body parts of the snake. Also, I am not very experienced with C++, so it took along time to get this code to where it is now.

@chrisl All the snake objects are needed, each time a new one is defined this is another part of the snake

Sure every part is needed, but you're initially filling all 64 elements of the array with new Snake_body objects and then overwrite the pointer to that object with snake[j] = new Snake_body(); i.e. another Snake_body object, that's a memory leak (https://www.geeksforgeeks.org/memory-leak-in-c-and-how-to-avoid-it/). You should be able to do this with all static allocation and no new / dynamic memory, just a static array of 64 Snake_body objects.

@MaximilianGerhardt Ok, I now see where you are going with this. What the for loop in setup does is that it just puts a Snake_body object in each space in the array and then, once the power up is touched the for loop will go through the snake array until an object which has not been enabled is found and then that object will then be configured and initialised.

@MaximilianGerhardt look at the code (I have commented what each part of the code does)

@jsotola the characters always change each time but I have posted an example above in the question of what similar characters come up.

@MaximilianGerhardt the problem is that I needed to do this because I needed something comparable so that I could find an empty space in the array. Do you know how I could possibly find an empty space in the array without having to have a RAM memory leak

If you want to mark an empty space, why not make this a member in your field element structure? Then you can have an always-there 64 field array with just different attributes in them. Maybe it's better to think of the play field as generic tiles which have attributes like whether a snake part is on them or a powerup, and then save extra info like the head and teal of the snake to move it / expand it.

@MaximilianGerhardt I am not 100% clear of what you said.

@MaximilianGerhardt Would it potentially work if each object at the beginning is null except snake[0]. Then to find an empty object you would go through the array until you find an element in the array with a NULL value then this element with a null value becomes an object. Could that potentially work


text:
Arduino PS2X module not working with controller
I have been trying to use the PS2X module to try to use my PS1 remote with my arduino (I am guessing that PS1 controllers will work because they are virtually the same ). The problem is that I don't want to download the module from the original website because it is not secure (it had HTTP instead of HTTPS) and the download file could contain viruses. Instead I got it off the arduino website. I have wired my controller correctly and have triple checked that all the wires are in the right positions and they seem to be. I then used the arduino example program given by the safely downloaded file. I complied it onto my arduino and I got the following message in the serial "No controller found, check wiring, see readme.txt to enable debug. visit www.billporter.info for troubleshooting tips
Unknown Controller type." I then looked into the text file to debug it and it told me to uncomment 2 of the lines which is what I did. I then ran the example program file again like how the readme file told me to, and I still get the same errors.
link github:https://github.com/madsci1016/Arduino-PS2X
link to the main: http://www.billporter.info/2011/03/27/arduino-playstation-2-controller-library-troubleshooting-guide/
wiring:

how I have wired it:

@jsotola it LITERALLY means "the same"

Which example in which library? Please provide a link, if possible for the github site of the library. Also show your wiring

@chrisl shown above

You still didn't include the link. Do you mean this library?

yes I do mean that one sorry