Change Pin # for this program

So to start off I literally have never used an arduino before and as such am obviously very new in general to how to do anything with it. Basically what I'm trying to do is reset a 3d da vinchi printer cartridge so that I can print more filament, but no walkthroughs use my particular arduino.

So basically what it boils down to is I need to figure out what to change in the program listed below in order for it to recognize the pin names on my arduino. I'm using a makey makey arduino which uses a leonardo arduino and is laid out as the picture shows.

Additionally I believe the portion of the code that refers to the pin # I need changed is below.

static void set_bus(boolean state) {
PORTD=(PORTD&0x7f)|(!!state)<<7;

Here is part of the full program. The rest of it is in the post below. Any help would be greatly appreciated.

/*
Da Vinci EEPROM update Copyright (C) 2014 by Oliver Fueckert <oliver@voltivo.com>
Increment Serial code - contributed by Matt
UNI/O Library Copyright (C) 2011 by Stephen Early <steve@greenend.org.uk>
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.  */

#ifndef _NANODEUNIO_LIB_H
#define _NANODEUNIO_LIB_H

#if ARDUINO >= 100
  #include <Arduino.h> // Arduino 1.0
#else
  #include <WProgram.h> // Arduino 0022
#endif

#define NANODE_MAC_DEVICE 0xa0
#define NANODE_MAC_ADDRESS 0xfa

#define CODE 0x00 //1 Byte
#define MATERIAL 0x01 //1 Byte
#define COLOR 0x02  //2 Bytes
#define DATE 0x05  //4 Bytes
#define TOTALLEN 0x08 //4 Bytes
#define NEWLEN 0x0C //4 Bytes
#define HEADTEMP 0x10 //2 Bytes
#define BEDTEMP 0x12  //2Bytes
#define MLOC 0x14 //2 Bytes
#define DLOC 0x16 //2 Bytes
#define SN 0x18   //12 Bytes
#define CRC 0x24  //2 Bytes
#define LEN2 0x34 //4 Bytes

void IncrementSerial(unsigned char * cArray, long lAddress, long lSize)
{
  unsigned char szTempBuffer[20] = {0};
  memcpy(szTempBuffer,&cArray[lAddress],lSize);
  long lSerial = atol((char *)szTempBuffer);
  lSerial++;
  sprintf((char *)szTempBuffer,"%04d",lSerial);
  memcpy(&cArray[lAddress],szTempBuffer,lSize);
}

class NanodeUNIO {
 private:
  byte addr;
 public:
  NanodeUNIO(byte address);

  boolean read(byte *buffer,word address,word length);
  boolean start_write(const byte *buffer,word address,word length);
  boolean enable_write(void);
  boolean disable_write(void);
  boolean read_status(byte *status);
  boolean write_status(byte status);
  boolean await_write_complete(void);
  boolean simple_write(const byte *buffer,word address,word length);
};

#endif /* _NANODEUNIO_LIB_H */

#define UNIO_STARTHEADER 0x55
#define UNIO_READ        0x03
#define UNIO_CRRD        0x06
#define UNIO_WRITE       0x6c
#define UNIO_WREN        0x96
#define UNIO_WRDI        0x91
#define UNIO_RDSR        0x05
#define UNIO_WRSR        0x6e
#define UNIO_ERAL        0x6d
#define UNIO_SETAL       0x67

#define UNIO_TSTBY 600
#define UNIO_TSS    10
#define UNIO_THDR    5
#define UNIO_QUARTER_BIT 10
#define UNIO_FUDGE_FACTOR 5
#define UNIO_OUTPUT() do { DDRD |= 0x80; } while (0)
#define UNIO_INPUT() do { DDRD &= 0x7f; } while (0)

static void set_bus(boolean state) {
  PORTD=(PORTD&0x7f)|(!!state)<<7;
}

static boolean read_bus(void) {
  return !!(PIND&0x80);
}
static void unio_inter_command_gap(void) {
  set_bus(1);
  delayMicroseconds(UNIO_TSS+UNIO_FUDGE_FACTOR);
}

static void unio_standby_pulse(void) {
  set_bus(0);
  UNIO_OUTPUT();
  delayMicroseconds(UNIO_TSS+UNIO_FUDGE_FACTOR);
  set_bus(1);
  delayMicroseconds(UNIO_TSTBY+UNIO_FUDGE_FACTOR);
}

static volatile boolean rwbit(boolean w) {
  boolean a,b;
  set_bus(!w);
  delayMicroseconds(UNIO_QUARTER_BIT);
  a=read_bus();
  delayMicroseconds(UNIO_QUARTER_BIT);
  set_bus(w);
  delayMicroseconds(UNIO_QUARTER_BIT);
  b=read_bus();
  delayMicroseconds(UNIO_QUARTER_BIT);
  return b&&!a;
}

static boolean read_bit(void) {
  boolean b;
  UNIO_INPUT();
  b=rwbit(1);
  UNIO_OUTPUT();
  return b;
}

static boolean send_byte(byte b, boolean mak) {
  for (int i=0; i<8; i++) {
    rwbit(b&0x80);
    b<<=1;
  }
  rwbit(mak);
  return read_bit();
}

static boolean read_byte(byte *b, boolean mak) {
  byte data=0;
  UNIO_INPUT();
  for (int i=0; i<8; i++) {
    data = (data << 1) | rwbit(1);
  }
  UNIO_OUTPUT();
  *b=data;
  rwbit(mak);
  return read_bit();
}

static boolean unio_send(const byte *data,word length,boolean end) {
  for (word i=0; i<length; i++) {
    if (!send_byte(data[i],!(((i+1)==length) && end))) return false;
  }
  return true;
}

static boolean unio_read(byte *data,word length)  {
  for (word i=0; i<length; i++) {
    if (!read_byte(data+i,!((i+1)==length))) return false;
  }
  return true;
}

static void unio_start_header(void) {
  set_bus(0);
  delayMicroseconds(UNIO_THDR+UNIO_FUDGE_FACTOR);
  send_byte(UNIO_STARTHEADER,true);
}

NanodeUNIO::NanodeUNIO(byte address) {
  addr=address;
}

#define fail() do { sei(); return false; } while (0)

boolean NanodeUNIO::read(byte *buffer,word address,word length) {
  byte cmd[4];
  cmd[0]=addr;
  cmd[1]=UNIO_READ;
  cmd[2]=(byte)(address>>8);
  cmd[3]=(byte)(address&0xff);
  unio_standby_pulse();
  cli();
  unio_start_header();
  if (!unio_send(cmd,4,false)) fail();
  if (!unio_read(buffer,length)) fail();
  sei();
  return true;
}

boolean NanodeUNIO::start_write(const byte *buffer,word address,word length) {
  byte cmd[4];
  if (((address&0x0f)+length)>16) return false; // would cross page boundary
  cmd[0]=addr;
  cmd[1]=UNIO_WRITE;
  cmd[2]=(byte)(address>>8);
  cmd[3]=(byte)(address&0xff);
  unio_standby_pulse();
  cli();
  unio_start_header();
  if (!unio_send(cmd,4,false)) fail();
  if (!unio_send(buffer,length,true)) fail();
  sei();
  return true;
}

Apparently the code was too long. Here is the rest of it.

\boolean NanodeUNIO::enable_write(void) {
  byte cmd[2];
  cmd[0]=addr;
  cmd[1]=UNIO_WREN;
  unio_standby_pulse();
  cli();
  unio_start_header();
  if (!unio_send(cmd,2,true)) fail();
  sei();
  return true;
}

boolean NanodeUNIO::disable_write(void) {
  byte cmd[2];
  cmd[0]=addr;
  cmd[1]=UNIO_WRDI;
  unio_standby_pulse();
  cli();
  unio_start_header();
  if (!unio_send(cmd,2,true)) fail();
  sei();
  return true;
}

boolean NanodeUNIO::read_status(byte *status) {
  byte cmd[2];
  cmd[0]=addr;
  cmd[1]=UNIO_RDSR;
  unio_standby_pulse();
  cli();
  unio_start_header();
  if (!unio_send(cmd,2,false)) fail();
  if (!unio_read(status,1)) fail();
  sei();
  return true;
}

boolean NanodeUNIO::write_status(byte status) {
  byte cmd[3];
  cmd[0]=addr;
  cmd[1]=UNIO_WRSR;
  cmd[2]=status;
  unio_standby_pulse();
  cli();
  unio_start_header();
  if (!unio_send(cmd,3,true)) fail();
  sei();
  return true;
}

boolean NanodeUNIO::await_write_complete(void) {
  byte cmd[2];
  byte status;
  cmd[0]=addr;
  cmd[1]=UNIO_RDSR;
  unio_standby_pulse();
  do {
    unio_inter_command_gap();
    cli();
    unio_start_header();
    if (!unio_send(cmd,2,false)) fail();
    if (!unio_read(&status,1)) fail();
    sei();
  } while (status&0x01);
  return true;
}

boolean NanodeUNIO::simple_write(const byte *buffer,word address,word length) {
  word wlen;
  while (length>0) {
    wlen=length;
    if (((address&0x0f)+wlen)>16) {
      wlen=16-(address&0x0f);
    }
    if (!enable_write()) return false;
    if (!start_write(buffer,address,wlen)) return false;
    if (!await_write_complete()) return false;
    buffer+=wlen;
    address+=wlen;
    length-=wlen;
  }
  return true;
}

static void status(boolean r)
{
  if (r) Serial.println("(success)");
  else Serial.println("(failure)");
}

static void dump_eeprom(word address,word length)
{
  byte buf[128];
  char lbuf[80];
  char *x;
  int i,j;

  NanodeUNIO unio(NANODE_MAC_DEVICE);
  
  memset(buf,0,128);
  status(unio.read(buf,address,length));
  
  for (i=0; i<128; i+=16) {
    x=lbuf;
    sprintf(x,"%02X: ",i);
    x+=4;
    for (j=0; j<16; j++) {
      sprintf(x,"%02X",buf[i+j]);
      x+=2;
    }
    *x=32;
    x+=1;
    for (j=0; j<16; j++) {
      if (buf[i+j]>=32 && buf[i+j]<127) *x=buf[i+j];
      else *x=46;
      x++;
    }
    *x=0;
    Serial.println(lbuf);
  }
}

int led = 13;

/*
These are the values to be written to the EEPROM
Make sure only one is uncommented.
By default its set for the starter ABS cartdridge with 120m of filament 
Verified with firmware 1.1.I
*/

// Value to write to the EEPROM for remaining filament lenght
// Default Starter Cartdridge is 120m
char x[] = {0xc0,0xd4,0x01,0x00}; //120m
//char x[x] = {0x80,0xa9,0x03,0x00}; //240m
//char x[x] = {0x80,0x1a,0x06,0x00}; //400m

// extruder temp, default is 210 C for ABS
char et[] = {0xd2,0x00}; // 210 C 
//char et[] = {0xe6,0x00}; // 230 C
//char et[] = {0xf5,0x00}; // 245 C
//char et[] = {0xfa,0x00}; // 250 C

// bed temp 90 degrees, default ABS
char bt[] = {0x5a,0x00};

byte sr;
NanodeUNIO unio(NANODE_MAC_DEVICE);
  
void setup() {
  pinMode(13, OUTPUT);
  Serial.begin(115200);
}

void loop() {
  
  do {
    digitalWrite(led, LOW); 
    Serial.println("Testing connection to Da Vinci EEPROM CHIP\n");    
    delay(100);
    digitalWrite(led, HIGH);
  } while(!unio.read_status(&sr));
  
  Serial.println("Da Vinci EEPROM found...");
  Serial.println("Reading the Davinci EEPROM Contents...");
  dump_eeprom(0,128);
  //dump_eeprom(116,4);
  
  //Read the serial number - added by Matt
  byte buf[20];
  memset(buf,0,20);
  status(unio.read(buf,SN,12));
  //Increment the serial number
  IncrementSerial(&buf[0], 0, 12);  
 
  Serial.println("Updating EEPROM...");
  status(unio.simple_write((const byte *)x,TOTALLEN,4));
  status(unio.simple_write((const byte *)x,NEWLEN,4));
  status(unio.simple_write((const byte *)et,HEADTEMP,2)); // extruder temp
  status(unio.simple_write((const byte *)bt,BEDTEMP,2)); // bed temp
  //Write the serial number
  status(unio.simple_write((const byte *)buf,SN,12)); //Serial Number
  status(unio.simple_write((const byte *)x,LEN2,4));
  // same block from offset 0 is offset 64 bytes
  status(unio.simple_write((const byte *)x,64 + TOTALLEN,4));
  status(unio.simple_write((const byte *)x,64 + NEWLEN,4));
  status(unio.simple_write((const byte *)et,64 + HEADTEMP,2)); // extruder temp
  status(unio.simple_write((const byte *)bt,64 + BEDTEMP,2)); // bed temp
   //Write the serial number
  status(unio.simple_write((const byte *)buf,64 + SN,12)); //Serial Number
  status(unio.simple_write((const byte *)x,64 + LEN2,4));

  Serial.println("Dumping Content after modification...");
  dump_eeprom(0,128);
 
  digitalWrite(led, HIGH);   // turn the LED on
  delay(10000);               // wait for two seconds 
}

For a long program you should add the .ino file as an attachment. Your two pieces do not join together properly.

In any case, I think it would take a very long time to figure out what the program does based on the small amount of info you have provided.

You will probably make progress faster by asking on a 3D printing Forum such as the RepRap forum. This Forum is really for helping you write your own programs.

...R

Robin2:
For a long program you should add the .ino file as an attachment. Your two pieces do not join together properly. I don't know why they don't line up as I literally just cut the bottom half and pasted. Either way, here is the link to the .ino davinci_filament_reset_arduino/xyz_dv_eprom.ino at master · voltivo/davinci_filament_reset_arduino · GitHub I will post it in the original post as well.

In any case, I think it would take a very long time to figure out what the program does based on the small amount of info you have provided. I did not write the program and was following a youtube video which linked the program. Here is the video if you're curious. https://www.youtube.com/watch?v=MLnXEyarf4I

So here is a basic background on the program. The 3D printer brand Da Vinchi have a printer cartridge that when empty will not let you print anymore until you replace it with a Da Vinchi brand filament. This works similar to how many standard ink printers work. The program is a workaround to replace the code on the chip on the printer cartridge with another one saying that there is still filament left in the cartridge therefor allowing you to indefinitely use other brand filament which is much cheaper as well as much more varied.

You will probably make progress faster by asking on a 3D printing Forum such as the RepRap forum. This Forum is really for helping you write your own programs. This entire forum or just this area? I would gladly repost this in another area if it would fit better somewhere else. However, I figure for an arduino question this site does seem to have the experts. I am asking the same question over on a forum dedicated to Da Vinchi printers, but the problem is they don't have the arduino experience to answer my question. They merely know how to use code that someone else wrote, which isn't any more than I know.

...R

Thank you for your response and let me know if this helps you better understand my problem. Additionally in case I wasn't originally clear, the program already works. It just won't work with the arduino that I have and I need to know what to change so that it recognizes the pins (specifically when it's using the 7 pin) on my arduino as the program was designed for a uno and I have a makey makey using a leonardo arduino.

Your Reply #3 does not advance matters. You have not posted the full program and this entire Forum is not a place for specialized 3D printing questions. As I said earlier this is a Forum for helping people write their own programs.

From the way you have presented the problem I would need to learn all about your Da Vinci thing in order to help you - and I have no personal interest in it and I am too lazy to learn it just to explain it someone else. Sorry if that sounds blunt but it is not intended to be unfriendly - just honest.

If you generally understand the program and can reduce your query to a specific piece that you have a problem with then I may be able to help.

...R

I'm confused as I already did post a link to the entire program. Here it is again if you missed it. davinci_filament_reset_arduino/xyz_dv_eprom.ino at master · voltivo/davinci_filament_reset_arduino · GitHub

Regardless the problem is not with the program. I am not asking how to fix the program as it does work for many people, just not with my arduino. I cannot hook up a wire to the proper pin as the program is looking for a pin reference that does not exist on my arduino. As I understand it the program is looking for a pin 7 and my arduino does not have a pin 7. I merely need to know how to modify the program to change the name of the pin it is looking for to one that is on my leonardo auduino that it recognizes and I can hook up a wire to that pin.

The code in reference are line 99 and 100. There may be more to it, but I know for sure that is part of it. It reads as follows.

static void set_bus(boolean state) {
PORTD=(PORTD&0x7f)|(!!state)<<7;

I would imagine for someone who understands the program language to know what to change would be as simple as delegating drive C vs drive D on your computer.

Bryan5255:
As I understand it the program is looking for a pin 7 and my arduino does not have a pin 7. I merely need to know how to modify the program to change the name of the pin it is looking for to one that is on my leonardo auduino that it recognizes and I can hook up a wire to that pin.

The code in reference are line 99 and 100. There may be more to it, but I know for sure that is part of it. It reads as follows.

static void set_bus(boolean state) {
PORTD=(PORTD&0x7f)|(!!state)<<7;

That code is not referring to pin 7. It is much more complex. It is setting the 8 pins on PORTD to some value that takes account of the value that is passed to the function.

I'm guessing (an no more than that) that it is setting bit 7 either HIGH or LOW depending on the value that is passed to the function. But I have no idea which physical pin is connected to bit 7. You will need to find the pin mapping diagram for that. And you will also need the pin mapping diagram for whatever board you are using so you can figure out which Port and which bit within the Port that you need for your purpose.

Hopefully someone else will come along who may already have experience of your problem.

...R

You may be better off buying a cheap Uno clone rather than trying to convert that code to suit the Leonardo. It is a different processor.

dannable:
You may be better off buying a cheap Uno clone rather than trying to convert that code to suit the Leonardo. It is a different processor.

That sounds very sensible, I wish I had thought of it.

...R

dannable:
You may be better off buying a cheap Uno clone rather than trying to convert that code to suit the Leonardo. It is a different processor.

I was hoping on saving myself some time by using the one I have and be able to start on the project sooner and I honestly didn't expect changing the program to be that difficult, but yea, it's looking like that's what I'm going to have to do.