Passing a string value to a function - How?
oakguy
Posts: 21
Guys
I am working with my activity bot and was building this and playing with using function calls for the first time. All my functions worked except when I realized I did not know how to instantiate a string variable in Propeller C. In my Void Display function I wanted to pass a string value as to where my servo was looking. I realize that I could do it an easier way. I was just trying to pass a string value. But I could not even create one. Functions sure do clean up your code.
void Display(string pointer,int cmDist)
{
print("pointer = %d, cmDist = %d\n", pointer, cmDist);
}
Thanks
Oakguy
#include "simpletools.h"
#include "servo.h"
#include "ping.h"
#include "wavplayer.h"
int main()
{
int delay1 = 1000;
int delay2 = 3000;
int cmDist = 0;
pointer = ("Forward");
//Opening Wave file and Ping forward
PlayWav();
servo_angle(17, 900);
pause(2000);
while(1)
{
servo_angle(17, 0);
pause(delay1);
cmDist = ping_cm(8);
Buzzer();
pointer = "Right";
Display(pointer, cmDist);
pause(delay2);
servo_angle(17, 900);
pause(delay1);
cmDist = ping_cm(8);
Buzzer();
pointer = "Forward";
Display(pointer, cmDist);
pause(delay2);
servo_angle(17, 1800);
pause(delay1);
cmDist = ping_cm(8);
Buzzer();
pointer = "Left";
Display(pointer, cmDist);
pause(delay2);
servo_angle(17, 900);
pause(delay1);
cmDist = ping_cm(8);
Buzzer();
pointer = "Forward";
Display(pointer, cmDist);
pause(delay2);
}
}
void Display(string pointer,int cmDist)
{
print("pointer = %d, cmDist = %d\n", pointer, cmDist);
}
void PlayWav(void)
{
int DO = 22, CLK = 23, DI = 24, CS = 25;
int Buzzer = 4, Duration = 250, Frequency = 4000;
//Mount the SD Card
sd_mount(DO, CLK, DI, CS);
const char techloop[] = {"techloop.wav"};
//play intro sound file
wav_play(techloop);
wav_volume(9);
pause(3500);
wav_stop();
}
void Buzzer(void)
{
int Buzzer = 4, Duration = 150, Frequency = 4000;
freqout(Buzzer, Duration, Frequency); //pin, duration, frequency
}
I am working with my activity bot and was building this and playing with using function calls for the first time. All my functions worked except when I realized I did not know how to instantiate a string variable in Propeller C. In my Void Display function I wanted to pass a string value as to where my servo was looking. I realize that I could do it an easier way. I was just trying to pass a string value. But I could not even create one. Functions sure do clean up your code.
void Display(string pointer,int cmDist)
{
print("pointer = %d, cmDist = %d\n", pointer, cmDist);
}
Thanks
Oakguy
#include "simpletools.h"
#include "servo.h"
#include "ping.h"
#include "wavplayer.h"
int main()
{
int delay1 = 1000;
int delay2 = 3000;
int cmDist = 0;
pointer = ("Forward");
//Opening Wave file and Ping forward
PlayWav();
servo_angle(17, 900);
pause(2000);
while(1)
{
servo_angle(17, 0);
pause(delay1);
cmDist = ping_cm(8);
Buzzer();
pointer = "Right";
Display(pointer, cmDist);
pause(delay2);
servo_angle(17, 900);
pause(delay1);
cmDist = ping_cm(8);
Buzzer();
pointer = "Forward";
Display(pointer, cmDist);
pause(delay2);
servo_angle(17, 1800);
pause(delay1);
cmDist = ping_cm(8);
Buzzer();
pointer = "Left";
Display(pointer, cmDist);
pause(delay2);
servo_angle(17, 900);
pause(delay1);
cmDist = ping_cm(8);
Buzzer();
pointer = "Forward";
Display(pointer, cmDist);
pause(delay2);
}
}
void Display(string pointer,int cmDist)
{
print("pointer = %d, cmDist = %d\n", pointer, cmDist);
}
void PlayWav(void)
{
int DO = 22, CLK = 23, DI = 24, CS = 25;
int Buzzer = 4, Duration = 250, Frequency = 4000;
//Mount the SD Card
sd_mount(DO, CLK, DI, CS);
const char techloop[] = {"techloop.wav"};
//play intro sound file
wav_play(techloop);
wav_volume(9);
pause(3500);
wav_stop();
}
void Buzzer(void)
{
int Buzzer = 4, Duration = 150, Frequency = 4000;
freqout(Buzzer, Duration, Frequency); //pin, duration, frequency
}
Comments
The output is:
char s[1] = "Right";
Display(s, cmDist);
char s[2] = "Forward";
Display(s, cmDist);
char s[3] = "Left";
Display(s, cmDist);
Oakguy
http://forums.parallax.com/showthread.php/152879-How-to-use-XBee-with-Activity-Board?p=1232211&viewfull=1#post1232211