Jump to content

Tracking Pixel Color Using Arduino


jeffmx
 Share

Recommended Posts

http://youtu.be/2-2jpvvDP98

I finish this litle project and I want share with AUTOIT community.

AUTOIT CODE

#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <Webcam.au3>
#include 'CommMG.au3'
#CS
Tracking Pixel Color Using ARDUINO by JEFF
MADE IN CUCEI
2012
#CE
_WebcamInit()
$X_MAX = 360
$Y_MAX = 430
$Y_Diference = $Y_MAX  - $X_MAX
$Gui_X = @DesktopHeight / 2 - $X_MAX / 2
$Gui_Y = @DesktopWidth / 2 - $Y_MAX / 2
$Form1 = GUICreate("WEBCAM POSITION CONTROL By JEFF", $X_MAX, $Y_MAX , $Gui_Y, $Gui_X)
GUISetCursor (3)
_Webcam($Form1, 360, 360, 0, 0)
$DUMY = GUICtrlCreateLabel("",0,$Y_Max - $Y_Diference,360,5)
GUICtrlSetBkColor($DUMY, 0xFFFFFF)
$CONECT = GUICtrlCreateButton ("CONNECT",130,390,75,30)
GUICtrlSetState ($CONECT , $GUI_DISABLE)
$Set_Color = GUICtrlCreateButton ("COLOR",10,390,75,30)
GUICtrlCreateLabel ("X Map",10,375,60,13)
$X_MAP = GUICtrlCreateLabel ("X",60,375,30,13)
GUICtrlCreateLabel ("Y Map",230,375,60,13)
$Y_MAP = GUICtrlCreateLabel ("Y",280,375,30,13)
$Color = GUICtrlCreateLabel("",310,390,25,25)
GUICtrlSetState ($Color , $GUI_HIDE)
GUISetState(@SW_SHOW)
$Send = 0
$Pixel_Color = 0x00FF00
$Main_X = 0
$Main_Y = 0
$Master_X = 90
$Master_Y = 30
While 1
$nMsg = GUIGetMsg()

$Location = GUIGetCursorInfo ($Form1)
$X_POS = $Location[0]
$Y_POS = $Location[1]

If $X_POS < $X_MAX And $Y_POS < $Y_MAX - $Y_Diference And $X_POS > 0 And $Y_POS > 0 Then
 
  If $Send = 1 Then
   $Loc_Pixel = Search_PixelC ($Pixel_Color)
  
   If $Loc_Pixel <> 0 Then
    $Loc_Pixel_X = $Loc_Pixel[0]
    $Loc_Pixel_Y = $Loc_Pixel[1]   
    Target ($Loc_Pixel_X, $Loc_Pixel_Y)
   EndIf
  
  EndIf
EndIf

Switch $nMsg
  Case $GUI_EVENT_CLOSE
   Exit
 
  Case $CONECT
 
   If _CommSetport(4,"Fail Connection Port",9600,8,0,1,0,0,0) = 0 Then
    MsgBox (16,"ERROR","Error al conectar puerto predeterminado verifica que las opciones sean validas."&GUICtrlRead ($CmboPortsAvailable)&" "&GUICtrlRead ($CmBoBaud)&" "&GUICtrlRead ($CmboDataBits))
    Exit
   EndIf
  
   $Send = 1
 
  Case $Set_Color
  
   SplashTextOn ("PICK UP COLOR","Waiting Click On Mouse",150,75, "","",0)
   GUICtrlSetState ($Color , $GUI_Show)
  
   Do
    $Location = GUIGetCursorInfo ($Form1)
    $MOUSE_LOC =  MouseGetPos ()
   
;~   $Pixel_Color = PixelGetColor ($MOUSE_LOC[0],$MOUSE_LOC[1])  
    GUICtrlSetBkColor ($Color,$Pixel_Color)
;~   ClipPut ($Pixel_Color)
;~   Sleep (25)
   
   Until $Location[2] <> 0
     
   SplashOff()
  
   GUICtrlSetState ($CONECT , $GUI_ENABLE)
  
EndSwitch
WEnd
Func Map ($Value, $Min, $Max, $Min_Func, $Max_Func )
Local $VT = Abs ($Max - $Min)
Local $FT = Abs ($Max_Func - $Min_Func)
Local $Aprox = Abs ($Value * $FT)
    $Aprox = Int ($Aprox / $VT)
  If $Aprox = 0 Then $Aprox = 1
Return $Aprox
EndFunc
Func Search_PixelC ($Color)
Local $Coor_Pixel[2]
Local $Gui_Pos = WinGetPos ($Form1)
Local $Left = $Gui_Pos[0]
Local $Top = $Gui_Pos[1]
Local $Right = $Gui_Pos[0] + $X_MAX
Local $Botton = $Gui_Pos[1] + $Y_MAX - $Y_Diference
Local $Pixel_Loc = PixelSearch ($Left, $Top, $Right, $Botton, $Color, 5, 1, $Form1)

If Not @error Then
  $PixelX = $Pixel_Loc[0] - $Left
  $Coor_Pixel[0] = Map ($PixelX,$Left,$Right,1,181)
  $PixelY = $Pixel_Loc[1] - $Top
  $Coor_Pixel[1] = Map ($PixelY,$Top,$Botton,71,131)
 
  GUICtrlSetData ($X_MAP,$Coor_Pixel[0])
  GUICtrlSetData ($Y_MAP,$Coor_Pixel[1])
  Return $Coor_Pixel
Else
  Return 0
EndIf
EndFunc
Func Target ($PosX , $PosY)
Switch $PosX
  Case 1 To 70
   $Master_X = $Master_X + 2
   _CommSendString ("Ser1"&Chr($Master_X))
   Sleep (25)
  
  Case 112 To 180
   $Master_X = $Master_X - 2
   _CommSendString ("Ser1"&Chr($Master_X))
   Sleep (25)
   
EndSwitch
  
Switch $PosY
  Case 1 To 21
   $Master_Y = $Master_Y - 2
   _CommSendString ("Ser2"&Chr($Master_Y))
   Sleep (25)
  
  Case 41 To 61
   $Master_Y = $Master_Y + 2
   _CommSendString ("Ser2"&Chr($Master_Y))
   Sleep (25)
 
EndSwitch
EndFunc

ARDUINO CODE

#include <Servo.h>
#include <LiquidCrystal.h>
/*
TRACKING PIXEL COLOR BY JEFF
MADE IN CUCEI 2012
*/
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
Servo myservo1;
Servo myservo2;
int pos = 0;
int inByte = 0;
const int Servo1 = 8;
const int Servo2 = 7;
int Step = 0;
void setup()
{
  myservo1.attach(Servo1);
  myservo2.attach(Servo2);
  myservo1.write(90);
  myservo2.write(30);
 
  Serial.begin(9600);
  lcd.begin(16, 2);
  lcd.clear();
  lcd.print("SERVOMOTORES");
  lcd.setCursor(0,1);
}


void loop()
{
  if (Serial.available())
  {
   
    inByte = Serial.read();
  
    switch (Step)
    {
      case 0:
      //S
      if (inByte == 83)
      {
      Step = 1;
      }
      else
      {
      Step = 0;
      }
      break;
     
      case 1:
      //e
      if (inByte == 101)
      {
      Step = 2;
      }
      else
      {
      Step = 0;
      }
      break;
     
      case 2:
      //r
      if (inByte == 114)
      {
      Step = 3;
      }
      else
      {
      Step = 0;
      }
      break;
     
      case 3:
     
        //1
        if (inByte == 49)
        {
          Step = 4;
        }
        else if (inByte == 50)
        {
          Step = 5;
        }
        else
        {
          Step = 0;
        }
      break;
     
      case 4:
        myservo1.write(inByte);
        Serial.flush();
        Step = 0;
      break;
         
      case 5:
        myservo2.write(inByte);
        Serial.flush();
        Step = 0;
      break;
         
      default:
        Step = 0;
      }
  }
  
}
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...