0% found this document useful (0 votes)
15 views

Pwmpin Motorspeed : Setup

The code defines variables to control a motor's speed and direction using PWM. It increments the motor speed from 0 to 255 over 30ms, then decrements it back to 0. A second part uses analog input to vary the motor's enabled speed and switches direction when a button is pressed.

Uploaded by

VISHNU VISHNU935
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Pwmpin Motorspeed : Setup

The code defines variables to control a motor's speed and direction using PWM. It increments the motor speed from 0 to 255 over 30ms, then decrements it back to 0. A second part uses analog input to vary the motor's enabled speed and switches direction when a button is pressed.

Uploaded by

VISHNU VISHNU935
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

int

PWMPin
= 10;
int motorSpeed = 0

void setup()
{

void loop()
{

for (motorSpeed = 0 ; motorSpeed <= 255; motorSpeed += 10)


{
analogWrite(PWMPin, motorSpeed);
delay(30);
}

for (motorSpeed = 255 ; motorSpeed >= 0; motorSpeed -= 10)


{
analogWrite(PWMPin, motorSpeed);
delay(30);
}
}

int
mot1
= 8;
int mot2 = 9;
int en1 = 10;
int dir = 6;
bool state = true;
int nob = A0;
int val=0;

void setup()
{
pinMode(mot1,OUTPUT);
pinMode(mot2,OUTPUT);
pinMode(en1,OUTPUT);
pinMode(dir,INPUT_PULLUP);

void loop()
{

val = analogRead(nob);

analogWrite(en1, val / 4);

if(digitalRead(dir)==LOW)
{
state=!state;
while(dir==LOW);
delay(300);
}
if(state)
{
digitalWrite(mot1,HIGH);
digitalWrite(mot2,LOW);
}
else
{
digitalWrite(mot1,LOW);
digitalWrite(mot2,HIGH);
}
}

You might also like