Show Posts
Pages: [1] 2 3 4
1  Using Arduino / Project Guidance / Best approach of Parsing variable data from a string? on: December 02, 2013, 05:04:18 pm
I am building a project that refers to GCode for its data input.
I have managed to put together a sketch that reads the file one line at a time, and currently ignores anything that doesnt have a G in front of it!
Here is a snippet of the file (each line is a string)

Code:
G03 X128.058354 Y39.348978 Z-0.125000 I-1.307156 J-7.287508
G03 X127.286493 Y39.381414 Z-0.125000 I-0.772541 J-9.183613
G01 X126.899108 Y39.381414 Z-0.125000
G01 X126.899108 Y41.491541 Z-0.125000
G01 X135.489923 Y41.491541 Z-0.125000
G01 X135.489923 Y8.833634 Z-0.125000
G00 Z1.000000

So i want to populate three Floats with the appropriate values, X, Y and Z (I, and J are getting ignored for now)

As you can see, X is not necessarily the first number a parse would come across!
is there a way I can scan through the string to the X, if there is an X fill it with the float (and if not do something else with it perhaps), and then scan across to the Y, and then the Z?
I have found "IndexOf", but have not managed to work out how to grab the number which follows that position.
I suspect it might be a bit like this?:

Code:
  int indexX = inputString.indexOf('X');
        if (indexX > 1) int x = inputString[indexX].parseFloat();

except it isnt., or at least i get:

HelloWorld:81: error: request for member 'parseFloat' in 'inputString.String::operator[](((unsigned int)indexX))', which is of non-class type 'char'

any pointers as to which function would achieve what i need?

Thanks
2  Using Arduino / Storage / Re: Reading an SD card, one line at a time. on: December 02, 2013, 02:09:14 pm
It seems to be because the String wasnt long enough to accept the next line. Sorted it. Onto the next issue!
3  Using Arduino / Storage / Re: Reading an SD card, one line at a time. on: December 02, 2013, 01:28:06 pm
I didn't include the rest of the code as there is a lot of excess guff to look through, which will be used later (the sketch is based on a preceding version)
I got a "no changes to make" response from autoformat and as far as i can see the curly braces are on their own lines apart from the ones at the "voids"
Thanks for looking.

Code:
//LIBRARIES
#include <SD.h>
#include <AFMotor.h>

//CONNECTIONS
File myFile; // instance of a file
const int chipSelect = 15; // adafruit SD breakout, wired 15 - 18. must use modified SD library to allow for reassignment of pins.
AF_Stepper motorL(200, 1);  // Left Motor, M1 & M2
AF_Stepper motorR(200, 2);  // Right Motor, M3 & M4 // Forward is Up on both motors.
const int button = 13; //button holds the sketch in setup, until pressed. This stops the motors from moving under USB power while uploading.
const int led = 14;
const int relay = 2;

// WORKING VALUES
char inputString [100];
char inputChar;
int stringIndex = 0; // String stringIndexing int;

void setup(){

  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  // setup
  pinMode (led, OUTPUT);
  pinMode (button, INPUT);
  pinMode (relay, OUTPUT);
  motorL.setSpeed(20);  // 10 rpm   
  motorR.setSpeed(20);  // 10 rpm   

  Serial.print("Motors ready, Initializing SD card...");
  // make sure that the default chip select pin is set to
  // output, even if you don't use it:
  pinMode(SS, OUTPUT);

  // see if the card is present and can be initialized:
  if (!SD.begin(15,16,17,18))
{
    Serial.println("Card failed, or not present");
    // don't do anything more:
    while (1) ;
  }
  Serial.println("card initialized.");

  // Open up the file we're going to log to!
  myFile = SD.open("HELLO.txt");
  if (! myFile)
 {
    Serial.println("error opening datalog.txt");
    // Wait forever since we cant write data
    while (1) ;
  }
  digitalWrite (led, HIGH);
  Serial.println("Waiting...");
  //hold
  while (digitalRead (button) == HIGH)
{
    // stops script. Its waiting for a button press (LOW on "button")
  }
  Serial.println("....Running");
}
void loop() {

  inputChar = myFile.read(); // Gets one byte from serial buffer
  if (inputChar != 'G') // define breaking char here (\n isnt working for some reason, i will follow this up later)
  {
    inputString[stringIndex] = inputChar; // Store it
    stringIndex++; // Increment where to write next
  }
  else 
  {
    Serial.print("test: "); // shows that the program is cycling, for debugging only
    Serial.println(inputString);
    delay (1000);
    stringIndex = 0; // clear the value for the next cycle
  }
}
4  Using Arduino / Storage / Re: Reading an SD card, one line at a time. on: December 01, 2013, 03:26:34 pm
So i got it to read the first line! but now it isnt cycling, it reads the first line then stops.
can anyone see why?
I was expecting this to serialprint individual lines (between Gs in this case) with a 1 second delay in between...

Code:
void loop() {

 
  inputChar = myFile.read(); // Gets one byte from serial buffer
  if (inputChar != 'G') // define breaking char here (\n isnt working for some reason, i will follow this up later)
    {
    inputString[stringIndex] = inputChar; // Store it
    stringIndex++; // Increment where to write next
  }
  else 
  {
    Serial.print("test: "); // shows that the program is cycling, for debugging only
    Serial.println(inputString);
    delay (1000);
    stringIndex = 0; // clear the value for the next cycle
    inputString [100]; // clear the value for the next cycle?
  }
}
5  Using Arduino / Storage / Re: reading file on the SD card on: December 01, 2013, 06:36:14 am
Im a complete noob, so my language wont be technical

One of the examples has a section where by it only writes to the SD card periodically. it buffers the data inbetween onboard the arduino. This is used because it uses a lot of a battery power to  write to the card.  on the flip side if the power dies you loose what was saved to the buffer in that period.
perhaps you could use that function, make the writes to SD card occur only once every half hour or so? (or even on the half hour using the RTC, so you know how much time you have to take the card out and do what you need to do?)

also, my SD card breakout board has a pin which can be used to tell when there is a card in the slot (or not)
you could use that function so that when the card is missing, the sketch buffers the data. once the card is returned to the slot, it dumps what it has saved and carries on as normal

I have NO idea what the removal of the card does to the sketch.
6  Using Arduino / Storage / Reading an SD card, one line at a time. on: December 01, 2013, 06:27:19 am
Im certain this is in the reference section, and probably in the forum too, but for the life of me i cant find it! maybe my search box skills are not cut out for this forum. smiley-red

I want to read an SD card text file one line at a time. I am using the arduino to process a Gcode file (saved as a .TXT rather than .NGC)

I think the best approach is to be buffer one line at a time, using /n. (using a char string perhaps?)
I can then discard the lines that dont start with G"ii" and make an action based on the ones that do.
I figure i would then go one line at a time in the loop, but would make sure it would progress to the next time each time the loop cycles, rather than repeating line one.....

would someone for who this is easy possibly outlay what i would need?

Read from SD card then buffer to a string??



7  Using Arduino / Programming Questions / Re: Read Servo Position based on light level? on: November 18, 2012, 05:32:36 pm
I got assistance and constructive feedback elsewhere.
8  Using Arduino / Programming Questions / Re: Read Servo Position based on light level? on: November 18, 2012, 02:57:28 pm
Quote
If you are going to discard the return value, why bother calling the function?

I don’t recall discarding the information.
As i understand it, by placing this in the setup part of the sketch, it applies the restriction to the entire Code. I have put it in here, to prevent my servos from clashing on physical objects and trashing themselves due to a balls up in the loop section. If it doesnt work like this then i have made a mistake. I refer to you the "Newbie" part of my stats.

Quote
If you aren't going to do anything, does the conditional matter? If you aren't sure yet what to do, put a comment to that effect in the body.

I don't know, you tell me. It makes the sketch works in a different way to how it works without it, a way that is closer to what I want/expect it to do. So it seems to be a good idea. I specifically want it to do nothing here. Again, I refer to you the "Newbie" part of my stats.

Quote
If vertical spacing is good, why isn't horizontal spacing?

maybe because I have been working on this simple silly little code (which appears to be below you) for some time, and things have been chopped and changed more times than I care to count.
maybe just because it annoys you. Evidently


9  Using Arduino / Programming Questions / Re: Read Servo Position based on light level? on: November 18, 2012, 01:02:47 pm
I think this is properly indented now.

I realised already that i needed to move the calcs into the "for" sections. Although whether or not "for" is the best instruction to use in this scenario, i don’t know.
I have got so far, I want it to update once per sweep. I have added a count function, and have put a "print" request within this, so that it should only print once per sweep too.

I am currently getting the prints at the end of each sweep, and it is printing the value of the extremes only. (40, 130, 40, 130 etc..)

(once i have got a simple sweep back and forth going, i will be able to include the vertical axis, and identify a position, before making a movement decision based on the information.)

I think it is the voidloop where i have fallen down, but here is everything:


Code:
#include <AFMotor.h>
#include <VarSpeedServo.h>

AF_DCMotor motorL(3, MOTOR12_1KHZ); 
AF_DCMotor motorR(2, MOTOR12_1KHZ);
VarSpeedServo Hori;
VarSpeedServo Vert;

const int PR = A1; // connect PR to A1
const int RLED = 13; // indicator LED: 13 and 2 are the only properly pins on the motorshield.
const int LLED = 2; // indicator LED

int HoriPos = 90;      // straight forward,
int VertPos = 90;      // 30 degrees up from horizontal

int LightLevel;
int PrevLightLevel;
int Direction;
byte count =1;

void setup() {

  Serial.begin(9600);  // Serial at 9600 bps

  Hori.attach(9);  // Hori Servo on Pin 9 (Servo2 on the Motorshield)
  Vert.attach(10); // Vert Servo on Pin 10 (Ser1 on the Motorshield)

  constrain(HoriPos, 40, 130); // 90 is center, straightforward, 40 is Left, 130 is full right. Doesnt foul when vert is = 120, would foul if vert is < 120
  constrain(VertPos, 1, 170); // 0 is tilted back approx 30,  170 is max down without fouling on body. 30 is vertical, 120 is horizontal

  motorL.setSpeed(150);      // set the speed of motors: 0 is stop, 255 is full speed:
  motorR.setSpeed(150);

  pinMode (RLED,OUTPUT);
  pinMode (LLED,OUTPUT);

}

void loop() {


   
  for(HoriPos = 40; HoriPos <= 130; HoriPos += 1)
  {   

    Hori.write(HoriPos);
    PrevLightLevel = LightLevel;
    LightLevel = analogRead(PR);   

    if (LightLevel > PrevLightLevel && count == 1) {
      Direction = HoriPos;
      Serial.println(Direction);
      count = 0;
    }

    if (LightLevel > PrevLightLevel && count == 0) {
    }

    if (LightLevel < PrevLightLevel) {
    }
   
    if (HoriPos == 130){
      count = 1;
    }
    delay(50);                         


  }


  for(HoriPos = 130; HoriPos >= 40; HoriPos-=1)   
  {

    Hori.write(HoriPos);

    LightLevel = analogRead(PR);   
    PrevLightLevel = LightLevel;
    if (LightLevel > PrevLightLevel && count == 1) {
      Direction = HoriPos;
      Serial.println(Direction);
      count = 0;
    }

    if (LightLevel > PrevLightLevel && count == 0) {
    }

    if (LightLevel < PrevLightLevel) {
    }

    if (HoriPos == 40){ 
      count = 1;
    }
    delay(50);                         


  }
}



   

Thanks for looking.
10  Using Arduino / Programming Questions / Read Servo Position based on light level? on: November 15, 2012, 03:37:10 pm
Hi there smiley I am fiddling with my second light follower robot on my 2WD chassis
the first one was the classic "3 photo resistors" model.
Now iI have fitted a servo onto the chassis, and have mounted a single photo resistor onto the servo.



https://fbcdn-sphotos-e-a.akamaihd.net/hphotos-ak-ash3/178416_637341408647_239375050_o.jpg

The servo scans from 40 degrees, to 130 degrees (90 is the centre, both of these values are the extents possible without fouling the photo resistor on the bodywork)

I want the photo resistor to take a reading for each increment of servo movement, compare it to the previous reading, and once the light level starts to drop down again (as in once the photo resistor has scanned past the light, and starts to scan away from the light source), it notes the position of the servo, and logs it as a "direction" to the light source.

currently 130 is left, 90 is straight forward, and 40 is right. I would build "windows" into the factors, as in "if the light is between 80 and 100, straight forward, else steer, depending on value)

here is my voidloop. I think im along the right lines, I know it is incomplete to do what i want, but im not sure what to focus on to do what i need to do. any advice would be top

Code:
 
//Sense Light direction
  int LightLevel = analogRead(PR);   

      if (LightLevel < PrevLightLevel) {
        PrevLightLevel = LightLevel;
      } 
  else if (LightLevel > PrevLightLevel) {
        Direction = HoriPos;
      } 
     
  Serial.print(Direction);
  Serial.print(", ");
  Serial.println(LightLevel);   

 
  {
  for(HoriPos = 40; HoriPos < 130; HoriPos += 1)
  {   
    Hori.write(HoriPos);             
    delay(15);                     
  }
  for(HoriPos = 130; HoriPos>=40; HoriPos-=1)   
  {
    Hori.write(HoriPos);             
    delay(15);                     




  }
}

11  Using Arduino / Sensors / Re: Noisey Analog photoresistor read on: November 04, 2012, 12:53:32 pm
Woah. Thats the Badger. Sorry i didnt put the code up earlier. 

Im a bit of a noob, so tend to take "example" codes, and then mash them together. I think i see what i did wrong there. I also think i see why on the sample code I was referencing it wasnt causing any problems. smiley-red

Though there are anomalies which still confuse me.

prime example, some codes specify that pins are either input or outputs.
others, they dont bother.
does it matter which way around you do these things?

every day is a school day, still learning. I will try and regraph this data now...

Thank you kindly.
12  Using Arduino / Sensors / Re: Noisey Analog photoresistor read on: November 04, 2012, 11:41:09 am
Code is below. Sorry, i could have done something daft in there.

whats an "SW artefact"??
it did occour to me that it could be a short, but i started off with the circuit on a breadboard, and have subsequently put it onto a veroboard. both builds have the same issues.
Also, the spikes back on the colours graph nr the top, all line up, which suggests to me it could be an arduino issue, rather than the hardware.
i dont suppose i can clean up the power supply recieved from the usb, but obviously i need it to read the serial port.

I am trying to sort the PR values, with no colours involved. on the lastest tests, the LED has been off (write 255 to all pins as it is common anode, rather than cathode)

Code:
const int LEDR = 6;
const int LEDG = 5;
const int LEDB = 3;
int PR1 = A2;

const int button = 4;
//int Scale = 0;


void setup(){

  Serial.begin(9600);

  pinMode (LEDR, OUTPUT);
  pinMode (LEDG, OUTPUT);
  pinMode (LEDB, OUTPUT);
  pinMode (PR1, INPUT);

  pinMode (button, INPUT);

  analogWrite(LEDR, 255);
  analogWrite(LEDG, 255);
  analogWrite(LEDB, 0); // Program Starts Blue
 

}

void loop()  {

  analogWrite(LEDR, 255);
  analogWrite(LEDG, 255);
  analogWrite(LEDB, 0); // Program Starts Blue

  while (digitalRead (button) == HIGH){
    // stops script. Its waiting for a button press (LOW on "button")
  }

Serial.println("************************Break*********************");  
  //Scale = 0;

  //Green Up
  for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) {
    analogWrite(LEDG, fadeValue);
    delay(20);  
    PR1 = analogRead(PR1);
    Serial.println(PR1);
  
                          
  }

  //Blue Down
  for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5) {
    analogWrite(LEDB, fadeValue);  
  delay(20);      
    PR1 = analogRead(PR1);
    Serial.println(PR1);
                      
  }

  //Red Up
  for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) {
    analogWrite(LEDR, fadeValue);  
    delay(20);    
    PR1 = analogRead(PR1);
    Serial.println(PR1);
                        
  }

  //Green Down
  for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5) {
    analogWrite(LEDG, fadeValue);      
   delay(20);    
    PR1 = analogRead(PR1);
    Serial.println(PR1);
                            
  }



}
13  Using Arduino / Sensors / Re: Noisey Analog photoresistor read on: November 04, 2012, 10:35:15 am


more graphs.

The Blue line is with "//" before the "write to LED" line. The duino still carries out the calcs.
the red line is with a "//" before EVERYTHING other than "write PR value to serial port"
less spikeyness, but the spikeyness is still there.
the LED isnt doing anything either, its off, so it cant be PWM sensitivity?

suggestion as to resistor change perhaps?
i wouldnt know where to start there  smiley-red
14  Using Arduino / Sensors / Re: Noisey Analog photoresistor read on: November 04, 2012, 10:17:55 am
Hi dc42. That was my thoughts, the opaque SHOULD be super predictable and stable, so thats the one to concentrate on.
so your suggestions in order:

  • The filter is very opaque. held directly over a Halogen Desk lamp, you can JUST see a glow through it, but it gets very hot very quickly. The LED has nothing on the power of this lamp.
  • I am pretty certain no exterior light is getting in. the setup has a slot barely big enough to fit the filter into, and is encased on all sides by ply or natural timber (modified further than the picture above). Running the test in the dark makes no difference. I have also blacked out the onboard LEDs on the duino on the off chance they were shining through the shield
  • the pull up resistor is 10k ohm.
  • i moved the USB to a front mounted port on the PC, rather than in the back. I dont THINK it made any difference. It is still very spiky.
  • i have run the program without the "write to LED" line in it, so it still carries out all the calculation. It still creates the spikes just the same

Maybe a change of resistor would help, the spikes are effectively the voltage returning to LOW?, but what would you suggest on that front?
I will have a fiddle with the PWM,if i can work it out...

Thank you kindly.
15  Using Arduino / Sensors / Re: Noisey Analog photoresistor read on: November 03, 2012, 12:15:46 pm
The sketch outputs the reading taken from the PR, once per "fade unit" on the LED.

this results in a column of data pasted into excel
The sketch is re run for each coloured filter, and the column pasted into the file

the x Axis, represents the cycling from Blue (Left most) through to Red (Right most). The units are currently only "line numbers"

The Y axis represents the reading from the PR. as the colour changes on the LED, the filter allows only a certain percentage of light through to teh PR.

for each colour, there is a separate column in the data set, and therefore a separate line on the graph.
The Dark grey represents an opaque filter, and in theory should sit at or close to 1023 and be stable...
the Light grey represents no filter, and is expected to be "high" on the graph (low resistance) but how high is not known, as it this will depend on the sensitivity of the PR and the quality of light out put by the LED.

ideally the coloured graphs will be adjusted so that the 100% light transmission is defined by the light grey coloured line, as 100% on the Y axis, and 0% is defined by the Dark Grey line.
Pages: [1] 2 3 4