Major design mistake in Markerbot Rev3

58 views
Skip to first unread message

Romain Bazile

unread,
Oct 16, 2011, 7:02:16 AM10/16/11
to makerb...@googlegroups.com
Hey all,

I've been spending some time trying to get my Makerbot Watch to work
(with some success... I'll post the code soon).
But I've been encountering a little hiccup. The switch wouldn't work in
any way. And in fact, there is a reason... I completely forgot to add
pullup resistors...
Thus, the switches are not working...

I'm working to see if there can be a software way, but I'm quite
pessimistic about this fact...

Any way, the ds1337 is working really fine! I'm quite happy about this!
I'm now trying to reduce the power consumption via sleep and a watchdog
to wake up the microcontroller to see if there's anything to do.

I'll also try to set up soon an alarm system!


Cheers,


Romain

Romain Bazile

unread,
Oct 16, 2011, 7:20:24 AM10/16/11
to makerb...@googlegroups.com
Again,

Little correction: I forgot it, but the ATMEGA does have internal pullup
resistors! And I even forgot that I knew it, because I deleted the
pullup resistors as I wanted to use the internal ones!

So it works well!

I'm going to do some cleanup in the code, and then, I'll post it here!


Cheers all!


Romain

Ante Vukorepa

unread,
Oct 16, 2011, 7:27:27 AM10/16/11
to makerb...@googlegroups.com
You don't need pullups.
Atmegas have internal ones.

Just set the relevant pins to input and digitalWrite 1 to them.
They'll detect button presses as 0 and report 1 otherwise.

Ante Vukorepa
----
Sent from my iPad

Ante Vukorepa

unread,
Oct 16, 2011, 7:33:39 AM10/16/11
to makerb...@googlegroups.com
Hahah, i went through the exact same brainfart after assembling the Chronoduino for the first time. "Oh, crap, i forgot the pullups! Wait... internal pullups... Oh, right, that's why there are no pullup resistors on the board."

I did have an additional double take shortly after - i forgot the values reported by the switch pins are inverted and wondered why i'm getting unexpected results from (switch1 || switch2) :)

Ante Vukorepa
----
Sent from my iPad

Romain Bazile

unread,
Oct 16, 2011, 7:32:34 AM10/16/11
to makerb...@googlegroups.com
Thanks anyway for the help!

And now...

The code!

You will need RTCLib from here: https://github.com/adafruit/RTClib

Copy/pasting the code should work quite well!


#include <Wire.h>
#include "RTClib.h"
#include <avr/sleep.h>
#include <avr/wdt.h>
#ifndef cbi
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
#endif
#ifndef sbi
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
#endif
volatile boolean f_wdt=1;
#define DS1307_I2C_ADDRESS 0x68  // This is the I2C address
int rows[]={8,9,14,15,16,17};
int cols[]={3,4,5,6,7};
int S1 = 10;
int S2 = 11;
int BUZZ = 2;
struct TIME {
  int row;
  int col;
};
TIME hours[] = {{9,3},{14,3},{15,3},{16,3},{17,3},{8,4},{9,4},{14,4},{15,4},{16,4},{17,4},{8,3}};
TIME minutes[] = {{8,5},{9,5},{14,5},{15,5},{16,5},{17,5},{8,6},{9,6},{14,6},{15,6},{16,6},{17,6}};
TIME minute[] = {{8,7},{9,7},{14,7},{15,7}};
RTC_DS1307 RTC;
void setup() {
  Wire.begin();
  Serial.begin(57600);
  RTC.begin();
  for(int i=1; i<18; i++)
  {
    pinMode(i, OUTPUT);
  }
  pinMode(S1, INPUT);
  pinMode(S2, INPUT);
  digitalWrite(S1, HIGH);
  digitalWrite(S2, HIGH);
  allOff();
  // CPU Sleep Modes
  // SM2 SM1 SM0 Sleep Mode
  // 0    0  0 Idle
  // 0    0  1 ADC Noise Reduction
  // 0    1  0 Power-down
  // 0    1  1 Power-save
  // 1    0  0 Reserved
  // 1    0  1 Reserved
  // 1    1  0 Standby(1)
  cbi( SMCR,SE );      // sleep enable, power down mode
  cbi( SMCR,SM0 );     // power down mode
  sbi( SMCR,SM1 );     // power down mode
  cbi( SMCR,SM2 );     // power down mode
  // 0=16ms, 1=32ms,2=64ms,3=128ms,4=250ms,5=500ms
  // 6=1 sec,7=2 sec, 8=4 sec, 9= 8sec
  setup_watchdog(4);
 
  if (! RTC.isrunning()) {
    Serial.println("RTC is NOT running!");
    // following line sets the RTC to the date & time this sketch was compiled
    RTC.adjust(DateTime(__DATE__, __TIME__));
  }
  RTC.adjust(DateTime(__DATE__, __TIME__));
 
}
void loop() {
  if (f_wdt==1) // wait for timed out watchdog
    f_wdt=0;
  if (digitalRead(S1)==LOW){
    allOff();
    DateTime now = RTC.now();
    int minu=now.minute();
    int hr;
    if (now.hour()>12)
      hr=now.hour()-12;
    else
      hr=now.hour();
    for (int x=0; x<200; x++) {
      ShowTime(hr,minu);
    }
  }
  system_sleep(); // when we wake up, we’ll return to the top of the loop
}
void LightLED(int row, int col) {
  digitalWrite(row,HIGH);
  digitalWrite(col,LOW);
}
void UnLightLED(int row, int col) {
  digitalWrite(row,LOW);
  digitalWrite(col,HIGH);
}
void allOff() {
  for(int i=0; i<5; i++) {
    digitalWrite(cols[i], HIGH);
  }
  for(int i=0; i<6; i++) {
    digitalWrite(rows[i], LOW);
  }
}
void ShowTime(int hour, int mnt) {
  int hrow = hours[hour-1].row;
  int fivemin = mnt / 5;
  int onemin = mnt % 5;
  LightLED(hours[hour-1].row,hours[hour-1].col);
  delay(3);
  UnLightLED(hours[hour-1].row,hours[hour-1].col);
  LightLED(minutes[fivemin].row,minutes[fivemin].col);
  delay(3);
  UnLightLED(minutes[fivemin].row,minutes[fivemin].col);
  if(onemin>=1)
    LightLED(minute[0].row,minute[0].col);
  if(onemin>=2)
    LightLED(minute[1].row,minute[1].col);
  if(onemin>=3)
    LightLED(minute[2].row,minute[2].col);
  if(onemin>=4)
    LightLED(minute[3].row,minute[3].col);
  allOff();
}
//**************************************************************** 
// set system into the sleep state
// system wakes up when wtchdog is timed out
void system_sleep() {
  cbi(ADCSRA,ADEN);                    // switch Analog to Digitalconverter OFF
  set_sleep_mode(SLEEP_MODE_PWR_DOWN); // sleep mode is set here
  sleep_enable();
  sleep_mode();                        // System sleeps here
  sleep_disable();                     // System continues execution here when watchdog timed out
  sbi(ADCSRA,ADEN);                    // switch Analog to Digitalconverter ON
}
//****************************************************************
// 0=16ms, 1=32ms,2=64ms,3=128ms,4=250ms,5=500ms
// 6=1 sec,7=2 sec, 8=4 sec, 9= 8sec
void setup_watchdog(int ii) {
  byte bb;
  int ww;
  if (ii > 9 ) ii=9;
  bb=ii & 7;
  if (ii > 7) bb|= (1<<5);
  bb|= (1<<WDCE);
  ww=bb;
  Serial.println(ww);
  MCUSR &= ~(1<<WDRF);
  // start timed sequence
  WDTCSR |= (1<<WDCE) | (1<<WDE);
  // set new watchdog timeout value
  WDTCSR = bb;
  WDTCSR |= _BV(WDIE);
}
//**************************************************************** 
// Watchdog Interrupt Service / is executed when  watchdog timed out
ISR(WDT_vect) {
  f_wdt=1;  // set global flag
}





Cheers everyone!


Romain
Reply all
Reply to author
Forward
0 new messages