Show Posts
Pages: 1 ... 60 61 [62] 63 64 ... 93
916  Using Arduino / Project Guidance / Re: Parsing image for processing on Arduino on: April 07, 2012, 12:02:05 pm
1. The posted code relates to the beginning of my initial paragraph, using Arduino to detect motion when there's enough light. 

2. After "Instead" I wrote about using Processing on a PC to detect an IR laser dot in the dark.  Sorry for the confusion.

What does the Uno do?

1. In the 1st case, it does it all, without a PC.  Detecting motion, sounding an alarm, printing levels, sending a decent picture only when there is motion.  See posted code above.

2. In the 2nd case, it only sends a small picture.  Depending on the PC to analyze it.  It can do this at 2fps over XBee remotely 1000' away.  The Processing code is long, complex and difficult to read.  Will improve it before I post.
917  Using Arduino / Project Guidance / Re: NEED PROJECT IDEAS on: April 07, 2012, 11:15:04 am
IR laser detection.  When something moves at night it will detect the beam being blocked with no reflector at the other end.
I've got a static demonstration working, next I want to put the laser on a motor and detect WHERE the object is.
918  Using Arduino / Project Guidance / Re: Parsing image for processing on Arduino on: April 07, 2012, 11:12:09 am
You can see this is not Processing.  That code is much longer.  It should work with any Serial JPG camera.  There are a few different brands, but the command set in my code is the same.  It simply looks for a change in the length of the JPG compression.  It does this over 2 time frames, 1 shorter for a fast movement.  And longer for a static change.  SImple?

You can simplify the program flow by commenting out the last 3 if's in loop().
I added a beep and tested by standing still across the room, then moving.
919  Using Arduino / Project Guidance / Re: Parsing image for processing on Arduino on: April 07, 2012, 10:22:39 am
Can detect even the slightest finger movement from 10m.  Person walking towards at 50m.  Perpendicular path at 100m.
These results require good lighting.  It fails when the moving object is the same color as it's background of course.
It works better when there is texture on the background and moving object.
Some parameters require tweaking when it's darker in the room.
A bright window in an otherwise dim room causes problems, the shadows have noise.
It fails to detect a laser dot in the dark, but I've solved that on PC!
It detects a static change to the scene, yet ignores moving shadows by resetting every 200 samples.
It detects fast motion like a baseball being thrown across frame.
Can send the picture to PC for display when detected.

Code:
#define FLUSH Serial.flush()
#define WR(s) Serial.print(s)
#define WR13 Serial.write(10);
#define WR32 Serial.write(32);
#define HALT {while(1);}
#define CMD(x,var) for(cii=0;cii<x;cii++)Serial.print(var[cii])
//#define DEBUG1 WR13 for(i=0;i<5;i++){Serial.print( int(Serial.read()),HEX);WR32} WR13
//#define DEBUG2 WR13 for(i=0;i<5;i++){Serial.print(char(Serial.read())    );WR32} WR13
char length[] ={0,0,0,0};
char cmdRST[] ={0x56,0,0x26,0}; //reset then >10ms
char cmdCAP[] ={0x56,0,0x36,1,0};
char cmdLEN[] ={0x56,0,0x34,1,0};
char cmdCON[] ={0x56,0,0x36,1,2};
char cmdGET[] ={0x56,0,0x32,0x0c,0,10,0,0,0,0,0,0,0,0,0,10}; //x=16 10*0.01ms
//x=9:
//0x36,d10-80 56k-8k from CommTool, here 40-255
char cmdCMP[] ={0x56,0,0x31,5,1,1,0x12,4,0x36};
char cmd640[] ={0x56,0,0x31,5,4,1,0,0x19,0};
char cmd320[] ={0x56,0,0x31,5,4,1,0,0x19,0x11};
char cmdOFF[] ={0x56,0,0x3e,3,0,1,1}; //[6]=1,0 x=7,30ma,60ma
char cmdBAU[] ={0x56,0,0x24,3,1,0xae,0xc8}; //9600
int i,j,k,last,cii,dlen,cnt=0,a[10];
unsigned int picLen;
boolean same2x=0;

void setup(){
pinMode(13,OUTPUT);
Serial.begin(115200);
CMD(4,cmdRST);delay(500);FLUSH;  //need time to get long string
//CMD(7,cmdBAU);delay(500);Serial.begin(9600);
cmdCMP[8]=200;
CMD(9,cmdCMP);delay(10);
//CMD(9,cmd640);delay(10);  //remembers even with long power off how?  causes image corrupt?
//still in R buffer OK?
}
void loop(){
for(k=0,j=0;j<10;j++){
digitalWrite(13,1);
CMD(5,cmdCAP);delay(60);  //try while instead?
digitalWrite(13,0);
CMD(5,cmdLEN);
FLUSH;  //wrong order on purpose
while(Serial.available()<9) ;
for(i=0;i<5;i++)Serial.read();
for(i=0;i<4;i++)length[i]=Serial.read();
//if(Serial.available())WR("Err");
picLen=((unsigned int)length[2])*256 +int(length[3]);
if(j<9)CMD(5,cmdCON); //delay(10);
k+=picLen/30;
a[j]=picLen/30;
}  //for
WR13
i=abs(k-last)/5-6;
if(i<0)i=0;
WR(dlen=i);WR32
for(j=0,i=0;i<10;i++)
  j+=abs(a[i]-k/10);
j-=50;
if(j<0)j=0;
WR(j/5);WR32

cnt++;
if(((dlen>=5)||(j>=10))&&(cnt>5)) {  //20,cnt>50? 5,2
  WR13
  //getdata(picLen); 
  cnt=0;
  same2x=0;
}
CMD(5,cmdCON);
if(abs(k-last)/5<=2)same2x=1;
if(cnt%200==0)same2x=0; //200
if(!same2x)
  last=k;
//delay(500);
}
void getdata(unsigned int len){
const int BSIZE=64;
int count=len/BSIZE;
int tail=len&(BSIZE-1);
unsigned int addr=0;
cmdGET[6]=cmdGET[7]=cmdGET[8]=cmdGET[9]=0;
cmdGET[13]=BSIZE;
for(int i=0;i<count;i++){
  //cmdGET[15]=10;
  CMD(16,cmdGET);
  delay(10); //10
  if(Serial.available()!=BSIZE+10)HALT
//Serial.read();Serial.read();
//if(Serial.read()!=0x32)Serial.println("Halt?");
  FLUSH;
  addr+=BSIZE;
  cmdGET[8]=addr>>8;
  cmdGET[9]=addr;
  }
cmdGET[13]=tail;
CMD(16,cmdGET);
delay(100);
//Serial.read();Serial.read();
//if(Serial.read()!=0x32)Serial.println("Halt?");
FLUSH;
}
920  Using Arduino / Motors, Mechanics, and Power / Re: record data from potentiometer & playback to servo on: April 07, 2012, 09:23:19 am
Each PC app is different.  First they should export.  Then I can use Processing to translate that text file to array syntax.  Then cut and paste into IDE, compile, run.

Alternatively I could write a Sketch to take data from serial port and put it into array.  Still  need Processing to translate exported file to Serial data.  I haven't found an app that exports to serial port yet.  Then I could skip the middle man?
921  Using Arduino / Project Guidance / Re: Variation of an inertial navigation system on: April 07, 2012, 09:15:55 am
Ordered this:
http://www.ebay.com/itm/330708102771?ssPageName=STRK:MEWNX:IT&_trksid=p3984.m1439.l2649
Will try to do something similar to what you wish, with much lower expectations.

Talk about low power!
http://www.fastraxgps.com/products/gpsantennamodules/400series/uc430/
922  Using Arduino / Project Guidance / Re: Arduino Longterm on: April 07, 2012, 08:46:33 am
I've used GPS to calibrate my Arduino clock to nearly 1ppm.  It's stable and consistent at a given temperature.  The calibration process takes about an hour.  With GPS you do not need an accurate RTC.  Even when you briefly lose the signal.  <10ppm.
923  Using Arduino / Motors, Mechanics, and Power / Re: New Libray for L298N on: April 07, 2012, 08:32:33 am
The apparent torque depends on the RPM you choose.  You can use up to 10x the rated continuous voltage for a stepper.  But you cannot leave this voltage on to hold it into position for long.  Be careful!  Steppers can run hot safely, but there is a limit.
924  Using Arduino / Motors, Mechanics, and Power / Re: New 28BYJ-48 library on: April 07, 2012, 08:28:29 am
Pasted from above:

The library is setup to run other code in loop while spinning the motor non-blocking without using interrupts.  First initialize the move you want to make 1x.  This could be done in setup or an if statement in loop for example:

void degrpm_nowait (boolean bcw, long deg100, int rpm100)

Then call this function often:

int calloften_micro (boolean bcw)

At faster speeds you have to call it more often or it will get behind and run slower.  You can look at d in loop to see how much time you have in micros.  There are lines to uncomment if you want it to be more smooth consistent and partly blocking, some of the time.  The calloften functions use little integer math so they are fast.

There is another pair of functions for going slower and cooler using 12v:
void degrpmslowCool4_nowait (boolean bcw, long deg100, int rph100)
int calloften()

With these functions you have much more free time to process in loop, d milliseconds.  If you miss a step it's no big deal, the motor will catch up to keep the speed mostly constant.  It will move the correct distance no matter what you do wrong with timing.

All you have to do is choose the RPM and *100.
925  Using Arduino / Project Guidance / Re: Parsing image for processing on Arduino on: April 07, 2012, 08:17:57 am
I've got WORKING code to detect movement from a Grove serial camera.  That's all it does, nothing more.  It can do this at about 10fps on Arduino.  It's a little tricky but it's well tested!  This method does not work for detecting a laser dot in the dark, due to all the noise and compression artifacts.  Instead I did it like this:  Change the compression so the jpg file is only 4k.  Send it to PC over XBee in 1/2 sec.  The Arduino doesn't even see the data, it's sent directly from camera to PC, with Uno sending commands to camera.  Using Processing it is only a few lines of code to uncompress the file and access each pixel by x,y.  Then I can track the location of the IR laser in the dark at 50m.  This is like blob tracking?  Except there is only one blob in the image when it's dark.  Finished today!  Testing and working at 2fps!  Is this useful to anyone?
926  Using Arduino / Project Guidance / Re: Using Arduino to send texts and photos on: April 07, 2012, 07:40:20 am
You can do it in text mode.  See sndmsg().  I have not looked at this for awhile, but it works with my phone.

Code:
#include <avrpins.h>
#include <max3421e.h>
#include <usbhost.h>
#include <usb_ch9.h>
#include <Usb.h>
#include <usbhub.h>
#include <avr/pgmspace.h>
#include <address.h>
#include <cdcacm.h>
#include <printhex.h>
#include <message.h>
#include <hexdump.h>
#include <parsetools.h>
#include "pgmstrings.h" 

#define IF0 if(0){
#define NDREC rcvd=64;rcode=Acm.RcvData(&rcvd,buf);
#define DELREC delay(2000);NDREC
#define DELREC1 delay(500);NDREC
#define PBUF for(i=0;i<rcvd;i++)Serial.print(buf[i]);
#define PRCV Serial.print(rcvd);
#define PHLIGHT sndstr("ATD611");delay(500);sndstr("/r");
#define CALLHU(num) sndstr(num);delay(15000);sndstr("/rATH");DELREC1;DELREC;
#define P32 Serial.write(32);
#define P13 Serial.write(13);

int first=1;
int j,svc,rss,hr,mi,se;
long k;
uint8_t  rcode,buf[64];           
uint16_t rcvd=64,i;
String s="1234567890123456789012345678901234567890123456789012345678901234"; //64
char t[12]="AT+CMSS=nn";
char cmgr[12]="AT+CMGR=nn";
char mnum[3]="nn";

int pirPin = 3;
int pirVal;

void(* resetFunc)(void)=0; //declare reset function @ address 0
class ACMAsyncOper : public CDCAsyncOper {
public: virtual uint8_t OnInit(ACM *pacm);
};
uint8_t ACMAsyncOper::OnInit(ACM *pacm) {
    uint8_t rcode;
    rcode = pacm->SetControlLineState(3);
    if (rcode) {
        ErrorMessage<uint8_t>(PSTR("SetControlLineState"), rcode);
        return rcode;  }
    LINE_CODING lc;
    lc.dwDTERate = 115200;
    lc.bCharFormat = 0;
    lc.bParityType = 0;
    lc.bDataBits = 8;
    rcode = pacm->SetLineCoding(&lc);
    if (rcode)ErrorMessage<uint8_t>(PSTR("SetLineCoding"), rcode);
    return rcode;
}
USB           Usb;
ACMAsyncOper  AsyncOper;
ACM           Acm(&Usb, &AsyncOper);

void setup(){
  Serial.begin( 115200 );
  if (Usb.Init() == -1)
      Serial.println("OSCOKIRQ failed to assert");     
  delay( 200 );
  pinMode(pirPin,INPUT);
  //pinMode(13,OUTPUT);
  digitalWrite(pirPin,HIGH);  //instead 10k pullup but not enough
}
uint8_t sndstr(char *s) {
uint8_t i,j,k;
for(i=0;s[i]!=0;i++) {
  k=s[i];
  j=Acm.SndData(1,&k); //could do all at once?
  if(j) return j;
}
i=13;
Acm.SndData(1,&i);
return j;
}
uint8_t sndnocr(char *s) {
uint8_t i,j,k;
for(i=0;s[i]!=0;i++) {
  k=s[i];
  j=Acm.SndData(1,&k);
  if(j) return j;
}
i=13;
//Acm.SndData(1,&i); only difference from above
return j;
}
uint8_t sndmsg(char *n, char *msg) {
uint8_t i;
sndnocr("AT+CMGW=");  //CMGW or S when online, doesn't work ERROR
i=34; Acm.SndData(1,&i);  //"
sndnocr(n);
i=34; Acm.SndData(1,&i);
i=13; Acm.SndData(1,&i);
sndnocr(msg);
i=26; Acm.SndData(1,&i);
DELREC1 DELREC
//t="AT+CMSS=nn"; already init above/below
t[8]=buf[9];
t[9]=buf[10];
if(t[9]==13) t[9]=0; //single digit
//Serial.print  (int(t[8]));
//Serial.println(int(t[9]));
//Serial.println(t);
sndstr(t); //comment this for testing
//no return yet
}
void waitsvc(int d) {
svc=0;
for(j=0;svc==0;j++) { //j<100 should be svc==0 for/while no service signal ==1 skips
//buf[11]=48;
//NDREC  //flush from PHLIGHT below
sndstr("AT+CIND?");  //only works 1x w/svc OK now!
DELREC
svc=buf[11]-48; rss=buf[17]-48;
//Serial.print(svc); Serial.print(rss);
//if(svc==1) {PHLIGHT} //does not need immediate DELREC like above ATE0?
delay(d*1000);
} //for svc==0
NDREC
}
void waitSMS() {
rcvd=6;
while(rcvd==6) {  //CRLFokCRLF
  sndstr("AT+CMGL=\"REC UNREAD\"");  //READ to test UNREAD when ready
  DELREC1 PRCV PBUF P32
  while(rcvd==0) {DELREC1 PRCV PBUF P32}
  }
//if(rcvd!=6)
sndstr("/r"); //stops much data
for(i=0;i<rcvd;i++)  s.setCharAt(i,buf[i]);  //starts with 0!
for(i=rcvd;i<64;i++) s.setCharAt(i,0);  //clear the rest
//Serial.println(s);   
//if(s.indexOf("curb")>0) Serial.println("Found");  //-1 is not found
cmgr[8]=mnum[0]=buf[9];
cmgr[9]=mnum[1]=buf[10]; //could be 13 CR single digit fix later
mnum[2]=0;
P13
for(k=0;k<12;k++) {DELREC1 PRCV P32}
P13
}
void loop()
{   Usb.Task(); 
if( Usb.getUsbTaskState() == USB_STATE_RUNNING){ 
  if(first==1) {
     first=0;
//IF0 allows to keep formatting/colors in code
//IF0
     sndstr("AT+MODE=2");
     sndstr("ATE0");
     DELREC //needed or will crash! check for this error everywhere
//}
//delay(5000);
//PHLIGHT DELREC  //to make sure its running in field
//waitsvc(15);
//waitSMS();
//if(s.indexOf("CMGLnot")>0)  //if pw not found crashes without sending, +CMGL always
//k is long int
pirVal=HIGH;
for(j=0,k=1;pirVal==HIGH;k++){ //no movement
  pirVal=digitalRead(pirPin);
  if(pirVal==LOW){j++;delay(2000);}
  if(j<4)pirVal=HIGH;
  if(k%(20*60*5)==0)j=0; //every 5 mins overflow 30min? No k is long now
  if(k%(20*60*30)==0)break; //test every 30 min
  delay(50); //1/20 sec
  }
CALLHU("ATD9785443562")
DELREC;
DELREC; //req?
//sndmsg("2626969619","Hello Beaver"); //still in buffer
for(j=0;j<5*60;j++)delay(1000); //5min
resetFunc();  //call reset does not initialize registers

IF0
    sndstr("AT+CBC");
    DELREC Serial.println(buf[10]); //'1'=49?=100% battery or 7 or 9 is 90%
    sndstr("AT+CCLK?");
    DELREC
    hr=(int(buf[19])-48)*10+buf[20]-48;
    mi=(int(buf[22])-48)*10+buf[23]-48;
    se=(int(buf[25])-48)*10+buf[26]-48;
    Serial.print(hr); Serial.print(':');
    Serial.print(mi); Serial.print(':');
    Serial.print(se);
    while(rcvd!=8) {DELREC1 PRCV PBUF}
    //probably RING?
    //cmgr[8]=mnum[0]; cmgr[9]=mnum[1]; already done in waitSMS()
    //sndstr(cmgr); //gets timestamp too for specific SMS, no other purpose
} //IF0
} //first=1
//modified from ACM terminal:
  if(Serial.available()) {
    uint8_t data= Serial.read();
    rcode = Acm.SndData(1, &data);
    if (rcode)ErrorMessage<uint8_t>(PSTR("SndData"), rcode); }
  delay(50);
  rcvd=64;
  rcode = Acm.RcvData(&rcvd, buf);
  if(rcode && rcode != hrNAK) ErrorMessage<uint8_t>(PSTR("Ret"), rcode);
  if( rcvd ) { //more than zero bytes received
  //modified by me:
  Serial.println();
  Serial.print("Size: ");
  Serial.println(rcvd);
    for(uint16_t i=0; i < rcvd; i++ ) {
     j=buf[i];
     if(j==13) {
        Serial.write(13);
        //Serial.write(10);
        //i+=3;  //for single line results
        }
     if((j!=13)&&(j!=10)) {
        Serial.print(buf[i]); //printing on the screen
        Serial.print(" "); Serial.print(i); Serial.print(" "); Serial.println(j);
        }
     }
  }
  delay(10);           
  } //USB_STATE_RUNNING top of loop()
} //loop()
927  Using Arduino / Project Guidance / Re: Arduino Longterm on: April 04, 2012, 11:29:51 am
Good point Peter!  The solution is simple as you illustrated.  Most people will not see the difference in your 2 examples.
928  Using Arduino / Motors, Mechanics, and Power / Re: New Libray for L298N on: April 03, 2012, 06:52:33 pm
Did you try the 2 lines I suggested above?
For every hour:

degrpm(1,3000,1000);

If you are using 28byj motor then you are using the wrong code of mine.  Use stepper2.pde instead.  Did you see the other thread on this forum? 

I don't know what to do about vibration when you are moving at the speed of a clock second hand.  You can use the analog pins as digital in or out. 

Sorry I'm not following the intentions of your code.  That's not how I meant it to be used.  That's great if it works for you!
929  Using Arduino / Motors, Mechanics, and Power / Re: New Libray for L298N on: April 02, 2012, 08:40:21 pm
I don't understand what you did in the code.  But I can see some misunderstandings.  All the degrpm functions have deg then rpm as parameters.  So you put 10RPM where deg goes in loop.  I didn't mean for you to call mydelay in loop.  You can use it for that instead, but I meant it to be called where I put it in the function.  Here's how I would do it.  I have verified this does not drift on my motors.

degrpm1A(1,9000,1000);
Serial.println("At 0,9,18,270");

I'd be happy to send you my easing library, but it's not so "easy" as this one is.  I'm concerned you might need some help with it.

Yes! I will make it into a class.  It's under development.

I'm not really microstepping.  In this code I am half stepping.  The vibration depends on the speed you choose.  For big motors my PWM library helps with this.  But the circuit is more complex.  What motor are you using?  What speed is vibrating?  I know some tricks.

For x,y line drawing all you'd have to do is glue a laser on a 28BYJ motor shaft (x).  Then glue that to another perpendicular (y).  Each step is movement in the x or y direction.  I haven't gotten to this yet.  Maybe it is not fast enough to be persistent.
930  Using Arduino / Programming Questions / Re: Arduino won't go to sleep! on: April 02, 2012, 07:03:22 pm
I agree.  I will use my method when I need 1-2ms accuracy for each sample.  You are correct, this error accumulates.  If I need more accuracy I will be forced to use the method you were discussing. 
Pages: 1 ... 60 61 [62] 63 64 ... 93