Shop OBEX P1 Docs P2 Docs Learn Events
SimpleIDE C code problem ... — Parallax Forums

SimpleIDE C code problem ...

… I think. Since the Activity Board only has four ADC channels, I am adding a Micromite chip, which has six ADC channels available. Plus, I might be able to offload some other stuff from the Activity Board, to keep within the RAM confines.

When I run the C program, I get the following output. I cannot figure out why I am getting 'Val2: 0.00'. It seems that the fromMM1() function should be providing the Val2 values. When I comment out the Val1 code, I get 'Val1: 0.00 Val2: 8.01'. This is a mystery to me, hope somebody picks up on a silly mistake that I am making.

Thanks
Ray

SimpleIDE terminal output
Val1: 8.01 Val2: 0.00
Val1: 8.01 Val2: 0.00
Val1: 8.06 Val2: 0.00
Val1: 8.01 Val2: 0.00
Val1: 8.01 Val2: 0.00
Val1: 8.08 Val2: 0.00
Val1: 8.07 Val2: 0.00
Val1: 8.07 Val2: 0.00
Val1: 8.05 Val2: 0.00
Val1: 8.06 Val2: 0.00
Val1: 8.01 Val2: 0.00
Val1: 8.05 Val2: 0.00
Val1: 8.04 Val2: 0.00
Val1: 7.97 Val2: 0.00

SimpleIDE C code
/*
  homesta1.c
  
  May 10, 2019
  New home station sensors setup.
  Uses: Activity WX Board and two Micromite uMCU(12 ADC channels total, ...
  Only have one Micromite on line.
*/
#include "simpletools.h"
#include "wifi.h"
#include "fdserial.h"
#include "simpletext.h"

serial *ser;

/***********************/
/* wifi */
int event, id, handle;
int getFromPageId;
char cmdStr[64];
char *path;
/***********************/

/***********************/
/* Global Variables */
volatile float mm1val1,mm1val2;

//debug
//int dbugc;
/***********************/

/***********************/

/* COGed */
void fromMM1();
/***********************/

int main()
{
  // Add startup code here.
  cog_run(fromMM1,128);
  pause(150);
  wifi_start(31, 30, 115200, WX_ALL_COM);
  pause(50);
  getFromPageId = wifi_listen(HTTP, "/tpfm*");
 
  while(1)
  {
    // Add main loop code here.
    wifi_poll(&event, &id, &handle);
    if(event == 'G')
    {
      if(id == getFromPageId)
      {
        memset(cmdStr, 0, sizeof(cmdStr));
        sprint(cmdStr, "PATH:%d\r", handle);
        path = wifi_command(cmdStr);
        
        if(strstr(path, "/Batt") != 0)
        {
          wifi_print(GET, handle, "%.2f", mm1val1);
        }          
      }        
    }
    else if(event == 'P')
    {
      
    }
    //pause(200);
    //debug
    print("Val1: %.2f  ",mm1val1);
    print("Val2: %.2f\n",mm1val2);
    //print("%c\n",dbugc);
    pause(1000);
  }
}

/****************************/
/*COGed, receiving info fromm Micromite chip #1. */
void fromMM1()
{
  ser = fdserial_open(1, 2, 0, 9600);
  float val1,val2;
  
  while(1)
  {
    
    writeDec(ser,1);  // Send AIN channel 1 request.
    pause(165);
    val1 = readFloat(ser);   // Receive data.
    if(val1 > 0 && val1 < 20.00)  // Bracket the values.
    {
      mm1val1 = val1;  // Make it global.
      
    }
    // Get the next AIN channel.
    pause(50);
    writeDec(ser,2);
    pause(165);
    val2 = readFloat(ser);
    if(val2 > 0 && val2 < 20)
    {
      mm1val2 = val2;
      
    }
    
  }    
}  


Micromite
Open"COM2:9600" As #2


Do
indi$ = "  "
Pause 150
indi$ = Input$(1,#2)

If indi$ = "1" Then
  SetPin 2,AIN
  Print #2, Pin(2)*5.20945
ElseIf indi$ = "2" Then
  SetPin 3,AIN
  Print #2,Pin(3)*5.2094
'ElseIf indi$ = "3" Then
'  SetPin 4,AIN
'  Print #2,Pin(4)
'ElseIf indi$ = "4" Then
'  SetPin 5,AIN
'
'ElseIf indi$ = "5" Then
'  SetPin 6,AIN
'
'ElseIf indi$ = "6" Then
'  SetPin 7,AIN
'
EndIf
loop

Comments

  • The only thing I see is that you are not unsetting the pin.

    Mike
  • What's your memory usage that simpleIDE is reporting?

    My guess is that fromMm1 needs more space. You might try starting it with 256 longs instead of 128 and see if that fixes your problem.
  • Thanks Mike, I assume you meant the pin(s) on the Micromite chip. Well, I tried that, and it did not fix the problem. I thought this would be a straight forward addition of a chip for the Activity Board to work with, not that straight forward I guess.

    The only other thing that I can think of is maybe some kind of a timing problem between the two chips, in terms of reading and writing the data. Although their is no problem with the first go around, maybe something is going on with the second go around. Now how do I test that out. I tried inserting some more pauses, but that is not helping. Anybody have some better ideas?

    I was also thinking about what is happening with the fdserial being opened and used in a COGed function. Is their something that I should be aware of, that might be affecting the functionality.

    Ray
  • Back to the drawing board. The problem, I think, is mostly on the Micromite chip side. I have to come up with some code that uses the ADC component of the chip, in a correct manner. The simple do...loop that I was using, is just not working as expected. I think I have to come up with some firmware, written in MMBASIC; I just did not want to go there. But getting those six channels of ADC, is about the cheapest way that I could think of.

    Now, I am wondering, maybe, instead of the serial approach, I should think about using an I2C setup?? So, maybe some of you experts could advise me with the serial vs I2C approach, for accomplishing my objective.

    Ray
  • I think I have narrowed the problem down to a timing issue. Not sure how to resolve it.

    On the Micromite, I am using a do … loop, on the Activity Board, I am using a while() loop. So, how do I sync these two up, in order for them to talk to each other. On the Micromite, the input command does not have a, wait for data then continue feature, it just keeps looping.

    Maybe I have to reverse the process, have the Micromite continuously send the data, and have the C code sort out the data that it has received. I sounds kind of awkward, but it may accomplish the desired affect. Also the Micromite has a pin interrupt feature, maybe have the C code trigger the Micromite ADC pin, to get the data that way. Boy, this is starting to look like some very complicated code on both ends.

    Ray
  • I take it that the input function returns nothing if there is no data.

    I don't see the problem.

    Mike
  • This experiment is not working out the way I was thinking it would. Instead of the Micromite, I will just add another Activity Board, to pick up an additional four ADC channels.

    I think that I will wait for the P2 to become available. Not sure but I think the P2 will have multiple AD capable pins for usage. The next step in my project will need at least 12 ADC channels. Since I am using some voltage divider breakout boards that can bring down the DC voltage to less than three volts, then the usage of the P2 pins is a viable option, I am hoping. Also, some kind of WiFi module attachment would be a bonus. That with a SimpleIDE Linux version that could program the WiFi module, would be great.

    Ray
  • The P2 has ADC on every pin and, in my testing on the P2-ES, works exactly as it should and is really, really easy to use; set and forget - just set the smartpins to the ADC mode desired and values can easily be read with lightning speed at your convenience.
  • That is good to hear. I just started to look into other electronics to get what I need. Since the P2 will be able to do what I need, I will stick with Parallax.

    I noticed the next batch of P2s got moved to Aug, I might have to get something then. But, the only problem that I can see is there has been no movement for a SimpleIDE for the P2, my preference for working with the Px. And, since I like working with the Px setup remotely, I guess the WiFi component is very important. I hope some of these things starts to fall into place, soon.

    Ray
  • pmrobertpmrobert Posts: 673
    edited 2019-05-17 16:08
    Fastspin by ersmith is a phenomenal compiler under heavy development but already very usable. It does BASIC, C, Spin2, etc. There is at least one VS Code plugin for Spin2. I use VS Code configured to use fastspin for my preffered language, C, and am very happy.
    https://github.com/totalspectrum/spin2cpp/releases
  • The P2 has ADC on every pin and, in my testing on the P2-ES, works exactly as it should and is really, really easy to use; set and forget - just set the smartpins to the ADC mode desired and values can easily be read with lightning speed at your convenience.
    I assume "set and forget" has to be done in PASM. I noticed Chip is working on Spin2, but I have not seen any indication as to what the commands would look like on Spin2, for working with ADC. I hope that their is no expectation that we will be doing this in some cryptic form of PASM. Now that I think about it, there is a lot of new stuff with the P2, is all the new stuff going to be handled with cryptic PASM?

    Ray
Sign In or Register to comment.