checking an arduino code for a project (not working)

hi everyone, I am working with project that user enters predetermined speed that motor will play with, detect the distance if the distance less than 10 cm it will speed down automatically, else it will work with the predetermined speed.

system was working very well before some days… but now unfortunately not working I don’t know where is the mistake…

needs your help

    /* LCD Connection :
    VSS to Arduino GND VCC to Arduino 5V VEE to Arduino
    GND RS to Arduino pin 11 R/W to Arduino pin 10 E to Arduino pin 9 DB4 to Arduino pin 2 DB5 to Arduino 
    pin 3 DB6 to Arduino pin 4 DB7 to Arduino pin 5 LED+ to Arduino 5V LED- to Arduino GND Modified
 
    #include<LiquidCrystal.h>
    LiquidCrystal lcd(22,24,26,28,30,32); 
    
    #define trigPin 13 
    #define echoPin 12 
    #include "Keypad.h"
    #include <Wire.h>
    
    long duration, distance;
    int pwm = 11;
    const byte ROWS = 4; //four rows
    const byte COLS = 4; //four columns
    char keys[ROWS][COLS] =
    {
      {
        '1','2','3','A'  }
      ,
      {
        '4','5','6','B'  }
      ,
      {
        '7','8','9','C'  }
      ,
      {
        '*','0','#','D'  }
    };
    byte rowPins[ROWS] = {
      5, 4, 3, 2}; //connect to the row pinouts of the keypad
    byte colPins[COLS] = {
      9, 8, 7, 6}; //connect to the column pinouts of the keypad
     
    Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
     int duty, d;
    
    void setup()
    {
      Serial.begin(9600);
      lcd.begin(16,2); 
      // lcd.clear();
      lcd.print("Enter Speed");
      pinMode(trigPin, OUTPUT);
      pinMode(echoPin, INPUT);
      
      //Tell Arduino to start your 16 column 2 row LCD 
      lcd.setCursor(12,0); //Set LCD cursor to upper left corner, column 0, row 0
     
    }
    
    
     
    
     
    void loop()
    {
     duty = GetNumber();
        Serial.print(duty);
        lcd.print(duty);
        Serial.print("\n");
        Sendattemp(duty);
        while(1){
        d = Distance();
        Serial.print("Distance between you and the following car :");
        lcd.print("d: ");
        lcd.print(d);
        Serial.print("\n");
         Serial.print(d); //Print Message on First Row 
         Serial.print("\n");
    
         if (d < 10) {
              Sendattemp(duty - 30);
         } else 
         Sendattemp(duty);
      
        }
    }
    
    
    void   Sendattemp(int a) {
    int s = (a*255)/100;
     analogWrite(pwm, a); // writes "84" (duty cycle 32%).
        delay(200);
    
    }
    
    
    int GetNumber()
    {
      Serial.print ("Enter the predetermined speed : ");
       int num = 0;
       char key = keypad.getKey();
       while(key != '#')
       {
          switch (key)
          {
             case NO_KEY:
                break;
    
             case '0': case '1': case '2': case '3': case '4':
             case '5': case '6': case '7': case '8': case '9':
             
                num = num * 10 + (key - '0');
                break;
    
             case '*':
               return num = 0;
              //  Serial.clear();
                
          }
    
          key = keypad.getKey();
       }
    
       return num;
    }
    
    
    int   Distance(){
    
    
        digitalWrite(trigPin, LOW); 
        delayMicroseconds(2);
        digitalWrite(trigPin, HIGH);
        delayMicroseconds(10);
        digitalWrite(trigPin, LOW); 
        duration = pulseIn(echoPin, HIGH);
        distance = (duration/2)/ 29.1;
      //  LCD.setCursor(0,1); //Set cursor to first column of second row 
       // LCD.print(" "); //Print blanks to clear the row
       // LCD.setCursor(0,1); //Set Cursor again to first column of second row
      // LCD.print(distance); //Print measured distance 
       // LCD.print(" cm"); //Print your units.
        delay(250); //pause to let things settle
    
        return distance;
        }

but now unfortunately not working I don't know where is the mistake..

The code does something. You expect it to do something. The mistake is expecting it to something other than what it does.

If you need help making it do what you expect, you'll need to describe what you expect it to do, and to describe what it actually does.

system was working very well before some days.. but now unfortunately not working I don't know where is the mistake..

If you have not changed the code then the problem is in teh wiring or you have fried the Arduino.

How is the motor powered ?

    void   Sendattemp(int a) {
    int s = (a*255)/100;
     analogWrite(pwm, a); // writes "84" (duty cycle 32%).  ??? really ???
        delay(200);
    
    }

You calculate the value of s then don't use it. Is there a reason for this ?

UKHeliBob:
If you have not changed the code then the problem is in teh wiring or you have fried the Arduino.

How is the motor powered ?

    void   Sendattemp(int a) {

int s = (a*255)/100;
    analogWrite(pwm, a); // writes "84" (duty cycle 32%).  ??? really ???
       delay(200);
   
   }



You calculate the value of s then don't use it. Is there a reason for this ?

it was just for testing the code i put s as comment and send a instead of s..

sometimes it worked sometimes it didnt work .. really i dont know why..

the motor is powered by arduino ! is it problem :frowning: :confused:

if i can replace equation of calculating s then help me please

the motor is powered by arduino ! is it problem

Quite possibly. How much current does it take, particularly when starting or stalled ?

What sort of value do you see in the duty variable and/or the value you use with analogWrite() ?

i put s as comment and send a instead of s..

I have no idea what that means.