Show Posts
Pages: 1 2 [3] 4 5 ... 7
31  Using Arduino / Programming Questions / Re: mapping values on: October 22, 2012, 07:43:27 am
Quote
needs to be declared only the first time, will static also solve this problem?
That is what static is meant for. Declaring variables that you will expressly reset every pass through loop as static doesn't really make sense.
Thank you PaulS, you helped me excessively.
32  Using Arduino / Programming Questions / Re: mapping values on: October 22, 2012, 07:38:45 am
Quote
but they would be declared over and over
So? It doesn't matter. Make them static, so that doesn't happen, if it makes you feel better. (It won't make the Arduino feel any better.)
yep got the point...
Code:
static int perl, perr;

but this
Code:
int prevperl = 0, prevperr = 0;
needs to be declared only the first time, will static also solve this problem??
33  Using Arduino / Programming Questions / Re: mapping values on: October 22, 2012, 07:19:41 am
Quote
at least I remember that we used pointers (in highschool) instead of global variables ...
For what?

For instance,
Code:
int vall, valr;
int perl, perr;
int prevperl = 0, prevperr = 0;
are all global variables that do not need to be. There are only referenced in loop().

How would you use pointers instead of global variables? I can't see that that makes sense. Just make these local variables.

Now, you do want the values in the last two to persist between calls to loop(), so they should be static.

but they would be declared over and over ( while(1) ), which makes no sense to me.....
34  Using Arduino / Programming Questions / Re: mapping values on: October 22, 2012, 07:07:48 am
Quote
3. Are there any other major issues with my sketch?
Yes, this one:
Code:
vall = map(vall, 0, 100, 90, 180);
  valr = map(valr, 0, 180, 180, 90);
must have slipped through, thanks

and talking about
Quote
Are apples or beach balls better? There is no real comparison. Pointers can be local or global. Global variables can be pointers or non-pointers.
at least I remember that we used pointers (in highschool) instead of global variables ...
and
Quote
Rather than casting an int to a float, it would be cleaner to simply divide by a float (100.0) rather than an int (100)
Code:
servol.write(vall * (perl / 100.0) );
right ?

thanks
35  Using Arduino / Programming Questions / Re: mapping values on: October 22, 2012, 06:38:19 am
new version (saved about 50 lines in .hex file smiley)
Code:
//Written for atmega328-PU (16MHz external crystal)
//Compiled with arduino 1.0 (www.arduino.cc)

#include <Servo.h>

#define BAUD 9600       //UART baud rate
#define ECHOPINL 9      //Echo pin l = left
#define ECHOPINR 11     //Echo pin r = right
#define MINPULSE 600    //Minimum servo pulse
#define MAXPULSE 2400   //Maximum servo pulse
#define TRIGPINL 10     //Trig pin l
#define TRIGPINR 12     //Trig pin r

Servo servol;           //Creating servo object servol
Servo servor;           //Creating servo object servor

int holdtime = 15;
bool hold = false;
int vall, valr;
int perl, perr;
int prevperl = 0, prevperr = 0;

int sonic(int sensore, int sensort, int mincm, int maxcm)
{
  int per;
  long duration;
  digitalWrite(sensort, LOW);
  delay(2);
  digitalWrite(sensort, HIGH);
  delay(5);
  digitalWrite(sensort, LOW);
  duration = pulseIn(sensore, HIGH);
  per = ms_cm(duration);
  per = map(per, mincm, maxcm, 0, 100);
  return(per);
}

long ms_cm(long ms)
{
  return ms / 29 / 2;
}

void debugg()
{
  Serial.print(vall);
  Serial.print(" ");
  Serial.print(valr);
  Serial.println();
}

void setup()
{
  Serial.begin(BAUD);

  pinMode(ECHOPINL, INPUT);
  pinMode(ECHOPINR, INPUT);
  pinMode(TRIGPINL, OUTPUT);
  pinMode(TRIGPINR, OUTPUT);
}

void loop()
{
  perl = sonic(ECHOPINL, TRIGPINL, 20, 6);
  perr = sonic(ECHOPINR, TRIGPINR, 6, 20);

  if( (perl - prevperl) >= 5 )
  {
    perl = prevperl;
    hold=false;
  }

  if( (perr - prevperr) >= 5 )
  {
    perr = prevperr;
    hold=false;
  }

  vall = map(vall, 0, 100, 90, 180);
  valr = map(valr, 0, 180, 180, 90);

  servol.write(vall * ((float)perl / 100) );
  servor.write(valr * ((float)perl / 100) );
  if(hold==true) delay(holdtime);
  hold=true;
  debugg();
}

1. Is it better to use pointers or global variables?
2. Is my
Quote
servol.write(vall * ((float)perl / 100) );
valid?
3. Are there any other major issues with my sketch?
Thanks all for help..
36  Using Arduino / Programming Questions / Re: mapping values on: October 22, 2012, 05:41:26 am
Quote
but i'm unable to verify it (because I don't have my hardware here)
You need a PC running the Arduino IDE, NOT an Arduino, to verify code.

Come back when you have done that!

code is verified  smiley-red
i just need to know if i made right conversions of the data types (or if you know any good simulator that will let me simulate the program please give me a link)
37  Using Arduino / Programming Questions / Re: mapping values on: October 22, 2012, 04:58:07 am
k, thx, it wasn't my idea to make a compileable code in the first place...

However it's a good idea, nonetheless.

i appreciate your advice, but here's the new code, but i'm unable to verify it (because I don't have my hardware here)
but i would like if someone could skim trough the code and see if i made some stupidity (because i haven't programmed in months and because i'm not quite sure if i made right float to int conversions)...
Thanks, xvjeko

Code:
//Written for atmega328-PU (16MHz external crystal)
//Compiled with arduino 1.0 (www.arduino.cc)

#include <Servo.h>

#define BAUD 9600       //UART baud rate
#define ECHOPINL 9      //Echo pin l = left
#define ECHOPINR 11     //Echo pin r = right
#define MINPULSE 600    //Minimum servo pulse
#define MAXPULSE 2400   //Maximum servo pulse
#define TRIGPINL 10     //Trig pin l
#define TRIGPINR 12     //Trig pin r

Servo servol;           //Creating servo object servol
Servo servor;           //Creating servo object servor

int holdtime = 15;
bool hold = false;
int vall, valr;
float perl, perr;
float prevperl = 0, prevperr = 0;

int left(int d0, int d1)
{
  int cm;
  long duration;
  float percentage;
  digitalWrite(TRIGPINL, LOW);
  delay(2);
  digitalWrite(TRIGPINL, HIGH);
  delay(5);
  digitalWrite(TRIGPINL, LOW);
  duration = pulseIn(ECHOPINL, HIGH);
  cm = microsecondsToCentimeters(duration);
  percentage = map(cm, d0, d1, 0, 100);
  return( (float)percentage / 100);
}

int right(int d0, int d1)
{
  int cm;
  long duration;
  float percentage;
  digitalWrite(TRIGPINR, LOW);
  delay(2);
  digitalWrite(TRIGPINR, HIGH);
  delay(5);
  digitalWrite(TRIGPINR, LOW);
  duration = pulseIn(ECHOPINR, HIGH);
  cm = microsecondsToCentimeters(duration);
  percentage = map(cm, 20, 6, 90, 180);
  return( (float)percentage / 100);
}

long microsecondsToCentimeters(long microseconds)
{
  return microseconds / 29 / 2;
}

void debugg()
{
  Serial.print(vall);
  Serial.print(" ");
  Serial.print(valr);
  Serial.println();
}

void setup()
{
  Serial.begin(BAUD);

  pinMode(ECHOPINL, INPUT);
  pinMode(ECHOPINR, INPUT);
  pinMode(TRIGPINL, OUTPUT);
  pinMode(TRIGPINR, OUTPUT);
}

void loop()
{
  perl = left(20, 6);
  perr = right(6, 20);

  if( (perl - prevperl) >= (5/100) )
  {
    perl = prevperl;
    hold=false;
  }

  if( (perr - prevperr) >= (5/100) )
  {
    perr = prevperr;
    hold=false;
  }

  vall = perl * 100;
  vall = (int)vall;
  valr = perr * 100;
  valr = (int)valr;

  vall = map(vall, 0, 100, 90, 180);
  valr = map(valr, 0, 180, 180, 90);

  servol.write(vall * perl);
  servor.write(valr * perl);
  if(hold==true) delay(holdtime);
  hold=true;
  debugg();
}
38  Using Arduino / Programming Questions / Re: mapping values on: October 21, 2012, 08:14:10 am
Quote
my question is do you understand what i need to do ?
Making the controller of a mixing machine my kids are not allowed to use ...

The code you post is only a snippet that does not compile, so it is hard to improve on that.

I don't know if the use of % characters in variables is acceptable,
I would prefer written version like percJagermeister as the % sign is the modulo operator in C++

The remark of PaulS is a valid one.
In C++ there is a difference in the "comparison equals ==" and the "assign equals ="

a statement
if (right = 13)
assigns the value 13 to the var right and after that it evaluates to true; so the block {} is executed.

k, thx, it wasn't my idea to make a compileable code in the first place...
but i only needed to figure out the regulation part... but now when i think i solved it, i need an extra feature:
I wan't to be able to "fool" the system when i remove my hands from the line of ultrasonic sensor, so it remembers the recent position of the hands... anyone got an idea?

P.S redbull and jegermaister were first things that came on my mind (idk. maybee cuz of that im asking you to solve my problems) smiley-sad
39  Using Arduino / Programming Questions / Re: mapping values on: October 21, 2012, 07:47:09 am
Quote
I am thinking about smthing like this:
You might think about something that actually compiles, instead. == instead of = in some places, too.
my question is do you understand what i need to do ?
and if it is, may you verify the code i wrote and suggest improvements ?
40  Using Arduino / Programming Questions / Re: mapping values on: October 21, 2012, 07:23:10 am

You might use the multimap - http://arduino.cc/playground/Main/MultiMap - as I assume you have some non linear mapping for the ultrasonic sensors.




yes, this is true, i have some non linear mapping.... but first i want to make it to work in linear, just to test the hardware....
I am thinking about smthing like this:
Code:
int left, right; //lets assume this is the distance between the hand and the sensor in cm's
int %left, %right; //percentage
int redbull, jagermeister; //theese are the angles from 0 to 90 (corresponding to 0% and 100%) of the servo's

//lets assume that the distance of reading is from 6 cm and 20 cm
//so the left hand (20cm = 0%, 6cm = 100%), and right hand (6cm = 0% and 20cm = 100%)
//and on the servos 90 is closed and 180 is open

if(right=13) //right hand in the middle (13cm)
{
//the ratio is 1:1 so servos depend only on left hand;
servobull.write(180 * %left);
servojager.write(180 * %left);
}

if(right<13) //right hand goes bellow 13cm -->there will be more redbull than jagermeister
{
redbull = map(right, 12, 6, 90, 180);
jagermeister = map(right, 12, 6, 180, 90);
servobull.write(redbull * %left);
servojager.write(jagermaister * %left);
}

if(right>13) //right hand goes above 13cm -->there will be more jagermeister than redbull
{
redbull = map(right, 13, 20, 180, 90);
jagermeister = map(right, 13, 20, 90, 180);
servobull.write(redbull * %left);
servojager.write(jagermaister * %left);
}
41  Using Arduino / Programming Questions / Re: mapping values on: October 21, 2012, 06:09:27 am
Sorry but I have absolutely no idea what that diagram means. You might know but it is not clear.
The quality of an answer is only as good as the quality of the question.
Ok, the text version...
I have 2 substances (redbull and jagermeister)...
I have 2 ultrasonic sensors (one for left hand and one for right hand)...
With left hand i am controlling the amount of redbull and jagermeister that is going into glasses...
With right hand i am controlling the ratio of redbull and jagermeister that is going into glasses...
The ratio detirmined by the right hand depends on the value of the left hand.
for example if i have 80% red bull and 20% jager (detirmened with right hand), but left hand is at 0 % NOTHING will flow...
42  Using Arduino / Programming Questions / mapping values on: October 21, 2012, 05:28:06 am

can anyone write me a specific part of the code which maps inputs to outputs because i can't think any way of solving it. ty
43  Using Arduino / Programming Questions / Re: regulation on: October 20, 2012, 02:59:01 pm
I think the way of mine thinking is pretty clear, but if any doubts i can further explain...
It's not. "Picking the wrong values" doesn't really tell me your issue.

What is your sketch designed to do?

What values is the com port "picking"?

What values are you expecting and why?
sketch is designed to pick values from 2 ultrasonic sensors, and depending on the values picked,
operate 2 servo motors (motors need to recieve angles from 0 to 90, and im picking values from -400 to 800)
44  Using Arduino / Programming Questions / regulation on: October 20, 2012, 10:49:36 am
Good day, im having a problem with a program (my com port is picking wrong values)...
here's the code
Code:
#include <math.h>
#include <Servo.h>

//Constants
#define BAUD0 9600        //USB FTDI baud rate
#define ECHOPIN0 5        //Echo pin 0
#define ECHOPIN1 6        //Echo pin 1
#define MINPULSE 600     //Minimum servo pulse
#define MAXPULSE 2400   //Maximum servo pulse
#define TRIGPIN0 7         //Trig pin 0
#define TRIGPIN1 8         //Trig pin 1

Servo servo0;           //Creating servo object servo0
Servo servo1;           //Creating servo object servo1

//Variables
bool sdelay = false;
int delaytime = 50;
int cm0, precm0=1;
int cm1, precm1=1;
int val0, val1, maxdeg;
long duration0;
long duration1;

//PING DDC
long microsecondsToCentimeters(long microseconds)
{
  return microseconds / 29 / 2;
}

//DEBUGG
void debugg()
{
  Serial.print(val0);
  Serial.print(" ");
  Serial.print(val1);
  Serial.println();
}

//SETUP
void setup()
{
  Serial.begin(BAUD0);

  pinMode(TRIGPIN0, OUTPUT);
  pinMode(ECHOPIN0, INPUT);
  pinMode(TRIGPIN1, OUTPUT);
  pinMode(ECHOPIN1, INPUT);
  servo0.attach(9, MINPULSE, MAXPULSE);
  servo1.attach(10, MINPULSE, MAXPULSE);
}

//MAIN
void loop()
{
  //Ping sensor 0 reading
  digitalWrite(TRIGPIN0, LOW);
  delay(2);
  digitalWrite(TRIGPIN0, HIGH);
  delay(5);
  digitalWrite(TRIGPIN0, LOW);
  duration0 = pulseIn(ECHOPIN0, HIGH);
  cm0 = microsecondsToCentimeters(duration0);
  cm0 = map(cm0, 5, 20, 5, 20);

  //Ping sensor 1 reading
  digitalWrite(TRIGPIN1, LOW);
  delay(2);
  digitalWrite(TRIGPIN1, HIGH);
  delay(5);
  digitalWrite(TRIGPIN1, LOW);
  duration1 = pulseIn(ECHOPIN1, HIGH);
  cm1 = microsecondsToCentimeters(duration1);
  cm1 = map(cm1, 5, 20, 5, 20);

  //Mapping the readings to the corresponding servo angles
  maxdeg = map(cm0, 20, 5, 0, 90);
  val0 = map(cm1, 5, 20, maxdeg, 0);
  val1 = map(cm1, 5, 20, 0, maxdeg);
 
  switch(cm0)
  {
    case 20:
      if(20-precm0>=5)    //Hand removed quickly from the ultrasonic sensor (moved from the line of sensor reading)
      {
        servo0.write(val0);
        sdelay=true;
      }
      break;
     
    default:    //Default action (hand moved on the "line of sensor reading")
      servo0.write(val0);
      sdelay=true;
      precm0=cm0;
      break;
  }
  switch(cm1)
  {
    case 20:
      if(20-precm1>=5)
      {
        servo1.write(val1);
        sdelay=true;
      }
      break;
     
    default:
      servo1.write(val1);
      sdelay=true;
      precm1=cm1;
      break;
  }

  //Writing the values to the servos, applying the delays (if needed), and monitoring the results via UART
  servo0.write(val0);
  servo1.write(val1);
  if(sdelay==true) delay(delaytime);
  sdelay=false;

  debugg();
}

I think the way of mine thinking is pretty clear, but if any doubts i can further explain...
thanks  smiley
45  Using Arduino / Installation & Troubleshooting / Re: Upload Timeout Error on Mega 2560 - Not Sure What Changed on: October 17, 2012, 01:58:12 pm
OK, picked up the Mega2560 from my friend last night, and tested it out on the Lenovo Win7 32bit desktop and HP Win7 64bit laptop, and it works fine on both. The only variation from the Uno is the driver takes a long time to load (2-3 minutes - OK maybe not that long in the big scheme) and opening the serial monitor takes a while to reset the Mega - maybe 5 seconds. The Mega works well on our LED and LCD shield and takes only slightly longer to load.

So the issue looks like a Dell issue. Maybe if anyone has a dead Mega2560, please report the make, model, operating system, and any other details which are show when you right click on My Computer and select Properties.

i tossed my mega into junk (i only scalvaged some connectors...), if i will ever need a high end dev. platform, i will go for psoc, mbed, el.imp, chipkit or raspberry...  smiley-twist smiley-twist smiley-twist
Pages: 1 2 [3] 4 5 ... 7