Need some help with code for bicycle POV.

Hello there!
My name is Shan and I'm currently working on a high-school project. The idea is to create a Persistence of Vision effect using a set of maybe 5 LED-bars attached to the strings on a bicycle-wheel. With the help of a laser sensor that counts upwards everytime a wire breaks the beam i will be able to make the lights turn on for a few ms so that it gives the illusion of the bicycle wheel actually standing still.

Im very new to C++ and programming in general so I don't really know if the current code below would work. Some feedback and / or help would be greatly appreciated!

Code:

void setup(){
const int wireTotal = 70;
const int LEDs = 5;
const int LED1 = 0;
const int LED2 = 1;
const int LED3 = 2;
const int LED4 = 3;
const int LED5 = 4;
const int Laser = 5;
int wire = 0;

pinMode (LED1, OUTPUT);
pinMode (LED2, OUTPUT);
pinMode (LED3, OUTPUT);
pinMode (LED4, OUTPUT);
pinMode (LED5, OUTPUT);
pinMode (Laser, INPUT);
}

void loop(){
if (Laser = LOW){
wire++;
if (wire = (wireTotal / LEDs)){ //Detta går att förenkla när jag väl vet hur många slingor
digitalWrite(LED1, HIGH); //och stänger jag kommer att ha. I framtiden kan jag bara
digitalWrite(LED2, HIGH); //skriva villkoret som ett tal i stället för en operation.
digitalWrite(LED3, HIGH);
digitalWrite(LED4, HIGH);
digitalWrite(LED5, HIGH);
delay(5);
digitalWrite(LED1, LOW);
digitalWrite(LED2, LOW);
digitalWrite(LED3, LOW);
digitalWrite(LED4, LOW);
digitalWrite(LED5, LOW);
}
if (wire > wireTotal){
wire == 0;
}
}
}

  if (Laser = LOW){
    if (wire = (wireTotal / LEDs)){    //Detta går att förenkla när jag väl vet hur många slingor
      wire == 0;

No. You need to thoroughly understand the differences between the = operator and the == operator, and consistently use the correct one. You have them reversed here.

Of course! The "=" is used to assign values and the "==" returns true if it equals on both sides right?

I have now made some corrections in my code.

 if (Laser == LOW){
    wire++;
    if (wire == (wireTotal / LEDs)){

and

 if (wire > wireTotal){
      wire = 0;

When I verify the code in the Arduino SDK I get these errors:

 sketch_sep13a.cpp: In function 'void loop()':
sketch_sep13a:20: error: 'Laser' was not declared in this scope
sketch_sep13a:21: error: 'wire' was not declared in this scope
sketch_sep13a:22: error: 'wireTotal' was not declared in this scope
sketch_sep13a:22: error: 'LEDs' was not declared in this scope
sketch_sep13a:23: error: 'LED1' was not declared in this scope
sketch_sep13a:24: error: 'LED2' was not declared in this scope
sketch_sep13a:25: error: 'LED3' was not declared in this scope
sketch_sep13a:26: error: 'LED4' was not declared in this scope
sketch_sep13a:27: error: 'LED5' was not declared in this scope
sketch_sep13a:35: error: 'wireTotal' was not declared in this scope

I find this very strange as I already declared the variables in the void setup()...

You can read about scope of variables here:

Your vaiables are not global.

Thanks for the help guys! My new code now looks like this:

const int wireTotal = 70;
const int LEDs = 5;
const int LED1 = 0;
const int LED2 = 1;
const int LED3 = 2;
const int LED4 = 3;
const int LED5 = 4;
const int Laser = 5;
int wire = 0;

void setup(){
  pinMode (LED1, OUTPUT);
  pinMode (LED2, OUTPUT);
  pinMode (LED3, OUTPUT);
  pinMode (LED4, OUTPUT);
  pinMode (LED5, OUTPUT);
  pinMode (Laser, INPUT);
}

void loop(){
  if (digitalRead(Laser)==LOW){
    wire++;
    if (wire == (wireTotal / LEDs)){    //Detta går att förenkla när jag väl vet hur många slingor 
      digitalWrite(LED1, HIGH);        //och stänger jag kommer att ha. I framtiden kan jag bara 
      digitalWrite(LED2, HIGH);        //skriva villkoret som ett tal i stället för en operation.
      digitalWrite(LED3, HIGH);
      digitalWrite(LED4, HIGH);
      digitalWrite(LED5, HIGH);
      delay(5);
      digitalWrite(LED1, LOW);
      digitalWrite(LED2, LOW);
      digitalWrite(LED3, LOW);
      digitalWrite(LED4, LOW);
      digitalWrite(LED5, LOW);
    }
    if (wire > wireTotal){
      wire = 0;
    }
  }
}

The Arduino SDK compiled it succesfully (yay). Now I just have to see if this code would work in practice...

    wire++;
    if (wire == (wireTotal / LEDs)){

Each pass through loop, increment wire. If wire is 14, do something.

Then,

    if (wire > wireTotal){
      wire = 0;

When wire gets to 71, reset to 0.

I guess I am missing something.

It seems to me that you should have an series of LED states, for each of several values of wire, and that yout should reset wire at some different value.

Code that compiles is completely different from code that works properly.

PaulS, yes I saw that when I loaded the code into the Simulator for Arduino. Quite a stupid misstake :stuck_out_tongue:

Now my code runs and looks like this: