abchange the arduino code to matlab code.

조회 수: 17 (최근 30일)
million dadi
million dadi 2024년 4월 7일
댓글: Manikanta Aditya 2024년 4월 7일
// include the library code:
#include <LiquidCrystal.h>
//LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
LiquidCrystal lcd(13, 12, 11, 10, 9, 8);
const int SENSOR_PIN = A0;
const int Motor_PIN = 7;
void setup ()
{
pinMode (SENSOR_PIN, INPUT); // Set the Sensor pin as INPUT
pinMode(Motor_PIN, OUTPUT); // Set the Motor pin as OUTPUT
// set up the LCD's number of columns and rows:
lcd.begin(20, 4);
// set the cursor position:
lcd.setCursor(0,0);
// Print a message to the LCD.
lcd.print(" THE BRIGHT LIGHT ");
// set the cursor position:
lcd.setCursor(0,1);
// Print a message to the LCD.
lcd.print("SMART PLANT WATERING ");
delay(2500);
}
void loop ()
{
int Sensor_Val = analogRead(SENSOR_PIN); //get reading from Sensor
int Moisture = map(Sensor_Val, 0,1019, 0,100);
lcd.setCursor(0, 2);
lcd.print("MOISTER: ");
lcd.print(Moisture);
lcd.print("% ");
if (Moisture < 30) //If Moisture is Less than 30%
{
lcd.setCursor(0, 3);
lcd.print("MOTOR: ON ");
digitalWrite(Motor_PIN, HIGH);
}
else
{
lcd.setCursor(0, 3);
lcd.print("MOTOR: OFF ");
digitalWrite(Motor_PIN, LOW);
}
}
  댓글 수: 1
Manikanta Aditya
Manikanta Aditya 2024년 4월 7일
Here is the MATLAB code for the code you gave:
% Define pins
SENSOR_PIN = 'A0';
Motor_PIN = 'D7';
% Create arduino object
a = arduino();
% Initialize LCD
lcd = addon(a, 'ExampleAddon/LiquidCrystal', 'D13', 'D12', 'D11', 'D10', 'D9', 'D8');
begin(lcd, 20, 4);
setCursor(lcd, 0, 0);
print(lcd, " THE BRIGHT LIGHT ");
setCursor(lcd, 0, 1);
print(lcd, "SMART PLANT WATERING ");
pause(2.5);
% Main loop
while true
% Read sensor value
Sensor_Val = readVoltage(a, SENSOR_PIN);
% Map sensor value to moisture level
Moisture = (Sensor_Val/5)*100;
% Display moisture level
setCursor(lcd, 0, 2);
print(lcd, "MOISTER: " + Moisture + "% ");
% Control motor based on moisture level
if Moisture < 30
setCursor(lcd, 0, 3);
print(lcd, "MOTOR: ON ");
writeDigitalPin(a, Motor_PIN, 1);
else
setCursor(lcd, 0, 3);
print(lcd, "MOTOR: OFF ");
writeDigitalPin(a, Motor_PIN, 0);
end
% Delay before next reading
pause(0.1);
end
You need to have the MATLAB's Arduino support package and you need to have to necessary add-ons to use this.

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

Help CenterFile Exchange에서 Arduino Hardware에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by