TinyCircuits Forum

TinyCircuits Products => TinyDuino Processors & TinyShields => Topic started by: TheQuestor on March 17, 2021, 01:07:13 PM

Title: Unknown problem with sketch
Post by: TheQuestor on March 17, 2021, 01:07:13 PM
Ok, I have my board working (finally!)

I uploaded the included blink code, and it worked perfectly.  So, now it's time to do my own first sketch.  Here's what I have:

Code: [Select]
//Pin Number Assignments:
const int LED1 = 4;

void setup()
  {
    pinMode(LED1, OUTPUT);
  }

void loop()
  {
    digitalWrite(LED1, HIGH); // turn the LED on (HIGH is the voltage level)
    delay(1000);              // wait for a second
    digitalWrite(LED1, LOW);  // turn the LED off by making the voltage LOW
    delay(1000);              // wait for a second
  }

I'm using the Tinycircuits proto terminal blocks tinyshield (ASD2005-R-T-I):
(https://ibb.co/6nYXvG7)

The LED, which is attached to ground and #4, comes on immediately; it then sort of blinks (as if you had an intermittent wire -- that's the effect) - and then is on, but dimly.  That's it.  There's no repetitive blinking.  I'm obviously missing SOMETHING, but I don't know what.  I'm an expert programmer in BASIC, C, C#, PHP, and am currently learning python.  The structure of the program is correct; I'm wondering if I have something connected wrong.

Also:  Do I have to "clear" the memory of the board before uploading new code, or does it simply overwrite the old?  Any help is appreciated.  Eventually, I'm going to have nine different LED's, blinking on various schedules.  What I'd like to have is this:

1. Blink on while #2 is off.
2. Blink on while #1 is off.
3. Blink on while #3 is off.
4. Blink on while #4 is off.
5. Random sequence with #5, #6, & #7.
6. Random sequence with #5, #6, & #7.
7. Random sequence with #5, #6, & #7.
8. Ramp up/down (different timing from #9)
9. Ramp up/down (different timing from #8)

Any help is greatly appreciated!
Title: Re: Unknown problem with sketch
Post by: lennevia on March 17, 2021, 04:23:26 PM
Glad to hear you were able to get the board programmed.

Is it possible that when inserting wires into the Proto Terminal Block TinyShield that you didn't tighten the screw enough? This would be the likely culprit for erratic LED behavior.

It is also possible to misread the pin labels on the board since the silkscreen font is tiny. Pin 4 is a corner pin. This is also different from pin A4. Is it possible the LED wired up is in the wrong terminal input?

Best,
RĂ©na
Title: Re: Unknown problem with sketch
Post by: TheQuestor on March 17, 2021, 10:15:08 PM
Actually, I had the LED connected to #1, not #4; it turned on, then acted weird - probably because I was addressing the wrong pin.  Oops.  Can you tell me how do do a ramp up/ramp down (gradually brighter, gradually dimmer) for an LED?  Thanks!
Title: Re: Unknown problem with sketch
Post by: lennevia on March 18, 2021, 02:20:20 PM
You will need to use a PWM pin.

The Arduino IDE comes with a Fade Example built-in. I included a screenshot of how to navigate to the example using the File menu. The Arduino standard libraries, examples, and forums are all great places to learn about programming with Arduino. Since the TinyDuino uses the same processor, the programming part is all the same for the Arduino Uno.

Fade example tutorial: https://www.arduino.cc/en/Tutorial/BuiltInExamples/Fade
Arduino forum: https://forum.arduino.cc/
Title: Re: Unknown problem with sketch
Post by: TheQuestor on March 18, 2021, 02:49:11 PM
Ok... but which pin is a PWM pin on this board??  I can't find anything online that tells me.

(https://i.ibb.co/K06KNBp/Proto.png)
Title: Re: Unknown problem with sketch
Post by: lennevia on March 18, 2021, 03:36:18 PM
On the TinyDuino Platform page, it lists: Pins IO3, IO5, IO6, IO9, SS (IO10), MOSI (IO11): These pins can provide an 8-bit PWM.

The page: https://tinycircuits.com/pages/tinyduino-overview

You can also use a traditional Arduino Uno pinout diagram, as seen here: https://content.arduino.cc/assets/Pinout-UNOrev3_latest.pdf (The PWM pins have the ~ symbol next to the pin numbers.)
Title: Re: Unknown problem with sketch
Post by: TheQuestor on March 18, 2021, 04:58:48 PM
You said that pins IO3, IO5, IO6, IO9, SS (IO10), MOSI (IO11) are PWM; I'm assuming you're referring to the pins marked 3, 5, 6, 9, SS  and MOSI; I have the LED connected to pin 9, but it does NOT do anything at all.

Code: [Select]
    int brightness = 0;    // how bright the LED is
    int fadeAmount = 5;    // how many points to fade the LED by
    // set the brightness of pin 6:
    analogWrite(LED06, brightness);
    // change the brightness for next time through the loop:
    brightness = brightness + fadeAmount;
    // reverse the direction of the fading at the ends of the fade:
    if (brightness <= 0 || brightness >= 255)
      {
        fadeAmount = -fadeAmount;
      }
    // wait for 30 milliseconds to see the dimming effect
    delay(30);

What am I missing here?  If I change the initial brightness to 255, the LED comes on, but never dims.  It's neither brightening nor dimming this LED.
Title: Re: Unknown problem with sketch
Post by: lennevia on March 18, 2021, 05:03:08 PM
You're not showing all of your program, so I can't be sure, but it looks like you are programming pin 6 instead of the pin 9 with your LED.

I recommend trying the Fade example by Arduino:

Code: [Select]
/*
  Fade

  This example shows how to fade an LED on pin 9 using the analogWrite()
  function.

  The analogWrite() function uses PWM, so if you want to change the pin you're
  using, be sure to use another PWM capable pin. On most Arduino, the PWM pins
  are identified with a "~" sign, like ~3, ~5, ~6, ~9, ~10 and ~11.

  This example code is in the public domain.

  http://www.arduino.cc/en/Tutorial/Fade
*/

int led = 9;           // the PWM pin the LED is attached to
int brightness = 0;    // how bright the LED is
int fadeAmount = 5;    // how many points to fade the LED by

// the setup routine runs once when you press reset:
void setup() {
  // declare pin 9 to be an output:
  pinMode(led, OUTPUT);
}

// the loop routine runs over and over again forever:
void loop() {
  // set the brightness of pin 9:
  analogWrite(led, brightness);

  // change the brightness for next time through the loop:
  brightness = brightness + fadeAmount;

  // reverse the direction of the fading at the ends of the fade:
  if (brightness <= 0 || brightness >= 255) {
    fadeAmount = -fadeAmount;
  }
  // wait for 30 milliseconds to see the dimming effect
  delay(30);
}
Title: Re: Unknown problem with sketch
Post by: TheQuestor on March 18, 2021, 05:10:44 PM
Yes, I'm doing pin 6.  According to the site, 6 should be one of the PWM pins (3, 5, 6, 9, SS  and MOSI).  Am I reading this list wrong?  Or is IO6 = 6, and IO9 = 9?
Title: Re: Unknown problem with sketch
Post by: lennevia on March 18, 2021, 05:17:13 PM
You said that pins IO3, IO5, IO6, IO9, SS (IO10), MOSI (IO11) are PWM; I'm assuming you're referring to the pins marked 3, 5, 6, 9, SS  and MOSI; I have the LED connected to pin 9, but it does NOT do anything at all.

You typed pin 9 in your recent comment.

Is your LED facing the correct direction? I recommend trying the exact program and pin layout from the Arduino tutorial before making any edits or trying other pins so that you can make sure you are using a working program and working layout.
Title: Re: Unknown problem with sketch
Post by: TheQuestor on March 18, 2021, 05:57:22 PM
I tried both pin 9 and pin 6; neither did anything.  The red wire from the LED is going to the pin; the black wire to GND.  They light up normally with the digitalWrite(LED06, HIGH) command, so I know the LED's are both wired properly and working.
Title: Re: Unknown problem with sketch
Post by: lennevia on March 18, 2021, 06:14:41 PM
Send a picture of your hardware layout to verify the correct pin connections. I tested the example from Arduino and was able to get the fading behavior, picture attached of pin connections using jumper cables and pin 9.
Title: Re: Unknown problem with sketch
Post by: TheQuestor on March 18, 2021, 06:38:54 PM
(https://i.ibb.co/d4kRQrB/IMG-20210318-183400.jpg)

Yes, I know my wiring is a bit messy, but ALL of the LED's work independently, without lighting any adjacent ones.  I tried pins 3, 5, 6, and 9 - and none had any effect.
Title: Re: Unknown problem with sketch
Post by: lennevia on March 18, 2021, 06:57:25 PM
The messy wiring on the ground pin could be shorting the gnd to a voltage pin right next to it. What LED are you using?

Quote
ALL of the LED's work independently,

Are you saying all of your LEDs have been tested and work with the pins you are programming?

Quote
I tried pins 3, 5, 6, and 9 - and none had any effect.

Does this mean that you edited and uploaded the program for each pin as you changed your hardware configuration? Are you uploading to the correct TinyDuino pin? Do you get a "Done uploading." message each time you upload the program?
Title: Re: Unknown problem with sketch
Post by: TheQuestor on March 18, 2021, 07:00:58 PM
I wrote a simple piece of code that illuminated each LED in turn, from 1 to 9.  They all worked.  I have LED's connected to pins 1-9.  I modified the dimming code to try pin 3, 5, 6, & 9 separately, and uploaded each.  Each uploaded successfully.
Title: Re: Unknown problem with sketch
Post by: lennevia on March 18, 2021, 07:15:42 PM
Can you verify you are using the exact Arduino example for pin 9 that I included above?

The program you included before differed from the Arduino example so I am unable to help you further without knowing what complete code you are using.

To clarify, the loop() in the Arduino program loops over and over on the TinyDuino, so the first line of the code snippet you included " int brightness = 0; " initializes and sets the brightness variable to zero on every loop iteration. This is likely the problem. The brightness variable, as shown in the Arduino example I included, needs to be a global variable outside of the loop() function in order to be usable between loop iterations.
Title: Re: Unknown problem with sketch
Post by: TheQuestor on March 18, 2021, 07:50:01 PM
**CRASH!!*  (This is the sound of me banging my head on the desk).

DERP.

OMG.

I can't believe I did that.  Forty years of programming, and I made a rookie mistake like that.  Boy, do I feel dumb.  Thanks for enlightening me.  Now it works perfectly.
Title: Re: Unknown problem with sketch
Post by: TheQuestor on March 18, 2021, 08:09:45 PM
Here's my entire code:

Code: [Select]
//Pin Number Assignments:
const int LED01 = 1;
const int LED02 = 2;
const int LED03 = 3;
const int LED04 = 4;
const int LED05 = 5;
const int LED06 = 6;
const int LED07 = 7;
const int LED08 = 8;
const int LED09 = 9;
int brightness1 = 0;    // how bright the LED is
int fadeAmount1 = 5;    // how many points to fade the LED by
int brightness2 = 0;    // how bright the LED is
int fadeAmount2 = 5;    // how many points to fade the LED by

void setup()
  {
    pinMode(LED01, OUTPUT);
    pinMode(LED02, OUTPUT);
    pinMode(LED03, OUTPUT);
    pinMode(LED04, OUTPUT);
    pinMode(LED05, OUTPUT);
    pinMode(LED06, OUTPUT);
    pinMode(LED07, OUTPUT);
    pinMode(LED08, OUTPUT);
    pinMode(LED09, OUTPUT);
    randomSeed(analogRead(0));
  }

void loop()
  {
    FlashPair();
    Sequence();
    Ramp1();
    Ramp2();
  }
void FlashPair()
  {
    digitalWrite(LED01, HIGH); digitalWrite(LED03, HIGH);
    digitalWrite(LED02, LOW);  digitalWrite(LED04, LOW);
    delay(500);
    digitalWrite(LED01, LOW);  digitalWrite(LED03, LOW);
    digitalWrite(LED02, HIGH); digitalWrite(LED04, HIGH);
  }
void Sequence()
  {
    long randNumber;
    //5 ~~ 5 ~~ 8 ~~ 8 ~~ 7 ~~ 7
    //8 ~~ 7 ~~ 5 ~~ 7 ~~ 5 ~~ 8
    //7 ~~ 8 ~~ 7 ~~ 5 ~~ 8 ~~ 5
    randNumber = random(1, 6);
    if (randNumber == 1)
      {
        digitalWrite(LED05, HIGH); delay(500); digitalWrite(LED05, LOW); delay(500);
        digitalWrite(LED08, HIGH); delay(500); digitalWrite(LED08, LOW); delay(500);
        digitalWrite(LED07, HIGH); delay(500); digitalWrite(LED07, LOW);
      }
    if (randNumber == 2)
      {
        digitalWrite(LED05, HIGH); delay(500); digitalWrite(LED05, LOW); delay(500);
        digitalWrite(LED07, HIGH); delay(500); digitalWrite(LED07, LOW); delay(500);
        digitalWrite(LED08, HIGH); delay(500); digitalWrite(LED08, LOW);
      }
    if (randNumber == 3)
      {
        digitalWrite(LED08, HIGH); delay(500); digitalWrite(LED08, LOW); delay(500);
        digitalWrite(LED05, HIGH); delay(500); digitalWrite(LED05, LOW); delay(500);
        digitalWrite(LED07, HIGH); delay(500); digitalWrite(LED07, LOW);
      }
    if (randNumber == 4)
      {
        digitalWrite(LED08, HIGH); delay(500); digitalWrite(LED08, LOW); delay(500);
        digitalWrite(LED07, HIGH); delay(500); digitalWrite(LED07, LOW); delay(500);
        digitalWrite(LED05, HIGH); delay(500); digitalWrite(LED05, LOW);
      }
    if (randNumber == 5)
      {
        digitalWrite(LED07, HIGH); delay(500); digitalWrite(LED07, LOW); delay(500);
        digitalWrite(LED05, HIGH); delay(500); digitalWrite(LED05, LOW); delay(500);
        digitalWrite(LED08, HIGH); delay(500); digitalWrite(LED08, LOW);
      }
    if (randNumber == 6)
      {
        digitalWrite(LED07, HIGH); delay(500); digitalWrite(LED07, LOW); delay(500);
        digitalWrite(LED08, HIGH); delay(500); digitalWrite(LED08, LOW); delay(500);
        digitalWrite(LED05, HIGH); delay(500); digitalWrite(LED05, LOW);
      }
  }
void Ramp1()
  {
    // set the brightness of pin 6:
    analogWrite(LED06, brightness1);
    // change the brightness for next time through the loop:
    brightness1 = brightness1 + fadeAmount1;
    // reverse the direction of the fading at the ends of the fade:
    if (brightness1 <= 0 || brightness1 >= 255)
      {
        fadeAmount1 = -fadeAmount1;
      }
    // wait for 20 milliseconds to see the dimming effect
    delay(20);
  }
void Ramp2()
  {
    delay(150);
    // set the brightness of pin 9:
    analogWrite(LED09, brightness2);
    // change the brightness for next time through the loop:
    brightness2 = brightness2 + fadeAmount2;
    // reverse the direction of the fading at the ends of the fade:
    if (brightness2 <= 0 || brightness2 >= 255)
      {
        fadeAmount2 = -fadeAmount2;
      }
    // wait for 50 milliseconds to see the dimming effect
    delay(50);
  }

Everything seems to be working... except that pin 6 & pin 9 seem to be on, both at the same brightness, and aren't fading or brightening.  Am I missing something here?
Title: Re: Unknown problem with sketch
Post by: TheQuestor on March 19, 2021, 10:09:06 AM
I actually "fixed" that problem -- however, the ramp up/down process was WAY too slow; I solved that by not only increasing the amount for the brightness, but calling the ramp functions before & after every delay within my 2nd function.  Not as good as I wanted, but good enough.
Title: Re: Unknown problem with sketch
Post by: lennevia on March 19, 2021, 10:29:35 AM
If you wanted the fade effect to move faster, you can decrease the delay. It looks like you have it at 50ms in your program.
Title: Re: Unknown problem with sketch
Post by: TheQuestor on March 19, 2021, 12:01:10 PM
I actually removed the delay, but it's still slow.  Ideally, I'd like all of my effects to be happening simultaneously, but that's just not possible.  I guess close is good enough.