newbie from picaxe and a simple problem!

hi all,

i've decided to move on from Picaxe and give Arduino ago. I'm trying to do a simple program which would take about 5 mins on the Picaxe but am lost when i comes to the Arduino.

I am after one button to one PWM ouput.

basically one button, press it once and 1/4 pwm then second 1/2 pwm, etc, then off.

On the Picaxe i would define little subroutines and jump back and forth (gosub or goto in spectrum basic) but cant find how to do this on the Arduino.

please come to my rescue

The Arduino IDE is packed full of examples.

basically one button, press it once and 1/4 pwm then second 1/2 pwm, etc, then off.

The state change detection example is a perfect starting point.

vectraboyv6:
On the Picaxe i would define little subroutines and jump back and forth (gosub or goto in spectrum basic) but cant find how to do this on the Arduino.

If you write the program as you would for the Picaxe and post it we can explain what needs to be different.

In C++ a subroutine is called a function.

You may find some ideas in Planning and Implementing a Program

...R

thank you.. will post it in the morning... beer has been consumed now /;0

Right here's my picaxe code which works 100%. in picaxe i can goto subroutines with main: or third: etc i cant find a way to do this in arduino.. many thanks

; mirror gesture brightness control
; version 3
; :slight_smile:

w0 = 2500 ; wait time
b10 = 8 ; ramp up pause
b12 = 5 ; ramp down pause

main:

if pinb.5 = 1 then goto third
goto main

third:
;soft ramp up loop
for b9 = 0 to 80
pwmout pwmdiv64, C.0, 64, b9
pause b10
next b9
pause w0

waitswitch:

if pinb.5 =1 then goto twothirds
goto waitswitch

twothirds:

;soft ramp up loop
for b9 = 81 to 159
pwmout pwmdiv64, C.0, 64, b9
pause b10
next b9
pause w0

waitswitchone:

if pinb.5 =1 then goto full
goto waitswitchone

full:

;soft ramp up loop
for b9 = 160 to 255
pwmout pwmdiv64, C.0, 64, b9
pause b10
next b9
pause w0

totaloff:

if pinb.5 =1 then goto finnish
goto totaloff

finnish:

;soft ramp down loop
for b9 = 254 to 0 step -1
pwmout pwmdiv64, C.0, 64, b9
pause b12
next b9
pause w0
goto main

in picaxe i can goto subroutines

In C we call functions (subroutines).
We never goto.
Not in polite society, at least.

vectraboyv6:
On the Picaxe i would define little subroutines and jump back and forth (gosub or goto in spectrum basic) but cant find how to do this on the Arduino.

void setup ()
{
}


void loop ()
{
  first();   // function call
  second();  // function call
  third();   // function call
  recycle(); // function call
}

// ----------------------------

void first()
{
  Serial.println("1");
}

void second()
{
  Serial.println("2");
}

void third()
{
  Serial.println("3");
}

void recycle()
{
  Serial.println("Please wait...");
  delay(2000);
}

That is a very good example of why using goto is not a good idea if you want to make the flow of program easy to follow.

What would it look like written using gosub on the picaxe rather than goto, as this will be closer to how you would write it in C++ on the Arduino.

ok thank you for the repiles... im off to amazon to buy a arduino book me thinks :slight_smile:

Take a look at the simple blink example and try to understand it.

Basically you are learning a new language.

Wow… I had forgotten just how awful PICAXE coding was!

@vectraboyv6 you will never look back.

Here is a quite literal translation, to help you understand the equivalent commands. But by Arduino standards, it is still bad coding.

// mirror gesture brightness control
// version 3
// :)

#define PWM_OUTPUT_PIN 5
#define BUTTON_PIN 2
#define WAIT_TIME 2500
#define RAMP_UP_PAUSE 8
#define RAMP_DOWN_PAUSE 5

void setup() {
  pinMode(PWM_OUTPUT_PIN, OUTPUT);
}

void loop() {

  while (digitalRead(BUTTON_PIN) == LOW);

  //soft ramp up loop
  for (int i = 0; i <= 80; i++) {
    analogWrite(PWM_OUTPUT_PIN, i);
    delay(RAMP_UP_PAUSE);
  }
  delay(WAIT_TIME);

  while (digitalRead(BUTTON_PIN) == LOW);

  //soft ramp up loop
  for (int i = 81; i <= 159; i++) {
    analogWrite(PWM_OUTPUT_PIN, i);
    delay(RAMP_UP_PAUSE);
  }
  delay(WAIT_TIME);

  while (digitalRead(BUTTON_PIN) == LOW);

  //soft ramp up loop
  for (int i = 160; i <= 255; i++) {
    analogWrite(PWM_OUTPUT_PIN, i);
    delay(RAMP_UP_PAUSE);
  }
  delay(WAIT_TIME);

  while (digitalRead(BUTTON_PIN) == LOW);

  //soft ramp down loop
  for (int i = 254; i >= 0; i--) {
    analogWrite(PWM_OUTPUT_PIN, i);
    delay(RAMP_DOWN_PAUSE);
  }
  delay(WAIT_TIME);

}

And here is a shorter and slightly better version

// mirror gesture brightness control
// version 3
// :)

#define PWM_OUTPUT_PIN 5
#define BUTTON_PIN 2
#define WAIT_TIME 2500
#define RAMP_UP_PAUSE 8
#define RAMP_DOWN_PAUSE 5

void setup() {
  pinMode(PWM_OUTPUT_PIN, OUTPUT);
}

void ramp(int first, int last, int stepDir, int pause) {

  while (digitalRead(BUTTON_PIN) == LOW);

  //soft ramp up/down loop
  for (int i = first; i != last; i += stepDir) {
    analogWrite(PWM_OUTPUT_PIN, i);
    delay(pause);
  }
  delay(WAIT_TIME);

}

void loop() {

  ramp(0, 79, 1, RAMP_UP_PAUSE);
  ramp(80, 159, 1, RAMP_UP_PAUSE);
  ramp(160, 255, 1, RAMP_UP_PAUSE);
  ramp(254, 0, -1, RAMP_DOWN_PAUSE);

}