arduino для управления камерой

miloman00
Offline
Зарегистрирован: 11.12.2014

есть:

arduino

ethernet w5100

android sony x10mini(на нем установлен веб сервер для трансляции видео,к которому нужно добавить кнопки для управления сервами)

ну и естественно сервы

плиз!!!тыкните на пример написания скетча с использованием ethernet shield для управления сервами?

хочу поставить вот сюда

miloman00
Offline
Зарегистрирован: 11.12.2014

нашол похожий проект

miloman00
Offline
Зарегистрирован: 11.12.2014

вот что получилось

#include <SPI.h>
#include <Ethernet.h>
#include <Servo.h> 
#include <SD.h>
#include <EEPROM.h>
#include <SPI.h>

Servo myservo1;
Servo myservo2;
int val1 = 50;
int val2 = 90;
int servoSpeed = 50;
int servoAngle = 10;
int minAngle = -10;
int maxAngle = 190;


#define REQ_BUF_SZ   60


byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 1, 100);
EthernetServer server(80);  
#define W5200_CS  10
#define SDCARD_CS 4
File webFile;               
char HTTP_req[REQ_BUF_SZ] = {0}; 
char req_index = 0;              
void setup()
{
     Serial.begin(9600);       
    
    pinMode(SDCARD_CS,OUTPUT);
    digitalWrite(SDCARD_CS,HIGH);
    Ethernet.begin(mac, ip);
    server.begin();
    Serial.print("server is at ");
    Serial.println(Ethernet.localIP());
    
    
   Serial.println("Initializing SD card...");
    if (!SD.begin(4)) {
   Serial.println("ERROR - SD card initialization failed!");
        return;    
    }
  Serial.println("SUCCESS - SD card initialized.");
    
    if (!SD.exists("webcam.htm")) {
     
        return;  
    }
  Serial.println("SUCCESS - Found webcam.htm file.");
  
  myservo1.attach(6);  
  myservo2.attach(5);  
  myservo1.write(val1);
  myservo2.write(val2);
  delay(100);
  myservo1.detach();  
  myservo2.detach();  
}

void loop()
{
    EthernetClient client = server.available();  

    if (client) { 
        boolean currentLineIsBlank = true;
        while (client.connected()) {
            if (client.available()) {   
                char c = client.read(); 
                
                if (req_index < (REQ_BUF_SZ - 1)) {
                    HTTP_req[req_index] = c;          
                    req_index++;
                }
                
                if (c == '\n' && currentLineIsBlank) {
                    
                    client.println("HTTP/1.1 200 OK");
                    
                    if (StrContains(HTTP_req, "ajax_inputs")) {
                        
                        client.println("Content-Type: text/xml");
                        client.println("Connection: keep-alive");
                        client.println();
    
                       if (StrContains(HTTP_req, "UP=1")) {
                         if(val1<maxAngle-servoAngle){
                         myservo1.attach(5);  
                         for(int i=0;i<servoAngle;i++){
                         myservo1.write(val1+i);                   
                         delay(servoSpeed);
                         }
                        val1=val1+servoAngle;
                        myservo1.detach();
                        }
                       }
                       if (StrContains(HTTP_req, "DOWN=1")) {
                          if(val1>minAngle+servoAngle){
                         myservo1.attach(5);  // attaches the servo on pin 5
                         for(int i=0;i<servoAngle;i++){
                         myservo1.write(val1-i);                  // sets the servo position according to the scaled value 
                         delay(servoSpeed);
                         }
                         val1=val1-servoAngle;
                         myservo1.detach();  // detaches the servo on pin 5
                        }
                       }
                      if (StrContains(HTTP_req, "LEFT=1")) {
                         if(val2<maxAngle-servoAngle){
                         myservo2.attach(6);  // attaches the servo on pin 6
                         for(int i=0;i<servoAngle;i++){
                         myservo2.write(val2+i);                  // sets the servo position according to the scaled value 
                         delay(servoSpeed);
                         }
                         val2=val2+servoAngle;
                         myservo2.detach();  // detaches the servo on pin 6
                        }
                      }
                      if (StrContains(HTTP_req, "RIGHT=1")) {
                         if(val2>minAngle+servoAngle){
                         myservo2.attach(6);  // attaches the servo on pin 6
                         for(int i=0;i<servoAngle;i++){
                         myservo2.write(val2-i);                  // sets the servo position according to the scaled value 
                         delay(servoSpeed);
                         }
                         val2=val2-servoAngle;
                         myservo2.detach();  // detaches the servo on pin 6
                        }
                      }
                        //SetLEDs();
                        //SetServos();
                        // send XML file containing input states
                        //XML_response(client);
                    }
                    else {  // web page request
                        // send rest of HTTP header
                        client.println("Content-Type: text/html");
                        client.println("Connection: keep-alive");
                        client.println();
                        // send web page
                        webFile = SD.open("webcam.htm");        // open web page file
                        if (webFile) {
                            while(webFile.available()) {
                                client.write(webFile.read()); // send web page to client
                            }
                            webFile.close();
                        }
                    }
                    // display received HTTP request on serial port
                   Serial.print(HTTP_req);
                    // reset buffer index and all buffer elements to 0
                    req_index = 0;
                    StrClear(HTTP_req, REQ_BUF_SZ);
                    break;
                }
                // every line of text received from the client ends with \r\n
                if (c == '\n') {
                    // last character on line of received text
                    // starting new line with next character read
                    currentLineIsBlank = true;
                } 
                else if (c != '\r') {
                    // a text character was received from client
                    currentLineIsBlank = false;
                }
            } // end if (client.available())
        } // end while (client.connected())
        delay(1);      // give the web browser time to receive the data
        client.stop(); // close the connection
    } // end if (client)
}

// sets every element of str to 0 (clears array)
void StrClear(char *str, char length)
{
    for (int i = 0; i < length; i++) {
        str[i] = 0;
    }
}

// searches for the string sfind in the string str
// returns 1 if string found
// returns 0 if string not found
char StrContains(char *str, char *sfind)
{
    char found = 0;
    char index = 0;
    char len;

    len = strlen(str);
    
    if (strlen(sfind) > len) {
        return 0;
    }
    while (index < len) {
        if (str[index] == sfind[found]) {
            found++;
            if (strlen(sfind) == found) {
                return 1;
            }
        }
        else {
            found = 0;
        }
        index++;
    }

    return 0;
}

а вот html ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

<!DOCTYPE
 html>
<html>
    <head>
        <title>Arduino Ajax I/O</title>
        <script>
	    strUP = "";
		strDOWN = "";
	    strLEFT = "";
		strRIGHT = "";
		var UP_state;
		var DOWN_state;
		var LEFT_state;
		var RIGHT_state;
		
		function GetArduinoIO()
		{
		    var request = new XMLHttpRequest();
			nocache = "&nocache=" + Math.random() * 1000000;
			// send HTTP GET request with LEDs to switch on/off if any
			request.open("GET", "ajax_inputs" + strUP + strDOWN + strLEFT + 
strRIGHT + nocache, true);
			request.send(null);
			setTimeout('GetArduinoIO()', 1000);
			strUP = "";
			strDOWN = "";
			strLEFT = "";
			strRIGHT = "";
			
		}
		// service LEDs when checkbox checked/unchecked

		function GetButton3()
		       {	
				UP_state = 1;
				strUP = "&UP=1";
			   }
		
		function GetButton4()
		       {	
				DOWN_state = 1;
				strDOWN = "&DOWN=1";
			   }
		
		function GetButton5()
		       {	
				LEFT_state = 1;
				strLEFT = "&LEFT=1";
			   }
		function GetButton6()
		       {	
				RIGHT_state = 1;
				strRIGHT = "&RIGHT=1";
			   }
	
</script>

<style>
#container {
    margin: 0px auto;
    width: 500px;
    height: 375px;
    border: 10px #333 solid;
}
#videoElement {
    width: 500px;
    height: 375px;
    background-color: #666;
}
</style>
		
	<style>
		.IO_box {
			float: left;
			margin: 0 20px 20px 0;
			border: 1px solid blue;
			padding: 0 5px 0 5px;
			width: 120px;
		}
		h1 {
			font-size: 120%;
			color: blue;
			margin: 0 0 10px 0;
		}
		h2 {
			font-size: 85%;
			color: #5734E6;
			margin: 5px 0 5px 0;
		}
		p, form, button {
			font-size: 80%;
			color: #252525;
		}
		.small_text {
			font-size: 70%;
			color: #737373;
		}

	</style>
    </head>
    <body onload="GetArduinoIO()">
        <h1>Arduino Ajax I/O</h1>
				<div class="IO_box">
			<h2>WEBCAM Control</h2>
			<button type="button" id="UP" 
onclick="GetButton3()">..UP...</button>
			<button type="button" id="DOWN" 
onclick="GetButton4()">DOWN.</button><br /><br />
			<button type="button" id="LEFT" 
onclick="GetButton5()">.LEFT.</button>
			<button type="button" id="RIGHT" 
onclick="GetButton6()">RIGHT</button><br /><br />
		</div>	
  	
<table>

    <tr>
        <td>
            <script type="text/javascript">
                document.write("<img alt='Robot Eye' class='bot-eye' src='http://192.168.1.1:8080/?action=stream'/>")
            </script>
        </td>
    </tr>
    
</table>

    </body>
</html>

 

miloman00
Offline
Зарегистрирован: 11.12.2014

хочу попробовать написать скетч самому,без sd карты