Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Sample Arduino code for a Bartendro dispenser
#include "packet.h"
#include <stdint.h>
#define SYNC_PIN 4
#define BUTTON_PIN 3
void setup()
{
Serial.begin(9600);
delay(100);
Serial.write('?'); //write serial to get the dispenser to kick back it's address and begin comms mode
Serial.write(0xFF);
Serial.flush();
delay(100);
// syncro pin for LEDS
//pinMode(SYNC_PIN, OUTPUT);
// button pin for dispense
pinMode(BUTTON_PIN, INPUT);
}
void loop()
{
// Building out the packet data
packet_t p;
memset(&p, 0, sizeof(packet_t));
p.dest = DEST_BROADCAST;
p.type = PACKET_TIME_DISPENSE;
p.p.uint32 = 200;
int buttonState = 0;
buttonState = digitalRead(BUTTON_PIN);
if (buttonState && (debounce(BUTTON_PIN) == true)){
send_packet(&p);
delay(100);
}
//digitalWrite(SYNC_PIN, HIGH);
//delay(2);
//digitalWrite(SYNC_PIN, LOW);
//delay(2);
}
const int debounceDelay = 20; // milliseconds to wait until stable
// debounce returns the stable switch state
boolean debounce(int pin)
{
boolean state;
boolean previousState;
previousState = digitalRead(pin); // store switch state
for(int counter=0; counter < debounceDelay; counter++)
{
delay(1); // wait for 1 millisecond
state = digitalRead(pin); // read the pin
if( state != previousState)
{
counter = 0; // reset the counter if the state changes
previousState = state; // and save the current state
}
}
return state;
}
uint8_t send_packet(packet_t *p){
// Setting up for delivery
uint16_t crc = 0;
uint8_t i, *ch, ret, ack, packed_size;
uint8_t packed[RAW_PACKET_SIZE + 2]; // +2 for the header
// CRC for packet
crc = crc16_update(crc, p->dest);
crc = crc16_update(crc, p->type);
crc = crc16_update(crc, p->p.uint8[0]);
crc = crc16_update(crc, p->p.uint8[1]);
crc = crc16_update(crc, p->p.uint8[2]);
p->crc = crc16_update(crc, p->p.uint8[3]);
pack_7bit(sizeof(packet_t), (uint8_t *)p, &packed_size, &packed[2]);
packed[0] = 0xFF;
packed[1] = 0xFF;
// Send the packet data
Serial.write(packed, sizeof(packed));
}
uint16_t crc16_update(uint16_t crc, uint8_t a)
{
int i;
crc ^= a;
for (i = 0; i < 8; ++i)
{
if (crc & 1)
crc = (crc >> 1) ^ 0xA001;
else
crc = (crc >> 1);
}
return crc;
}
const char *b2b(int x)
{
static char b[9];
b[0] = '\0';
int z;
for (z = 128; z > 0; z >>= 1)
{
strcat(b, ((x & z) == z) ? "1" : "0");
}
return b;
}
void pack_7bit(uint8_t in_count, uint8_t *in, uint8_t *out_count, uint8_t *out)
{
uint16_t buffer = 0;
uint8_t bitcount = 0;
*out_count = 0;
for(;;)
{
if (bitcount < 7)
{
buffer <<= 8;
buffer |= *in++;
in_count--;
bitcount += 8;
}
*out = (buffer >> (bitcount - 7));
out++;
(*out_count)++;
buffer &= (1 << (bitcount - 7)) - 1;
bitcount -= 7;
if (in_count == 0)
break;
}
*out = buffer << (7 - bitcount);
(*out_count)++;
}

Hi Brian,

I just get my Bartendro Pump and I'm trying to make it work with your code. Idk why it is not working. I do have the 'packet.h' so I know this is not the problem. Did you modified the program? or it should be working fine?

tjado16 commented Jun 19, 2016

Hi Brian,

Is the "packed" a typo and is it meant to be "packet"? Thank you so much for the code. It has helped. Now the only issue is that the output from the Bartendro is not stable at at times it does not work. Does this also apply for the PACKET_TICK_DISPENSE?
Kind regards.

Looks interesting... Could be used with alternate serial ports than the USB-serial for more direct control, I hope.
I;ll be hacking up a teensy for this.
btw - Tajado, 'packed' is a reference to the data being packed into bytes and removing padding that sometimes happen with data structures. C will try to word align data and insert extra (unused) bytes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment