slviajero / tinybasic

A BASIC interpreter for Arduino, ESP, RP2040, STM32, Infineon XMC and POSIX with IoT and microcontroller features.
GNU General Public License v3.0
203 stars 31 forks source link

How to return a factor to a variable #26

Closed EncomLab closed 2 years ago

EncomLab commented 2 years ago

My goal is to assign a variable to a value returned from a sensor - for example:

 10 LET U=PING 1,2
 20 PRINT U

U = cm where PING is:

void xping() {
  nexttoken();
  parsenarguments(2);

  //if (er != 0) return;
  x = pop();
  y = pop();

  short int trigPin; //required
  short int echoPin;
  long duration, cm; //inches;

  trigPin = pinNum(y);
  echoPin = pinNum(x);

  //initialize ping
  pinMode(trigPin, OUTPUT);//trigger
  pinMode(echoPin, INPUT);//echo

  // The sensor is triggered by a HIGH pulse of 10 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  digitalWrite(trigPin, LOW);
  delayMicroseconds(5);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  // Read the signal from the sensor
  pinMode(echoPin, INPUT);
  duration = pulseIn(echoPin, HIGH);

  // Convert the time into a distance
  cm = (duration / 2) / 29.1;   // Divide by 29.1 or multiply by 0.0343

  Serial.print(cm);
  push(cm);
}

This prints the correct distance but I do not understand the steps required to have the result assignable to a variable.

slviajero commented 2 years ago

xping has to be a function. This means it will be something like

10 LEN U=PING(1,2)

so it is subscripts and not arguments you want to parse. There is a method already implemented for it. Most of your function can stay but it has to be hooked into the factor function. Let’s look at an example, the USR function.

Look for the code

    case TUSR:
        parsefunction(xusr, 2);
        break;

This is how a function is hooked into the factor routine. It handles all the integration into the arithmetic. You need to add your token and function here and not in statement().

Then look at the function xusr itself. It simply pops two arguments and pushes one. There is no nexttoken or anything else required. parsefunction() does that for you.

The correct code would be

In factor

case TPING:
    parsefunction(xping, 2); // parse a function with two arguments
    break;

and then somewhere else

void xping() {

x = pop(); y = pop();

short int trigPin; //required short int echoPin; long duration, cm; //inches;

trigPin = pinNum(y); echoPin = pinNum(x);

//initialize ping pinMode(trigPin, OUTPUT);//trigger pinMode(echoPin, INPUT);//echo

// The sensor is triggered by a HIGH pulse of 10 or more microseconds. // Give a short LOW pulse beforehand to ensure a clean HIGH pulse: digitalWrite(trigPin, LOW); delayMicroseconds(5); digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW);

// Read the signal from the sensor pinMode(echoPin, INPUT); duration = pulseIn(echoPin, HIGH);

// Convert the time into a distance cm = (duration / 2) / 29.1; // Divide by 29.1 or multiply by 0.034 push(cm); }

Best Regards, Stefan

Am 18.07.2022 um 21:47 schrieb Encom Lab @.***>:

My goal is to assign a variable to a value returned from a sensor - for example:

10 LET U=PING 1,2 20 PRINT U U = cm where PING is:

void xping() { nexttoken(); parsenarguments(2);

//if (er != 0) return; x = pop(); y = pop();

short int trigPin; //required short int echoPin; long duration, cm; //inches;

trigPin = pinNum(y); echoPin = pinNum(x);

//initialize ping pinMode(trigPin, OUTPUT);//trigger pinMode(echoPin, INPUT);//echo

// The sensor is triggered by a HIGH pulse of 10 or more microseconds. // Give a short LOW pulse beforehand to ensure a clean HIGH pulse: digitalWrite(trigPin, LOW); delayMicroseconds(5); digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW);

// Read the signal from the sensor pinMode(echoPin, INPUT); duration = pulseIn(echoPin, HIGH);

// Convert the time into a distance cm = (duration / 2) / 29.1; // Divide by 29.1 or multiply by 0.0343

Serial.print(cm); push(cm); } This prints the correct distance but I do not understand the steps required to have the result assignable to a variable.

— Reply to this email directly, view it on GitHub https://github.com/slviajero/tinybasic/issues/26, or unsubscribe https://github.com/notifications/unsubscribe-auth/ACSY56BY3DXRQ43QJCT3X2TVUWYFBANCNFSM535NFMPQ. You are receiving this because you are subscribed to this thread.

EncomLab commented 2 years ago

Thanks for the quick response Stefan! Will make these changes.

EncomLab commented 2 years ago

Works perfectly - thanks again.

slviajero commented 2 years ago

Enjoy! Pleasure to help.

Am 18.07.2022 um 22:24 schrieb Encom Lab @.***>:

Works perfectly - thanks again.

— Reply to this email directly, view it on GitHub https://github.com/slviajero/tinybasic/issues/26#issuecomment-1188267993, or unsubscribe https://github.com/notifications/unsubscribe-auth/ACSY56AQ7S56FA3X3GTYYLDVUW4RBANCNFSM535NFMPQ. You are receiving this because you commented.