What could be the problem to our medicine dispencing machine?

Good day can someone help us to troubleshoot our issue our medicine dispensing and logging system ?

  1. We’ve built a device using HTML, CSS, JavaScript, Python, and an Arduino Mega 2560 Mini for hardware.
  2. The Raspberry Pi 4B is connected via serial communication pins 4-13.
  3. Regardless of the medicine type input by the user, pin 13 always activates the servo motor for dispensing.
  4. We’ve verified the Python code, and it sends the right data.

Heres the are the adruino and phyton code:

Phyton:

from flask_cors import CORS
import serial
import time
import json  # Import the json module 

app = Flask(__name__)
CORS(app) 

@app.route('/to_arduino', methods=['POST'])
def to_arduino():
    data = request.json
    print("Data received from JS:", data)
    
    # Determine which function to call based on the quantity value
    quantity = data.get('quantity')
    if quantity == '1':
        send_to_arduino_quantity_1(data)
    elif quantity == '2':
        send_to_arduino_quantity_2(data)
    else:
        print("Unknown quantity value received")
    
    return "Data received successfully" 

def send_to_arduino_quantity_1(data):
    """Function to send data to Arduino when quantity is 1."""
    try:
        # Open a serial connection to Arduino
        ser = serial.Serial('COM8', 9600, timeout=10)
        print("Connection Successful") 

        # Convert data dictionary to a JSON string
        data_str = json.dumps(data) 

        # Store the start time
        start_time = time.time() 

        # Run the loop until 5 seconds have passed
        while time.time() - start_time < 3:
            # Send the encoded data to Arduino
            ser.write(data_str.encode())
            
            # Sleep for a short time before sending the data again
            time.sleep(0.5)  # Adjust sleep time as needed 

            print(data_str)
        
        print("Sending data for quantity 1 completed")
        

    except Exception as e:
        print(f"Error during data sending for quantity 1: {e}")
    finally:
        # Close the serial connection
        ser.close()
        print("Serial connection closed for quantity 1") 

def send_to_arduino_quantity_2(data):
    """Function to send data to Arduino when quantity is 2."""
    try:
        # Open a serial connection to Arduino
        ser = serial.Serial('COM8', 9600, timeout=10)
        print("Connection Successful") 

        # Convert data dictionary to a JSON string
        data_str = json.dumps(data) 

        # Store the start time
        start_time = time.time() 

        # Run the loop until 5 seconds have passed
        while time.time() - start_time < 5:
            # Send the encoded data to Arduino
            ser.write(data_str.encode())
            
            # Sleep for a short time before sending the data again
            time.sleep(0.1)  # Adjust sleep time as needed
            print(data_str) 

        
        print("Sending data for quantity 2 completed") 

    except Exception as e:
        print(f"Error during data sending for quantity 2: {e}")
    finally:
        # Close the serial connection
        ser.close()
        print("Serial connection closed for quantity 2") 

if __name__ == '__main__':
    app.run(host='localhost', debug=True)

Adruino:

const int LED_PIN_1 = 13;
const int LED_PIN_2 = 11;
const int LED_PIN_3 = 9;
const int LED_PIN_4 = 7;
const int LED_PIN_5 = 5;
const int LED_PIN_6 = 4;
const int LED_PIN_7 = 6;
const int LED_PIN_8 = 8;
const int LED_PIN_9 = 10;
const int LED_PIN_10 = 12; 

String received; // Define the 'received' variable globally 

void setup() {
  Serial.begin(9600);  // Start the serial communication at 9600 baud rate
  pinMode(LED_PIN_1, OUTPUT); // Set LED pins as output
  pinMode(LED_PIN_2, OUTPUT);
  pinMode(LED_PIN_3, OUTPUT);
  pinMode(LED_PIN_4, OUTPUT);
  pinMode(LED_PIN_5, OUTPUT);
  pinMode(LED_PIN_6, OUTPUT);
  pinMode(LED_PIN_7, OUTPUT);
  pinMode(LED_PIN_8, OUTPUT);
  pinMode(LED_PIN_9, OUTPUT);
  pinMode(LED_PIN_10, OUTPUT);
} 

void loop() {
  if (Serial.available() > 0) {  // Check if there is incoming data
    received = Serial.readStringUntil('n');  // Read the data until newline
    Serial.println(received);  // Send back a confirmation message
    messageConditioning(); // Blink and rotate the LEDs upon successful connection
  }
} 

