Skip to content

Instantly share code, notes, and snippets.

@devwolf75
Created April 16, 2015 18:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save devwolf75/e5719d5caab2de06d851 to your computer and use it in GitHub Desktop.
Save devwolf75/e5719d5caab2de06d851 to your computer and use it in GitHub Desktop.
Codigo para proyecto de petfeeder
RECIBIR.php
<?php
$puerto="/dev/ttyACM0";
if(isset($_GET["codigo"]))
{
$codigo = $_GET["codigo"];
if($codigo == 2)
{
$conexion = fopen($puerto, "w+");
fwrite($conexion, $codigo);
sleep(0.5);
$lectura = fread($conexion, 8);
fclose($conexion);
echo "Distancia: ".$lectura." cm.<br>";
}
echo "Codigo recibido: ".$codigo;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Control Arduino</title>
</head>
<body>
<form action="recibir.php" method="GET">
<button type="submit" name="codigo" value="2">Distancia ultrasonico</button>
</form>
</body>
</html>
INDEX.php
<?php
$puerto="/dev/ttyACM0";
if(isset($_GET["codigo"]))
{
$codigo = $_GET["codigo"];
if($codigo == 1)
{
echo "Motor";
$conexion = fopen($puerto, "w");
fwrite($conexion, $codigo);
fclose($conexion);
}
if($codigo == 2)
{
echo "<script>alert('ultrasonico');</script>";
$conexion = fopen($puerto, "w+");
fwrite($conexion, $codigo);
sleep(0.5);
$lectura = fread($conexion, 10);
fclose($conexion);
echo "Distancia: ".$lectura." cm.<br>";
}
echo "Codigo recibido: ".$codigo;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Control Arduino</title>
</head>
<body>
<form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="GET">
<button type="submit" name="codigo" value="1">Prender motor 3 veces!</button>
<br>
<button type="submit" name="codigo" value="2">Distancia ultrasonico</button>
</form>
</body>
</html>
ARDUINO
//Servomotor
#include <Servo.h>
Servo servo;
int posServo;
int pinServo;
//Ultrasonico
int pinTrigger, pinEcho;
long duracion, cm;
//Datos seriales
int dataReceived;
void setup()
{
Serial.begin(9600);
posServo = 0;
pinServo = 6;
pinTrigger = 12;
pinEcho = 11;
pinMode(pinTrigger, OUTPUT); //Trigger
pinMode(pinEcho, INPUT); //Echo
}
void loop()
{
while(Serial.available()>0)
{
dataReceived = Serial.read();
//Serial.write(dataReceived);
if(dataReceived == '1'){ //GIRAR EL MOTOR 3 VECES.
int contador = 3;
servo.attach(pinServo);
while(contador > 0)
{
servo.write(180);
delay(1500);
servo.write(0);
delay(500);
contador--;
}
servo.detach();
}
if(dataReceived == '2')
{
digitalWrite(pinTrigger,LOW);
delayMicroseconds(5);
digitalWrite(pinTrigger,HIGH);
delayMicroseconds(10);
duracion = pulseIn(pinEcho,HIGH);
cm = int(0.017*duracion);
Serial.println(String(cm));
delay(1000);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment