Embedded Programming

1 2 3 4 5 6 7 8 9 10 11 12 13 14

Arduino

Blinking LED & button test

A couple of weeks ago, when setting up and soldering the PCB, I didn't manage to program the board on the RPL computer (maybe it was a permissions issue), so I ended up following Jackie's page and programming the board in Arduino instead. I simply modified the Blink sketch, and at that time I didn't realize that setting the pin mode of the pin I assigned to the switch to "INPUT_PULLUP" was also a possibility, and hence my switch button was sort of unstable.

This time, I created another short script in Arduino to properly control the pull-up resistor, and I wanted to vary the LED's behavior upon interaction with the button. I also wanted to test if the same setup, in Arduino and using the shell, would work on my computer, so I wouldn't need to rely on working in the architecture shops.


const int LED_PIN = 7;
const int BUTTON_PIN = 8;
const int DELTA_INIT = 100;
const int DELAY_INIT = 1000;

int buttonState = 0;  

float delta = 100;
float delta_fraction = 0.5f;
float limit = 0;
float currDelay = 1000;
float currValue = HIGH;
 
void setup() {
  currDelay = DELAY_INIT;
  delta = DELTA_INIT;

  pinMode(LED_PIN, OUTPUT);
  pinMode(BUTTON_PIN, INPUT_PULLUP);
}

void resetValues() {
  currDelay = DELAY_INIT;
  delta = DELTA_INIT;
}

void blinkLED(float delayVal) {
  digitalWrite(LED_PIN, HIGH);
  delay(delayVal);
  digitalWrite(LED_PIN, LOW);
  delay(delayVal);
}

void loop() {
   buttonState = digitalRead(BUTTON_PIN);

   if (buttonState == HIGH){
      digitalWrite(LED_PIN, LOW);
   }
   else{
      if (currDelay > limit) {
        blinkLED(currDelay);
        
        currDelay -= delta;
        delta *= delta_fraction;
      }
      else {
        resetValues();
      }
    }
}
      				

Quick modified Arduino sketch to test interaction & Arduino setup using my laptop


const int LED_PIN = 7;
const int BUTTON_PIN = 8;
const int DELAY_INIT = 1000;
const float LIMIT = 0.001f;

int buttonState = 0;  
float delta_fraction = 0.7f;
float currDelay = DELAY_INIT;
float currValue = HIGH;
 
void setup() {
  currDelay = DELAY_INIT;
  pinMode(LED_PIN, OUTPUT);
  pinMode(BUTTON_PIN, INPUT_PULLUP);
}

void resetValues() {
  if (currDelay != DELAY_INIT) {
    currDelay = DELAY_INIT;
  }
}

void blinkLED(float delayVal) {
  digitalWrite(LED_PIN, HIGH);
  delay(delayVal);
  digitalWrite(LED_PIN, LOW);
  delay(delayVal);
}

void loop() {
   buttonState = digitalRead(BUTTON_PIN);

   if (buttonState == HIGH){
      digitalWrite(LED_PIN, LOW);
      resetValues();
   }
   else{
      if (currDelay > LIMIT) {
        blinkLED(currDelay);
        currDelay *= delta_fraction;
      }
      else {
        resetValues();
      }
    }
}
      				

Simplified sketch with faster delay change
Blinking LED & button test

I also wanted to check if I could communicate with the board using my serial cable via the Arduino IDE (Serial monitor), and I was following this tutorial to get it to work.

#include < SoftwareSerial.h >

// initializing 
SoftwareSerial Serial(RX, TX);

// in setup
Serial.begin(9600);
while (!Serial) {
    ;
}
Serial.println("Initializing...");
      			

But I couldn't see the COM port under Ports, so the Serial wait loop never returned. I followed the tips here too, but the port is still not showing up.

Terminal

I didn't program the USBtiny on my computer, so I wanted to set up the environment for that too (as well as for testing C), but ran into a couple of install issues, mostly because I have a new computer and the move from my old one caused missing libraries and needed updates. But it all worked in the end!

Brew error possibly due to missing command line tools

I copied Neil's Blink code for Arduino's to test the LED blink using C, using the same Makefile as for the echo board (obviously replacing the file names).

#include 
#include 

#define output(directions,pin) (directions |= pin) // set port direction for output
#define set(port,pin) (port |= pin) // set port pin
#define clear(port,pin) (port &= (~pin)) // clear port pin
#define pin_test(pins,pin) (pins & pin) // test for port pin
#define bit_test(byte,bit) (byte & (1 << bit)) // test for bit set
#define led_delay() _delay_ms(100) // LED delay

#define led_port PORTA
#define led_direction DDRA
// #define led_pin (1 << PB7)
#define led_pin (1 << 7)


int main(void) {
   //
   // main
   //
   // set clock divider to /1
   //
   CLKPR = (1 << CLKPCE);
   CLKPR = (0 << CLKPS3) | (0 << CLKPS2) | (0 << CLKPS1) | (0 << CLKPS0);
   //
   // initialize LED pin
   //
   clear(led_port, led_pin);
   output(led_direction, led_pin);
   //
   // main loop
   //
   while (1) {
      set(led_port, led_pin);
      led_delay();
      clear(led_port, led_pin);
      led_delay();
      }
   }
      			

I had to switch the ports & the PB pins weren't recognized by the compiler, so I just changed PB7 to 7. This worked fine & was definitely fast to upload.