No errors/bugs in code and upload successful but nothing happens

Is there a reason as to why nothing happens when you upload code that has no errors brought up? Would it be due to circuitry reasons or is it purely just something more abstract.

Do you get an error from Avrdude or does your code simply do nothing visible once it's uploaded?

Please post your sketch here, using code tags when you do. What evidence do you have that it is doing nothing ?

Which Arduino are you using and what is connected to it ?

Hi, I did get an error from Avrdude from a part of my code(I will paste it below), however I removed it, as it can still function without it, and ran the code and the board does not do anything

int pot = A0;
int trig = 9;
int echo = 10;
int motor = 11;
int speedlow;
int speedhigh;
float ih;
float ch;
float rh;
float duration;
int newpot;
int potin;
//potin mapped value of pot

void setup() {
  pinMode(trig, OUTPUT);
  pinMode(echo, INPUT);
  pinMode(motor, OUTPUT);
  pinMode(pot, INPUT);
  digitalWrite(trig, LOW);
  digitalWrite(trig, HIGH);
  digitalWrite(trig, LOW);
  duration = pulseIn(echo, HIGH);
  ih = (duration * 0.0343)/2;
  rh = ih + 5;
  potin = analogRead(pot)/4;
  digitalWrite(motor, potin);

}

void loop() {

  pinMode(trig, OUTPUT);
  pinMode(echo, INPUT);
  pinMode(motor, OUTPUT);
  pinMode(pot, INPUT);
  
  digitalWrite(trig, LOW);
  digitalWrite(trig, HIGH);
  digitalWrite(trig, LOW);
  duration = pulseIn(echo, HIGH);
  ch = (duration * 0.0343)/2;
  
  if (ch < rh) {
    for (int speedhigh = 0; speedhigh < 256; speedhigh++) {
      digitalWrite(motor, speedhigh);
    }
  }
  
  if (ch > rh) {
    for (int speedlow = 255; speedlow >= 0; speedlow--) {
      digitalWrite(motor, speedlow);
    }
    }
  }

  newpot = analogRead(pot);
  if (newpot != potin) {
    //potin = newpot;
    digitalWrite(motor, newpot);
    
    if (ch < rh) {
    for (int speedhigh = 0; speedhigh < 256; speedhigh++) {
      digitalWrite(motor, speedhigh);
    }
  }
  
  if (ch > rh) {
    for (int speedlow = 255; speedlow >= 0; speedlow--) {
      digitalWrite(motor, speedlow);
    }
    }
  }
  
}

The evidence is that, once connected and ran, the board does not do anything. The sensors do not work or return any value, the motor does not run. I do not know if it is my circuit that is the problem or if it is some issue with lack of libraries. Also, if there is any improvement needed in my code do tell, as im quite new to this

I have included the newpot part of the code which is the part which has the avrdude error, saying it doesnt name a type

Serial.print() would be your friend here.

It's the compiler complaining. Count your braces and look at where your loop function ends.

yep I had figured so, I am currently adding it in

I see okay, and if I cut down or increase the amount of braces and rerun the code again in the void loop section, I assume that is when everything will be compiled properly and then the board will do as it is supposed to do?

If the compiler doesn't show up any errors, that does not mean, that your code contains no errors. The compiler only complains about syntax errors. It does NOT check the logic of your code. It's about you to do that.
Usually the logical errors are harder to find than the syntactical ones :wink:

What do you think that this for loop will do:

    for (int speedhigh = 0; speedhigh < 256; speedhigh++) {
      digitalWrite(motor, speedhigh);

The same with the following one.

  newpot = analogRead(pot);
  if (newpot != potin) {
    //potin = newpot;
    digitalWrite(motor, newpot);

Maybe you want to use analogWrite() in stead of digitalWrite()? But if you want to accelerate and decelerate the motor you need some sort of delay between the speed steps.

Ah okay thank you! for the for loop, I had though that the motor speed will increase, kind of like increasing the brightness of an LED light over time, so I had used the same logic here but for a motor, so its speed increases over time.

as for newpot, I wanted newpot to be equal to whatever value is returned from A0. However if the newpot value does not equal the previous value returned from A0(the one used for writing to the motor before in the code), then I would want to write the newpot value to the motor, instead of the motor running on the value from the previous A0 value. I hope this makes sense.

As for using AnalogWrite, would it make more sense to keep the digitalwrite command but mapping the newpot value first? or just replace digitalwrite with analogwrite, I presume it doesnt make much difference. I just noticed I was reading an analog value and writing a digital one without mapping said value.

And okay, would including something such as delay(100) work for the delay between accelerating and decelerating. And could I ask why I would need that delay too? Is it to make sure the components aren't overworking?

It makes very much difference. With digitalWrite you can only switch on/off your motor. You cannot control the speed. This is only possible with analogWrite, wich creates a PWM signal. But not every pin is capable of creating a PWM signal - it depends on your processor/board.
And be aware, that analogRead returns a value between 0...1023, but analogWrite accepts only 0...255.

You need a delay between every speed step.

Why set pinmode over and over again in loop when pinmode only needs be done once in setup()?

Post the latest version of your code, after this post, please.

Oh I see, I had not known that prior, thank you. So I should only really be using analogWrite for whenever I am writing a speed to the motor for it to follow. And I'm guessing that I will still have to map analogRead values to ones that analogWrite can accept. Okay thank you.

As for the newpot section, it comes up with a message saying that it does not name a type. If I change the digitalWrite to analogWrite and map the analogRead values, would that fix the error. I will also be changing any previous digitalWrite to analogWrite too.

And does the logic for the motor speed increase check out correctly, codewise and logic wise? With the inclusion of the delay.

Good question, I've noticed its unnecessary so I will be removing it.

That is the compiler's cryptic way of telling you that that line of code (and actually all that follow it) are not inside a function, which it really doesn't like.

Ohh I see, thanks, and I assume this is where I have to check the code braces in order for me to fix this error

Your topic has been moved to a more suitable location on the forum. If you can upload, it has nothing to do with the section where you did post it.

Okay thank you