Notifications
Clear all

Platformio/C++

28 Posts
7 Users
2 Likes
3,121 Views
 GAM
(@gam)
Member
Joined: 3 years ago
Posts: 58
Topic starter  

New to both C++ and Platformio.  New video was great and likely need to watch a few more times.  I have imported a project from Arduino to Platformio and have some errors I don't understand.  Even when I look up the info given I don't get it. It doesn't show below  but there is a Squiggle below the >.  This is the error associated with it.  comparison between signed and unsigned integer expressions [-Wsign-compare]

 // If the max ADC value from the current sensor exceeds the threshold, set the state to LOW
if (analogValue > sensor_thresholds[i]) {
stepper.enable();  //  energize coils - the motor will hold position
stepper.rotate(stepper_positions[i]);  //Moving motor using the degree notation in ()
 
 Hope I gave enough info to work with.  Any help with this would be appreciated and guidance on how to best to figure errors out in the future. 
Thanks
Greg

   
Quote
codecage
(@codecage)
Member Admin
Joined: 5 years ago
Posts: 1037
 

@gam

Well for me anyway that does not give me a picture of the problem you are having.  But maybe someone else can see it from the information you have given.

Do you get errors just loading the code into Platform I/O or when you go to compile?  What board did you select when you set up your project?  I just selected the Arduino UNO for this attempt to help.

I have put the lines you included above into my Platform I/O and just pasting them in I do not have an issue at least so far.  I do not have the squiggly you mention below the ">"

I will need a little (or maybe a lot) more info to know what I should try next.

I am sort of new to Platform I/O myself, so I may wind up confusing you more than helping.  Maybe some other will jump in to offer us both some assistance! 🤣 

Can you provide the entire program?  Maybe attach it as a file and not try to paste it into the post itself.

SteveG


   
ReplyQuote
(@dronebot-workshop)
Workshop Guru Admin
Joined: 5 years ago
Posts: 1081
 
Posted by: @gam

  comparison between signed and unsigned integer expressions [-Wsign-compare]

It's actually a fairly descriptive error message, essentially it says that the two variables you are comparing are not defined as the same type.  The squiggle is saying that the error is the greater-than sign, basically, from PlatformIO's viewpoint, you're comparing apples to oranges.

PlatformIO is less tolerant of errors like these than the Arduino IDE, which in the long run will help you write better code but it can admittedly be frustrating.

But, as I can't see the beginning of the code where you defined them that's the best I can offer right now.

Posted by: @codecage

Can you provide the entire program?  Maybe attach it as a file and not try to paste it into the post itself.

Even better, put it into the post but format it as code, not text.  Which is my way of saying "check out all the new Help screens I added today" LOL.

😎

Bill

"Never trust a computer you can’t throw out a window." — Steve Wozniak


   
ReplyQuote
codecage
(@codecage)
Member Admin
Joined: 5 years ago
Posts: 1037
 

@gam

By the way, I do have 5 undefineds highlighted by Platform I/O after pasting your code snippet into my setup.  And they all have the squiggles under them because they are undefined without the rest of your code.

SteveG


   
ReplyQuote
codecage
(@codecage)
Member Admin
Joined: 5 years ago
Posts: 1037
 

Well I knew the guru could help us out! 😀

And I saw that "Forum Help Menu" this morning, just haven't waded in yet.

Added Edit:

Just looked at the "How to Add Code to Your Post" help.  And I never thought about the attaching the code versus pasting the code, but now that I've read that, I can see the shortfalls of doing it that way.

SteveG


   
ReplyQuote
 GAM
(@gam)
Member
Joined: 3 years ago
Posts: 58
Topic starter  
Thanks for everyone's response.  Here is the complete code. Haven't figure out how to post it as code yet. Much to learn still.

Greg
#include <Arduino.h>
/*This one is starting to work.   When tool is turn on the ACS712 detects it and the stepper moves 90 deg/steps. 
When the tool is turned off it remains in position which is desirable as I would use the same tool multiple times in a row.
Need to add sensor A1 and assign different steps to it.
Can I declare the values of each sensor 1 time to be used whenever that analog port read > current Threshold?
Can I have a statement that will check all the analogs and act on the one that is >current Threshold?
Need for the stepper to only move if a different sensor (than the previous analog#) is > current Threshold?
IF/Else*/

#include <DRV8825.h>
#include <MultiDriver.h> 
#include <SyncDriver.h>
#define MOTOR_STEPS 360 // Motor steps per revolution. Most steppers are 200 steps or 1.8 degrees/step
#define RPM 200
#define MICROSTEPS 1    // Since microstepping is set externally, make sure this matches the selected mode
                        // If it doesn't, the motor will move at a different RPM than chosen
                        // 1=full step, 2=half step etc.
#define DIR 2           // All the wires needed for full functionality
#define STEP 3
#define ENABLE 4        // 2-wire basic config, microstepping is hardwired on the driver
BasicStepperDriver stepper(MOTOR_STEPSDIRSTEPENABLE);
#define SENSOR_N 8    // number of sensors
uint8_t sensor_pins[SENSOR_N] = {A0A1A2A3A4A5A6A7};  //Pins for sensors
uint16_t sensor_thresholds[SENSOR_N] = {600,600,600,600,600,600,600,600};  //Thresholds for corresponding analog pins  
uint8_t stepper_positions[SENSOR_N] = {00,45,90,135,180,225,270,315};   // Stepper positions for corresponding analog pins
#define CURRENT_SAMPLE_PERIOD 250 // The number of milliseconds to sample the current reading, May need to decrease this number if I can  

int analogValue = 0; // Stores ADC values read in from the current sensor
unsigned long stopwatch = 0; // Used to keep track of elapsed time
void setup() {
  Serial.begin(9600);
  pinMode(DIROUTPUT);
  pinMode(STEPOUTPUT);
  pinMode(ENABLEOUTPUT);
 // pinMode (A0, INPUT);
  
    stepper.begin(RPMMICROSTEPS);
    stepper.setEnableActiveState(LOW);  
  }

void loop() {
    for(uint8_t i=0;i<SENSOR_N;i++){
    analogValue = 0; // Initialize analogValue to a known state
    stopwatch = millis(); // Store a snapshot of the current time since the program started executing

    // Collect the max ADC value from the current sensor for the predetermined sample period
    while (millis() - stopwatch < CURRENT_SAMPLE_PERIOD) {
      analogValue = max(analogValueanalogRead(sensor_pins[i]));
      Serial.println(analogValue);
    }

    // If the max ADC value from the current sensor exceeds the threshold, set the state to LOW
    if (analogValue > sensor_thresholds[i]) {
      stepper.enable();  //  energize coils - the motor will hold position
      stepper.rotate(stepper_positions[i]);  //Moving motor using the degree notation in ()
      
   }
    
    }}
// need to return to top of loop
 

   
ReplyQuote
codecage
(@codecage)
Member Admin
Joined: 5 years ago
Posts: 1037
 

@gam

Here is Bill's help on embedding code in a post: How to Add Code to Your Post

 

Will give your code a shot now and see what I can discover.

SteveG


   
ReplyQuote
(@yurkshirelad)
Member
Joined: 3 years ago
Posts: 493
 

What are the types of the two variables analogValuesensor_thresholds[i]?

My guess is one is signed and one is unsigned. They need to be the same.


   
ReplyQuote
codecage
(@codecage)
Member Admin
Joined: 5 years ago
Posts: 1037
 

@gam

Now all I see in the code from above when loaded into my Platform I/O is three libraries that I don't have.  I'm not sure what good it would do me to load the libraries (I would guess I could if I was sure I was getting the exact same ones you have), but without the hardware setup I would not be able to go any further than a compile.  Does you Platform I/O now show errors or warnings even before compiling?

SteveG


   
ReplyQuote
codecage
(@codecage)
Member Admin
Joined: 5 years ago
Posts: 1037
 

@yurkshirelad

I think @gam got that corrected since when I loaded his latest code section I did not have any type mismatches.

SteveG


   
ReplyQuote
(@yurkshirelad)
Member
Joined: 3 years ago
Posts: 493
 

I think it boils down to:

uint16_t sensor_thresholds[SENSOR_N]...

int analogValue = 0; 

 

sensor_thresholds is unsigned, while analogValue is signed.


   
ReplyQuote
(@yurkshirelad)
Member
Joined: 3 years ago
Posts: 493
 
Posted by: @codecage

@yurkshirelad

I think @gam got that corrected since when I loaded his latest code section I did not have any type mismatches.

Sorry, I didn't realise that. Thanks.

 

Another option for posting code snippets is using Github gists ( https://gist.github.com/), assuming you have a Github account. It can be easier than trying to format and read bits of code in a forum topic, no offence to these forums.


   
ReplyQuote
codecage
(@codecage)
Member Admin
Joined: 5 years ago
Posts: 1037
 

@yurkshirelad

Not everyone is going to have a Github account, especially if they are just getting their feet wet.  So while reading the code in a "code" enhanced section of the post may be a little difficult, sometimes it may be the only option.

And the reason I said @gam may have corrected that already is that I'm not seeing the type mismatch at least with the code just sitting in my editor.  Does Platform I/O have enough 'smarts' to see that, or does it really have to be compiled before we'd see that error.  I have not attempted to compile as I probably won't load those missing libraries.

I do see now where the two variables aren't both either signed or unsigned!

SteveG


   
ReplyQuote
(@yurkshirelad)
Member
Joined: 3 years ago
Posts: 493
 

It's a compile time error, so you have to compile to see it. Unless it auto-compiles every time you change the file? I'm not familiar with Platformio. Eclipse auto-compiles in the background after you save a file, at least it does with Java projects. I don't know if that's true for C++.


   
ReplyQuote
 GAM
(@gam)
Member
Joined: 3 years ago
Posts: 58
Topic starter  

Thanks again for your help.  I thought this would be an easy question that it was just my lack of knowledge (TRUE),  I didn't expect that much  back and forth about what was going on.  I guess I need to read up on signed and unsigned. 

Thanks again

Greg


   
ReplyQuote
Page 1 / 2