void messageConditioning() {
  // Bioflu
  if (received .equals( "{"medicineType": "BIOFLU", "quantity": "1"}")) {
    bioflu_1();
  } else if (received .equals( "{"medicineType": "BIOFLU", "quantity": "2"}")) {
    bioflu_2();
  }
  // Paracetamol
  else if (received .equals( "{"medicineType": "PARACETAMOL", "quantity": "1"}")) {
    paracetamol_1();
  } else if (received .equals( "{"medicineType": "PARACETAMOL", "quantity": "2"}")) {
    paracetamol_2();
  }
  // Ambroxol
  else if (received .equals( "{"medicineType": "AMBROXOL", "quantity": "1"}")) {
    ambroxol_1();
  } else if (received .equals( "{"medicineType": "AMBROXOL", "quantity": "2"}")) {
    ambroxol_2();
  }
  // Dequadin
  else if (received .equals( "{"medicineType": "DEQUADIN", "quantity": "1"}")) {
    dequadin_1();
  } else if (received .equals( "{"medicineType": "DEQUADIN", "quantity": "2"}")) {
    dequadin_2();
  }
  // Solmux
  else if (received .equals( "{"medicineType": "SOLMUX", "quantity": "1"}")) {
    solmux_1();
  } else if (received .equals( "{"medicineType": "SOLMUX", "quantity": "2"}")) {
    solmux_2();
  }
  // Alaxan
  else if (received .equals( "{"medicineType": "ALAXAN", "quantity": "1"}")) {
    alaxan_1();
  } else if (received .equals( "{"medicineType": "ALAXAN", "quantity": "2"}")) {
    alaxan_2();
  }
  // Mefenamic
  else if (received .equals( "{"medicineType": "MEFENAMIC", "quantity": "1"}")) {
    mefenamic_1();
  } else if (received .equals( "{"medicineType": "MEFENAMIC", "quantity": "2"}")) {
    mefenamic_2();
  }
  // Bonamine
  else if (received .equals( "{"medicineType": "BONAMINE", "quantity": "1"}")) {
    bonamine_1();
  } else if (received .equals( "{"medicineType": "BONAMINE", "quantity": "2"}")) {
    bonamine_2();
  }
  // Diatabs
  else if (received .equals("{"medicineType": "DIATABS", "quantity": "1"}")) {
    diatabs_1();
  } else if (received .equals( "{"medicineType": "DIATABS", "quantity": "2"}")) {
    diatabs_2();
  }
  // Betadex
  else if (received .equals( "{"medicineType": "BETADEX", "quantity": "1"}")) {
    betadex_1();
  } else if (received .equals( "{"medicineType": "BETADEX", "quantity": "2"}")) {
    betadex_2();
  }
  // Default case
  else {
    allLightsOn();
  }
} 

// Bioflu Output
void bioflu_1() {
  digitalWrite(LED_PIN_1, HIGH);
  delay(500);
  digitalWrite(LED_PIN_1, LOW);
  delay(500);
} 

void bioflu_2() {
  for (int i = 0; i < 2; i++) {
    digitalWrite(LED_PIN_1, HIGH);
    delay(500);
    digitalWrite(LED_PIN_1, LOW);
    delay(500);
  }
} 

void paracetamol_1() {
  digitalWrite(LED_PIN_2, HIGH);
  delay(500);
  digitalWrite(LED_PIN_2, LOW);
  delay(500);
} 

void paracetamol_2() {
  for (int i = 0; i < 2; i++) {
    digitalWrite(LED_PIN_2, HIGH);
    delay(500);
    digitalWrite(LED_PIN_2, LOW);
    delay(500);
  }
} 

// Ambroxol Output
void ambroxol_1() {
  digitalWrite(LED_PIN_3, HIGH);
  delay(500);
  digitalWrite(LED_PIN_3, LOW);
  delay(500);
} 

void ambroxol_2() {
  for (int i = 0; i < 2; i++) {
    digitalWrite(LED_PIN_3, HIGH);
    delay(500);
    digitalWrite(LED_PIN_3, LOW);
    delay(500);
  }
} 

// Dequadin Output
void dequadin_1() {
  digitalWrite(LED_PIN_4, HIGH);
  delay(500);
  digitalWrite(LED_PIN_4, LOW);
  delay(500);
} 

