Go Down

Topic: Arduino Duemilanove code problem (Read 583 times) previous topic - next topic

elitedezigns

I'm a noob, and I did read about 35 pages from searching already, so be easy on me.  I just got this unit, and I am able to upload all the example codes (blink and blink w/delay) and they work fine.  But this one doesn't work.  I have an automated cat litter box and trying to reset the counter that it has on the cleaning cartridges. I got the code from instructables site.  When I verify, it shows me problems with "main", "setup" and "loop".  Several people claimed it works for them, but when I try to upload anyow, it will say "uploading sketch" for over an hour.  Here is the sketch if anyone can please help me out:

#include <Wire.h>#define CG (B1010000)boolean resetSuccess = false;int isReset = 13;int byteArray []= {01, 01, 01, 60, 60, 60, 60, 60, 60, 8, 8, 8, 33, 33, 33, 255};void setup(){ pinMode(isReset, OUTPUT); digitalWrite(isReset, LOW); Wire.begin(); // join i2c bus (address optional for master)}void loop(){ if (resetSuccess) { delay (2000); // our work is done - pause for a while resetSuccess = false; } else { resetCartridge(); resetSuccess = verifyCartridge(); digitalWrite(isReset, resetSuccess); }}void resetCartridge(){ for (int i=3; i < sizeof(byteArray)/2; i++) { Wire.beginTransmission(CG); Wire.send(i); Wire.send(byteArray); Wire.endTransmission(); delay(4); }}void movePointerTo(int deviceAddr, int memoryAddr){ Wire.beginTransmission(deviceAddr); Wire.send(memoryAddr); Wire.endTransmission();}boolean verifyCartridge(){ boolean success = true; movePointerTo(CG, 3); Wire.requestFrom(CG, 3); while (Wire.available()) { if (Wire.receive() == 60 && success == true) { // looking good so far } else { success = false; } } return success;}

retrolefty

#1
Jul 25, 2010, 03:16 am Last Edit: Jul 25, 2010, 03:18 am by retrolefty Reason: 1
Quote
Here is the sketch if anyone can please help me out:


Do you actually expect that anyone can read that? C/C++ is already hard enough to read without leaving out the LF/CR for each line.  ;)

elitedezigns

Sorry, I'm brand new at this.  Born like 3 hours ago, lol.  I just copied it from an instructable, and was thinking the same thing.  What is C/C++ and LF/CR?  I can't wait until I'm able to do cool stuff with this, but I know it takes time.

retrolefty

Quote
What is C/C++ and LF/CR?  I can't wait until I'm able to do cool stuff with this, but I know it takes time.


C/C++ is the programming language used to write your programs for the Arduino board.

LF/CR = carriage return/line feed characters, sometimes called NL= new line. It's the characters used to start a new line of text, delete them and you get like what you posted.  ;)

Lefty

elitedezigns

#4
Jul 25, 2010, 04:06 am Last Edit: Jul 25, 2010, 05:00 am by elitedezigns Reason: 1
Ok, thanks.  I just found another version of that code, and it works!  It looks so much shorter, but works.  Thanks again, I'm sure I'll need much more before I'll actually be able to write these things myself.  For search, or if anyone else wants the reset code for the litter box cartridges that actually works, here goes:
Code: [Select]
#include <Wire.h>

#define CG (B1010000)
boolean resetSuccess = false;
int isReset = 13;

int byteArray []= {01, 01, 01, 60, 60, 60, 60, 60, 60, 8, 8, 8, 33, 33, 33, 255};

void setup()
{
 pinMode(isReset, OUTPUT);
 digitalWrite(isReset, LOW);

 Wire.begin();        // join i2c bus (address optional for master)
 digitalWrite(isReset, HIGH);   // sets the LED on
 delay(5000);                  // waits for 5 seconds

 digitalWrite(isReset, LOW);    // sets the LED off
 delay(1000);                  // waits for a second
}

void loop()
{
 if (resetSuccess)
 {
   delay (2000);      // our work is done - pause for a while
   resetSuccess = false;
   digitalWrite(isReset, LOW);            // switch off the light
 } else {
     // Try a reset and test if it worked
   resetCartridge();
   resetSuccess = verifyCartridge();

     // switch the light on if we reset the cart OK
   digitalWrite(isReset, resetSuccess);
 }
}

void resetCartridge()
{
 for (int i=3; i < sizeof(byteArray)/2; i++)
 {
   Wire.beginTransmission(CG);
   Wire.send(i);
   Wire.send(byteArray[i]);
   Wire.endTransmission();
   delay(4);
 }
}

void movePointerTo(int deviceAddr, int memoryAddr)
{
 Wire.beginTransmission(deviceAddr);
 Wire.send(memoryAddr);
 Wire.endTransmission();
}

boolean verifyCartridge()
{
 boolean success = false;
 movePointerTo(CG, 3);
 Wire.requestFrom(CG, 3);
 while (Wire.available())
 {
   if (Wire.receive() == byteArray[3] )
   {
     success = true;
     // looking good so far
   } else {
     success = false;
   }
 }
 return success;
}

retrolefty

Cool. Next time you post code use the # button at the top of the editor and then paste your code into the code box created, it makes it much easier for all to read.

Good luck with your projects, the Arduino platform plain rocks.  ;)

Lefty

elitedezigns


Go Up