Hi All,
I recieved my Imp yesterday. And I love it! I can put my Arduino on eBay now. I am trying to translate all Arduino c++ codes to my Imp. But I am a complete Squirrrel newbie. I have only some basic C++ knowledge.
One of my code doesn’t want to be translated.
I tried to make a remote IR control for my kids to control their Technic Lego models.
For instance http://www.youtube.com/watch?v=hjmaHYJ3NY0
I have this code running perfectly on my Arduino Uno.
This is my Squirrel code:
http://pastebin.com/KJQjT1M9
or
`
// configure the imp (best practice)
imp.configure(“Lego IR Remote”, [], []);
// create a global variabled called led,
// and assign pin9 to it
led <- hardware.pin9;
toggle <- [0,0,0,0];
// create a global variable to store current
// state of the LED
state <- 0;
s2ms <- 0.000001;
//The used Global variabels
PWM_FWD7 <- 0x7;
PWM_FLT <- 0x0;
//channel
CH1 <- 0x0;
//output
RED <- 0x0;
// configure led to be a digital output
led.configure(DIGITAL_OUT);
// create a global variable to store current
// state of the LED
state <- 0;
function blink() {
// invert the value of state:
// when state = 1, 1-1 = 0
// when state = 0, 1-0 = 1
state = 1-state;
// sWITCHING IR COMMANDS
server.log("Starting motor");
SingleOutput(PWM_FWD7,RED,CH1);
// schedule imp to wakeup in .5 seconds and do it again.
imp.sleep(5);
server.log("Stopping motor");
SingleOutput(PWM_FLT, RED,CH1);
// schedule imp to wakeup in .5 seconds and do it again.
imp.wakeup(5, blink);
}
function SingleOutput(pwm,output,channel) {
server.log(“Sending IR Commands…”)
SINGLE_OUTPUT <- 0x4;
nib1 <- toggle[channel] | channel;
nib2 <- SINGLE_OUTPUT | output;
nib3 <- pwm;
nib4 <- 0xf ^ nib1 ^ nib2 ^ nib3;
for (local i=0; i<6; i++)
{
message_pause(channel, i);
pf_send(nib1 << 4 | nib2, nib3 << 4 | nib4);
}
if(toggle[channel] == 0)
toggle[channel] = 8;
else
toggle[channel] = 0;
server.log(“ready”)
}
function start_pause()
{
imp.sleep(1014*s2ms);
}
function high_pause()
{
imp.sleep(546*s2ms);
}
function low_pause()
{
imp.sleep(260*s2ms);
}
function tx_pause()
{
imp.sleep(156*s2ms);
}
function message_pause(channel, count)
{
a <- 0;
if(count == 0)
a = 4 - channel + 1;
else if(count == 1 || count == 2)
a = 5;
else if(count == 3 || count == 4)
a = 5 + (channel + 1) * 2;
imp.sleep(a * 77 * s2ms);
}
function pf_send(code1, code2)
{
x <- 128;
start_stop_bit();
while (x)
{
oscillationWrite(156 * s2ms);
if (code1 & x) //high bit
high_pause();
else //low bit
low_pause();
x = x >> 1; //next bit
}
x = 128;
while (x)
{
oscillationWrite(156*s2ms);
if (code2 & x) // high bit
high_pause();
else //low bit
low_pause();
x = x >> 1; //next bit
}
start_stop_bit();
}
function start_stop_bit()
{
oscillationWrite(156*s2ms);
start_pause();
}
function oscillationWrite(time) {
for( local i=0; i <= time/26; i++) {
led.write(1)
imp.sleep(13*s2ms);
led.write(0)
imp.sleep(13*s2ms);
}
}
// start the loop
blink();
`
Here is my Arduino code:
http://pastebin.com/E2N8KW2j
or
`//mode
#define SINGLE_OUTPUT 0x4
//PWM speed steps
#define PWM_FLT 0x0
#define PWM_FWD7 0x7
//channel
#define CH1 0x0
//output
#define RED 0x0
int IRPin = 13;
int toggle[4] = {0,0,0,0};
void setup()
{
pinMode(IRPin, OUTPUT);
digitalWrite(IRPin, LOW);
}
void loop()
{
SingleOutput(PWM_FWD7, RED, CH1);
delay(2000);
SingleOutput(PWM_FLT, RED, CH1);
delay(2000);
}
void pf_send(int code1, int code2)
{
int x = 128;
start_stop_bit();
while (x)
{
oscillationWrite(IRPin, 156);
if (code1 & x) //high bit
high_pause();
else //low bit
low_pause();
x >>= 1; //next bit
}
x = 128;
while (x)
{
oscillationWrite(IRPin, 156);
if (code2 & x) // high bit
high_pause();
else //low bit
low_pause();
x >>= 1; //next bit
}
start_stop_bit();
delay(10);
}
void SingleOutput(int pwm, int output, int channel)
{
int nib1, nib2, nib3, nib4, i;
//set nibs
nib1 = toggle[channel] | channel;
nib2 = SINGLE_OUTPUT | output;
nib3 = pwm;
nib4 = 0xf ^ nib1 ^ nib2 ^ nib3;
for(i = 0; i < 6; i++)
{
message_pause(channel, i);
pf_send(nib1 << 4 | nib2, nib3 << 4 | nib4);
}
if(toggle[channel] == 0)
toggle[channel] = 8;
else
toggle[channel] = 0;
}
void start_pause()
{
delayMicroseconds(1014);
}
void high_pause()
{
delayMicroseconds(546);
}
void low_pause()
{
delayMicroseconds(260);
}
void tx_pause()
{
delayMicroseconds(156);
}
void message_pause(int channel, int count)
{
unsigned char a = 0;
if(count == 0)
a = 4 - channel + 1;
else if(count == 1 || count == 2)
a = 5;
else if(count == 3 || count == 4)
a = 5 + (channel + 1) * 2;
delayMicroseconds(a * 77);
}
void start_stop_bit()
{
oscillationWrite(IRPin, 156);
//digitalWrite(IRPin, HIGH);
//tx_pause();
//digitalWrite(IRPin, LOW);
start_pause();
}
void oscillationWrite(int pin, int time) {
for(int i = 0; i <= time/26; i++) {
digitalWrite(pin, HIGH);
delayMicroseconds(13);
digitalWrite(pin, LOW);
delayMicroseconds(13);
}
}`
I don’t know what I have done wrong. The Arduino code is working great. The Squirrel/Imp code doesn’t react at all.
Please help. I am lost here.
Thanks
Jeroen :-*