dont understand some lines/codes

hey guys i got this arduino code but i dont fully understand some lines can someone please explain it to me?
#include<LiquidCrystal.h>
LiquidCrystal lcd(12,11,6,5,4,3);
int pwm=9;
int pot=A0;
float value=0;
int percent;
float rev=0;
int rpm;
int oldtime=0;
int time;

void isr() //interrupt service routine
{
rev++;
}

void setup()
{
lcd.begin(16,1); //initialize LCD
attachInterrupt(0,isr,RISING); //attaching the interrupt
}

void loop()
{
delay(1000);
detachInterrupt(0); //detaches the interrupt
time=millis()-oldtime; //finds the time
rpm=(rev/time)*60000; //calculates rpm
oldtime=millis(); //saves the current time
rev=0;
value=analogRead(pot); //reads the speed control POT
value=value/4;
analogWrite(pwm,value); //sets the desired speed
percent=(value/255)*100; //finds the duty cycle %
lcd.clear();
lcd.setCursor(0,0);
lcd.setCursor(0,1);
lcd.print(rpm);
lcd.print(" RPM");
lcd.print(" ");
lcd.print(percent);
lcd.print("%");
attachInterrupt(0,isr,RISING);

detachInterrupt(0); //detaches the interrupt i know what a interrupt is but this code doesnt include an interrupt or does it?
time=millis()-oldtime; //finds the time
rev=0;
value=analogRead(pot); //reads the speed control POT? the pot meter in my circuit does only make the lcd display less or brighter
value=value/4; random value ?
analogWrite(pwm,value); //sets the desired speed. wich component do i set the speed with?
percent=(value/255)100; why value/255100?

Some of these things you could figure out for yourself.
I'm going for the low hanging fruit:

percent=(value/255)*100;

if value = 255 then 255/255 = 1; then, 1 * 100 = 100 %
if value = 127 then 127/255 = 0.498; then, 0.498 * 100 = 49 %

Looks like the idee is to allow interrupts for repeated whole seconds...?? Dont know why, but can unterstand this thinking.
:slight_smile:
The calculation of time period will be close to a sec every time..
Value div 4 ensures that analog 0.. 1023 => 0..255, to match 8 bit pwm for led..
Calc of percentage id performed with integers only, not very suited here.. force cast to float

Besides the setup(), The code basically loops with a 1 second wait (the delay(1000); AT the begining of the loop) during which the ISR detects rotations (probably a magnet passing in front of a detector or something similar sending a rising front) and increments the rev variable. Then you remove the interrupt to stop the speed sampling, and as you know you sampled for 1 sec, the code calculates the rpm (1 minute = 60 sec = 60000 ms) then the code checks a potentiometer on A0 which gives a value between 0 and 1023, divides this value by 4 (so between get something between 0 and 255) which is used to set the PWM driving the motor speed and display info on the LCD. So basically the code uses a potentiometer to vary the speed of a motor driven most likely through an H bridge and display infos about the speed.

--> PWM : on an Arduino, the analogWrite function allows you to generate a PWM wave on some pin (If you have tried out the LED fade example, then you already know how to use it, if it read about it). This function takes a value between 0 and 255 (hemce the divide by 4) and doesn’t work on all pins in Arduino. On an Arduino Uno, it does so on pins 3, 5, 6, 9, 10 and 11 (you have a ~ usually next to the PIN number).

When you want to control a DC motor speed and direction it's common to use the help of a dual H-Bridge L293D motor driver for arduino --> The speed of the motor depends on a PWM value that was passed to the analogWrite function. Remember the value can be between 0 and 255. If you pass 0, then the motor will stop and if you pass 255 then it will run at full speed. If you pass a value between 1 and 254, then the speed of the motor will vary accordingly => hence the idea of percentage of full speed

--> to get a %age of something that can vary between 0 and 255, you divide by 255 so you get something between 0 and 1 and then multiply by 100 to get a value between 0 and 100 which is the percentage of the full speed

The math in the code is not great as it uses (non volatile) integer... some things to fix there probably

Does this help?

Because rev is not declared volatile, there is a good chance the code wont work.

(Making rev a float that gets incremented in the ISR is a bad move IMHO).

detachInterrupt(0);           //detaches the interrupt i know what a interrupt is but this code doesnt include an interrupt

What do you suppose

attachInterrupt(0,isr,RISING);

and the isr() function do ?

Whandall:
Because rev is not declared volatile, there is a good chance the code wont work.

(Making rev a float that gets incremented in the ISR is a bad move IMHO).

Well i made the circuit and uploaded it on a arduino and it works