servo not spinning

hi, please help T^T this time i was assigned to do some parts:

  1. dc motor temperature controlled (i succeed)
  2. potentiometer motor dc speed control (i succeed)
  3. servo push button controlled, when push once it keeps rotating, when push once again it stops. (i failed)

I am using tinkercad to do this and i dont know why when I start the simulation the servo rotates 0-180 and 180-0 first then return back to normal, when button pushed it didnt do anything at all.

this is my code:

#include <Servo.h>

Servo myservo;

int in1 = 10;
int in2 = 8;
int pwmpin = 11;
int servoPin = 9;
int pin = 0;
const int button = 3;
int potPin = 0;
int velocity = 0;
int oldpos = 0;
char degree = 176;
int var;
int count =0;
int pos = 0;
int buttonState = 0;
float tmp = 0;
float tmpCel = 0;

void setup()
{
  pinMode(in1,OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(pwmpin, OUTPUT);
  pinMode(button, INPUT);
  digitalWrite(pwmpin, HIGH);
  digitalWrite(in1, HIGH);
  digitalWrite(in2, LOW);
  myservo.attach(9);
  Serial.begin(9600);

}

void loop()
{

  double val;
  val = analogRead(A1);
  Serial.println(val);
  float tmp = analogRead(A1);
  float voltage = tmp * 5.0;
  voltage  /= 1024;
  tmpCel =  (voltage  - 0.5) * 100 ;
  if (tmpCel>30){
    digitalWrite(in1, HIGH);
    digitalWrite(in2, LOW);
    Serial.print("Temperature: ");
    Serial.print(tmpCel);
    Serial.println(degree);
    delay(2000);
  }else{
    digitalWrite(in1, LOW);
    digitalWrite(in2, LOW);
    Serial.print("Temperature: ");
    Serial.print(tmpCel);
    Serial.println(degree);
    delay(2000);
  }
  delay(200);
  

 buttonState = digitalRead(button);
  if (buttonState == LOW) {     
	for(pos = 0; pos <= 180; pos++){
	int var=analogRead(A0);
	var= map(var,0,1023,5,50);
	myservo.write(pos);
	}
	for(pos = 180; pos >= 0; pos --){
	int var=analogRead(A0);
	var= map(var,0,1023,5,50);
	myservo.write(pos);
	}
	}
  else{
    myservo.write(0);
  }

 if (pos != oldpos){
    velocity = map(pos, 0, 1023, 0, 255);
    analogWrite(pwmpin, velocity);
    oldpos = pos;
  }
  else{
  	digitalWrite(in1, LOW);  
    digitalWrite(in2, HIGH);
  }
  delay(200);

int speed = analogRead(potPin) / 4;
boolean reverse = digitalRead(button);
setMotor(speed, reverse);
}

void setMotor(int speed, boolean reverse)
{
analogWrite(pwmpin, speed);
digitalWrite(in1, ! reverse);
digitalWrite(in2, reverse);
}

analogRead does not return a double or a float

I am using tinkercad to do this and i dont know why when I start the simulation

Get back to us when you've got hardware to test it on.

myservo.write(0);Did you meanmyservo.write(var);

Why "var"? Why not "servoAngle"?

Pressing you button only changes the value of variable 'var'...

buttonState = digitalRead(button);
  if (buttonState == LOW) {     
     var =90;
  }
  else
  {
    var = 0;
  }

...as you don't use the variable 'var' anywhere else in your sketch, the above code is totally pointless.

You might want to look at:
File -> Examples -> 02.Digital -> Debounce
For an example which performs a toggle function. Then use the toggle state to control the motor speed.

pcbbc:
Pressing you button only changes the value of variable 'var'...

buttonState = digitalRead(button);

if (buttonState == LOW) {   
    var =90;
  }
  else
  {
    var = 0;
  }



...as you don't use the variable 'var' anywhere else in your sketch, the above code is totally pointless.

You might want to look at:
File -> Examples -> 02.Digital -> Debounce
For an example which performs a toggle function. Then use the toggle state to control the motor speed.

pcbbc:
Pressing you button only changes the value of variable 'var'...

buttonState = digitalRead(button);

if (buttonState == LOW) {   
    var =90;
  }
  else
  {
    var = 0;
  }



...as you don't use the variable 'var' anywhere else in your sketch, the above code is totally pointless.

You might want to look at:
File -> Examples -> 02.Digital -> Debounce
For an example which performs a toggle function. Then use the toggle state to control the motor speed.

hi, ive changed the code above, with for function, but it still didnt do anything, can u please help me check it out :frowning:

jesslynsukamto:
hi, ive changed the code above, with for function, but it still didn't do anything, can u please help me check it out :frowning:

What is a "for function"?
Do you mean a "for loop"?

Post your new code. We can't help if we can't see your new code.

Did you check the example and understand it?

pcbbc:
What is a "for function"?
Do you mean a "for loop"?

Post your new code. We can't help if we can't see your new code.

Did you check the example and understand it?

i mean, i alrd post above my new code, i am sorry for making misunderstandings hehe

jesslynsukamto:
i mean, i alrd post above my new code, i am sorry for making misunderstandings hehe

You mean you've edited the original post, and now none of our posts make any sense?

Don't do that, that's stupid.

Again, now the thing with "var" in the for loop still makes no sense

Hi,
How have you got your button wired?
If it is between gnd and the input have you got a pullup resistor, if you haven't you can turn the internal controller pullup on instead.

If it is between 5V and the input then you will need to put a 10K resistor between the input pin and gnd.

Tom.. :slight_smile: