abchange the arduino code to matlab code.

17 次查看(过去 30 天)
// 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