October 26, 2021

Week 7:
Embedded Programming

The button really bounces... like 20 times each time

This week was a short week, since I've done most of the work two weeks ago. The goal was to simply flash the bootloader onto my controller and make it do something. We used microchip studio to do that. Make sure the programmer is detecting the right voltage on your board. I have learned it the hard way that if you don't have the power condition set up correctly. The picture on the right shows the correct orientation for breakout pin converter on the board. We don't have to use jumper wires if we can use this, which is a lot faster and a lot less annoying (pairing each jumper wire to the correct pin). (Note, I need to use a different 10-pin connector to avoid this all together.)

So I moved on to program the board with my one button and one LED. At first, I wrote a code to simply change the state of LED whenever the button was pressed. Only to realize that the button bounces naturally due to the mechanism. I had to rewrite my code to debounce the button, here is an Arduino example. I also made the board count how many time the button has been pressed, didn't end up integrating that into my program, though. My code looks like this:


#include 
    const int led = 15;
    const int button = 2;
    int led_count = 0;
    int buttonState;           
    int lastButtonState = LOW;
    int ledState = HIGH;
    
    unsigned long pressTime = 0;  // the last time the output pin was toggled
    unsigned long waitTime = 50; // the debounce time; increase if the output flickers
    
    void setup() {
      // put your setup code here, to run once:
      Serial.begin(9600);
      pinMode(led,OUTPUT);
      pinMode(button,INPUT);
      digitalWrite(led,lastButtonState);
      while(!Serial);
      Serial.println("all system go");
      Serial.println(String(lastButtonState));
    
    }
    
    void loop() {
      int reading = digitalRead(button);
      if(reading != buttonState){
        pressTime = millis();
        }
      if(millis() - pressTime > waitTime){
        if(reading != buttonState) {
          buttonState = reading;
          if(buttonState == LOW) {
            ledState = !ledState;
            led_count= led_count + 1;
            Serial.println(led_count);
          }
        }
      }
      digitalWrite(led, ledState);
      lastButtonState = reading;
    }
                                

Nothing too complicated in that code, there was no much to control anyways. One thing did happened in the process of programming and debugging. All of sudden, my laptop would not recognize my board anymore, the port was not detected and there was no response at all. The Arduino IDE threw an error code like this:


processing.app.debug.RunnerException 
	at cc.arduino.packages.uploaders.SerialUploader.uploadUsingPreferences(SerialUploader.java:152)
	at cc.arduino.UploaderUtils.upload(UploaderUtils.java:77)
	at processing.app.SketchController.upload(SketchController.java:732)
	at processing.app.SketchController.exportApplet(SketchController.java:703)
	at processing.app.Editor$UploadHandler.run(Editor.java:2091)
	at java.lang.Thread.run(Thread.java:748)
Caused by: processing.app.SerialException: Fehler beim Ansprechen des seriellen Ports "COM6".
	at processing.app.Serial.touchForCDCReset(Serial.java:107)
	at cc.arduino.packages.uploaders.SerialUploader.uploadUsingPreferences(SerialUploader.java:136)
	... 5 more
Caused by: jssc.SerialPortException: Port name - COM6; Method name - openPort(); Exception type - Port busy.
	at jssc.SerialPort.openPort(SerialPort.java:164)
	at processing.app.Serial.touchForCDCReset(Serial.java:101)
	... 6 more
                                

It turns out the SAMD11 I had on the board was burned somehow... I ended up replacing it with a new one, which fixes the problem. After this, I moved on to designing a different board. This time, the goal is to use all the remaining pins for chalie plexing and while breaking them out to header pins so they can be usefull in the future. Here is a picture of what it looks like. (Note, it only has two LEDs and 4 header pins on it at the moment.) Unfortunately, I didn't get to finish this one, will work on it in the future. Here is a video of the button and the LED in action.


Today I Learned:

1) The button actuallty bounces pretty consistently, ~20 times each time you press.
2) Part placement is so important for routing a PCB... I wish I had done it better two weeks ago.