void dequadin_2() {
  for (int i = 0; i < 2; i++) {
    digitalWrite(LED_PIN_4, HIGH);
    delay(500);
    digitalWrite(LED_PIN_4, LOW);
    delay(500);
  }
} 

// Solmux Output
void solmux_1() {
  digitalWrite(LED_PIN_5, HIGH);
  delay(500);
  digitalWrite(LED_PIN_5, LOW);
  delay(500);
} 

void solmux_2() {
  for (int i = 0; i < 2; i++) {
    digitalWrite(LED_PIN_5, HIGH);
    delay(500);
    digitalWrite(LED_PIN_5, LOW);
    delay(500);
  }
} 

// Alaxan Output
void alaxan_1() {
  digitalWrite(LED_PIN_6, HIGH);
  delay(500);
  digitalWrite(LED_PIN_6, LOW);
  delay(500);
} 

void alaxan_2() {
  for (int i = 0; i < 2; i++) {
    digitalWrite(LED_PIN_6, HIGH);
    delay(500);
    digitalWrite(LED_PIN_6, LOW);
    delay(500);
  }
} 

// Mefenamic Output
void mefenamic_1() {
  digitalWrite(LED_PIN_7, HIGH);
  delay(500);
  digitalWrite(LED_PIN_7, LOW);
  delay(500);
} 

void mefenamic_2() {
  for (int i = 0; i < 2; i++) {
    digitalWrite(LED_PIN_7, HIGH);
    delay(500);
    digitalWrite(LED_PIN_7, LOW);
    delay(500);
  }
} 

// Bonamine Output
void bonamine_1() {
  digitalWrite(LED_PIN_8, HIGH);
  delay(500);
  digitalWrite(LED_PIN_8, LOW);
  delay(500);
} 

void bonamine_2() {
  for (int i = 0; i < 2; i++) {
    digitalWrite(LED_PIN_8, HIGH);
    delay(500);
    digitalWrite(LED_PIN_8, LOW);
    delay(500);
  }
} 

// Diatabs Output
void diatabs_1() {
  digitalWrite(LED_PIN_9, HIGH);
  delay(500);
  digitalWrite(LED_PIN_9, LOW);
  delay(500);
} 

void diatabs_2() {
  for (int i = 0; i < 2; i++) {
    digitalWrite(LED_PIN_9, HIGH);
    delay(500);
    digitalWrite(LED_PIN_9, LOW);
    delay(500);
  }
} 

// Betadex Output
void betadex_1() {
  digitalWrite(LED_PIN_10, HIGH);
  delay(500);
  digitalWrite(LED_PIN_10, LOW);
  delay(500);
} 

void betadex_2() {
  for (int i = 0; i < 2; i++) {
    digitalWrite(LED_PIN_10, HIGH);
    delay(500);
    digitalWrite(LED_PIN_10, LOW);
    delay(500);
  }
} 

void allLightsOn() {
  for (int i = 0; i < 2; i++) {
    digitalWrite(LED_PIN_1, HIGH);
    digitalWrite(LED_PIN_3, HIGH);
    digitalWrite(LED_PIN_5, HIGH);
    digitalWrite(LED_PIN_7, HIGH);
    digitalWrite(LED_PIN_9, HIGH);
    delay(1000);
    digitalWrite(LED_PIN_1, LOW);
    digitalWrite(LED_PIN_3, LOW);
    digitalWrite(LED_PIN_5, LOW);
    digitalWrite(LED_PIN_7, LOW);
    digitalWrite(LED_PIN_9, LOW); 

    digitalWrite(LED_PIN_2, HIGH);
    digitalWrite(LED_PIN_4, HIGH);
    digitalWrite(LED_PIN_6, HIGH);
    digitalWrite(LED_PIN_8, HIGH);
    digitalWrite(LED_PIN_10, HIGH);
    delay(1000);
    digitalWrite(LED_PIN_2, LOW);
    digitalWrite(LED_PIN_4, LOW);
    digitalWrite(LED_PIN_6, LOW);
    digitalWrite(LED_PIN_8, LOW);
    digitalWrite(LED_PIN_10, LOW);
  }
}

Thanks in advance!

We tried printing the values that will be sent to adruino and the values that are recieved by adruino and the datas matched . We just dont know what error is , we eveb tried making it an if and if else conditional statement but no luck the cervo in pin 13 is still the only one moving regardless of user input

New contributor

Ren Averion is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật