taweili / ardublock

ArduBlock is a Block Programming Language for Arduino.
GNU General Public License v3.0
414 stars 292 forks source link

Pin control blocks treat Constants (NumberBlocks) differently... but why? #153

Closed TonyCallear closed 7 years ago

TonyCallear commented 8 years ago

Code in many pin control blocks generate different code depending on whether integer constants are given (instance of NumberBlocks) or other kinds of valid input (Variables, numberlists). Usually this just means setting pinMode once in Setup rather than using a special function.

Does anyone know why this is done?

It can create some odd bugs, which seem easy to fix but perhaps I just don’t understand why NumberBLock should be “special”?

eg.

al0

Works and does something useful. If pin 5 is momentarily brought LOW an LED/resitor connected to it are powered on.

It generates...

void __ardublockDigitalWrite(int pinNumber, boolean status)
{
  pinMode(pinNumber, OUTPUT);
  digitalWrite(pinNumber, status);
}

void setup()
{
}

void loop()
{
  if (__ardublockDigitalRead(5))
  {
    __ardublockDigitalWrite(5, HIGH);
    delay( 5000 );
  }
}

but

al1

Does not work. It generates...

void setup()
{
  pinMode( 5 , INPUT);
  pinMode( 5 , OUTPUT);
}

void loop()
{
  if (digitalRead(5)) // Pin is in OUTPUT mode!
  {
    digitalWrite( 5 , HIGH );
    delay( 5000 );
  }
}