Shifting code from PICAXE to Arduino

Good morning all,
I am a 9th grader working on a science project (www.instructables.com/id/Air-quality-balloons/) where I built a sensor to detect VOCs and an LED to react to the data to turn green, yellow, or red based on the low, average, and high values. I am trying to shift code from PICAXE Editor to Ardunio but it doesn’t cross over well.

PICAXE code:

main:
'w5 = 448 ‘VOC GREEN’
'w6 = 470 ‘VOC RED’

goto runsensor

runsensor:
readadc10 4, w3’SENSOR VALUE’
if w3 < w5 then ‘GREEN’
high 1
low 2
endif

if w3 >= w5 and w3 < w6 then ‘YELLOW’
high 1
High 2

if w3 >= w6 the ‘RED’
low 1
high 2
endif
pause 500
goto runsensor

Any ideas??? Thanks! (ps this code I got from the instructables website so I am hoping it is correct; I am shifting from PICAXE because PICAXE doesn’t want to read the Tiny AVR Programmer that I purchased) doesn’t want

but it doesn't cross over well.

Of course it doesn't. The picaxe is not programmed using C++. The Arduino is.

Any ideas???

Ditch the whole idea of trying to port the code. Understand what it actually does, and write the same thing in C++. Or, get the hardware that the destructable site actually used.

goto runsensor

runsensor:

Now I know the square root of zip about Picaxe, but that looks like an awfully suspect construct

C++ got it, still confusing

AWOL:

goto runsensor

runsensor:


Now I know the square root of zip about Picaxe, but that looks like an awfully suspect construct

The first goto is useless, but there is another one at the bottom of the posted code.

can anyone help me with this C++ coding? i want the LED to change color based on the sensor reading...

Start by looking at the examples in the IDE. They will show you how to read the state of a pin (either analogue or digital) and drive an LED pin HIGH or LOW.

Write out the logic of your program in pseudo code before writing it for real.

thanks! :)

so ultimately i want:

if sensor value is less than or equal to 448 then the LED turns green

if sensor value is greater than 448 and less than 470 then the LED turns yellow

if sensor value is greater than or equal to 470 then the LED turns red

OK, you've got the basis of a spec, so see if you can translate that to C/C++

So, you need to find out how to read an analogue sensor, how to compare the value returned to some constants and how to turn on LEDs

Here is an outline

declare global variables such as sensible names for pins

start of setup()
  set the pinModes() for the pins
  start the Serial interface to allow debug messages to be output if required
end of setup()

start of loop()
  read value from sensor
  if value less than 449
    turn on green LED
  else
  if value is less than 471
    turn on the yellow LED
  else
  if value is greater than 470 (flaw in the logic here based on your description)
    turn on the red LED
  end if
end of loop()

What conditions turn the LEDs off ?

oh meant if the sensor value was between 448 and 470 (not equal to 470) then the led would turn yellow...

i want the LEDs to stay on and the color they turn. My goal is that the LEDs will be inside a balloon and the balloon will turn the color of the LED and I can take pictures of them

Using the examples you should be able to work out how to implement the program using the Arduino as all the functions you require are illustrated in them as is the syntax and program layout required for the Arduino environment.

start of setup(){ set the pinModes() for the pins start the Serial interface to allow debug messages to be output if required end of setup()

}

start of loop() read value from sensor if value less than 449 turn on green LED else if value is greater than 449 and less than 471 turn on the yellow LED else if value is greater than 471 turn on the red LED end if end of loop()

}

GOT ERROR:

Arduino: 1.8.4 (Windows 7), Board: "Arduino/Genuino Uno"

sketch_nov16b:1: error: 'startof' does not name a type

start of setup(){

^

sketch_nov16b:1: error: 'start' does not name a type

start of setup(){

^

sketch_nov16b:8: error: 'start' does not name a type

start of loop()

^

sketch_nov16b:21: error: expected declaration before '}' token

}

^

exit status 1 'startof' does not name a type

This report would have more information with "Show verbose output during compilation" option enabled in File -> Preferences.

The setup() function is declared:

void setup()
{
}

That was pseudo-code that you were supposed to arrange into real code.

i am having a lot of trouble understanding how to set the pinmodes.... the tutorials have examples using LEDs but don't show the range. What am I missing? :(

What am I missing?

A clue?

The pinMode() function sets the mode, INPUT, OUTPUT, or INPUT_PULLUP for ONE pin. There are no ranges involved.

Can someone see if this would work for what I am trying to do? (Again I am trying to get a tri-colored LED to display green, yellow, red, based on a sensor’s value (g<=448; 449<y<470; r >= 470) Many thank yous!!!

const int sensorPin = A0;
const int ledPin = 8;

int sensorValue = 0;
int sensorMin = 448;
int sensorMax = 470;

void setup() {
pinMode(1, OUTPUT);
digitalWrite(1, HIGH);

while (millis() < 5000) {
sensorValue = analogRead(sensorPin);

if (sensorValue > sensorMax){
sensorMax = sensorValue;
}

if (sensorValue < sensorMin) {
sensorMin = sensorValue;
}
}

digitalWrite(1, LOW);
}

void loop() {
sensorValue = analogRead(sensorPin);

sensorValue = map(sensorValue, sensorMin, sensorMax, 0, 255);

sensorValue = constrain(sensorValue, 0, 255);

analogWrite(ledPin, sensorValue);
}

Can someone see if this would work for what I am trying to do?

At first sight it will not do what you want.

What type of LED are you using and how are the 3 colours produced ?

Note that you would be well advised not to use digital pin 1 in case Serial output is needed for debugging later.

analogWrite to a non-PWM pin isn’t recommended.

Use of code tags when posting code is.