Need some help using a arduino code

I need to make this code run on my spark core i have made the circuit and have tested it with blink a led code but i can get it to work as a roulette :confused:

and this is the code

Roulette code:

//www.kreativekiste.de

// Roulette code for arduino uno

#define time 30 // blink rate 
#define stop 10 
#define 500 // start the program starts

int pin [] = {} 3,4,5,6,7,8,9,10,11,12; // LED pin outputs 
tasterPin int = 0, taster value = 0; // Button pin 
int last number = 1;

void setup () { 
	randomSeed (analog read (2)); 
	for (int i = 0; i <10; i ++) 
	pinMode (pin [i], OUTPUT); 
} 

void loop () { 
	if (analog read (tasterPin)> start Reading) // button 
	game start (random (1 stop)); 
} 

void game start (int number) {// roulette start 
	digitalWrite (last number + 1, LOW); 
	last number = number; 
	int k = 1; 
	for (int i = 0; i <6; i ++) { 
		if (i> 2) k ++; 
		for (int j = 0; j <10; j ++) { 
			digitalWrite (pin [j], HIGH); 
			if ((i == 5) && (j + 1 == number)) break; 
			delay (time * k); 
			digitalWrite (pin [j], LOW); 
		} 
	} 
} // end and restart 

This is what i have done until now

//www.kreativekiste.de

// Roulette code for arduino uno

#define time 30 // blink rate 
#define stop 10 
#define 500 // start the program starts

int led = D0;
int led1 = D0;
int led2= D0;
int led3= D0;
int led4 = D0;
int led5 = D0;
int led6 = D0;
int led7 = D0;
int led8 = D0;
int led9 = D0;
buttonPin int = 0, button value = D5; // Button pin 
int last number = 1;

void setup () {    
	pinMode(led, OUTPUT);
	pinMode(led1, OUTPUT);
	pinMode(led2, OUTPUT);
	pinMode(led3, OUTPUT);
	pinMode(led4, OUTPUT);
	pinMode(led5, OUTPUT);
	pinMode(led6, OUTPUT);
	pinMode(led7, OUTPUT);
	pinMode(led8, OUTPUT);
	pinMode(led9, OUTPUT);
	pinMode(buttonPin, INPUT);

	randomSeed (analog read (2)); 
	for (int i = 0; i <10; i ++) 
	pinMode (pin [i], OUTPUT); 
} 

void loop () { 
	if (analog read (buttonPin)> start Reading) // button 
	game start (random (1 stop)); 
} 

void game start (int number) {// roulette start 
	digitalWrite (last number + 1, LOW); 
	last number = number; 
	int k = 1; 
	for (int i = 0; i <6; i ++) { 
		if (i> 2) k ++; 
		for (int j = 0; j <10; j ++) { 
			digitalWrite (pin [j], HIGH); 
			if ((i == 5) && (j + 1 == number)) break; 
			delay (time * k); 
			digitalWrite (pin [j], LOW); 
		} 
	} 
} // end and restart

Looking at the first part, I see every led is declared to pin D0. This means you have only 1 LED you are controlling, not sure if you want this behavior.

Second, your call of randomSeed (analog read (2)); is wrong, it should be randomSeed (analogRead(A2));
Same for other analog read calls since it should be analogRead(A0-A7).

Please read the docs for more info about these functions and the syntax of them.

1 Like

I tried a bit ,i guess ill try some more

I’d suggest you go through the code step by step, looking at the docs, and making sure the syntax is okay. The logic can be looked at after that, although that should be good. The way you wrote analogRead() is a good example of why you need to use the docs :wink:

It looks like you have several issues in your code, but there is a lot of help her for you!

Perhaps you can describe what it is that you want to do… I understand Roulette, I can’t understand what your code is trying to do.

Start with a basic explanation: When I push the button… I want this or that to happen…

@uzar I see you copied the code from a google translated German website. The translation completely messed it up! I’ve re-written it for you… study the code and figure out where to hook your LEDs and button based on the code. This compiles for me, and should work. You should power things from the 3V3 pin instead of a 5V source though.

// www.kreativekiste.de
// roulette Code für arduino uno
// Translated to English and Spark Core by BDub
//-------------------------------
#define TIME 30 // Blink Rate
#define TOTAL 10
#define START 500 // Program Start

int pin[] = {
    A1, A0, D0, D1, D2, D3, D4, D5, D6, D7
}; // LED Pin Outputs
int buttonPin = A2;
int floatingPin = A3; // Leave this pin unconnected
int value = 0;
int lastNumber = 1;

void gameStart(int num) { // roulette Start
    digitalWrite(lastNumber + 1, LOW);
    lastNumber = num;
    int k = 1;
    for (int i = 0; i < 6; i++) {
        if (i > 2) k++;
        for (int j = 0; j < 10; j++) {
            digitalWrite(pin[j], HIGH);
            if ((i == 5) && (j + 1 == num)) break;
            delay(TIME * k);
            digitalWrite(pin[j], LOW);
        }
    }
}

void setup() {
    randomSeed(analogRead(floatingPin));
    for (int i = 0; i < 10; i++)
        pinMode(pin[i], OUTPUT);
 }
 
void loop() {
    int tempVal = analogRead(buttonPin);
    if (tempVal > START) {
        gameStart( random(1, TOTAL) );
    }
}
3 Likes

Yes, this is my first go at spark with little arduino and c++ experience.
As fare as code goes ill use the BDub code he provided, im trying to make the roulette game that starts when the button is pushed and the leds start lighting up as if the roulette sphere is going through them and after some spins it stops at one led. I intend to put them in circle shape and solder them later when i have all working.
Thanks.

Wow, Thanks a lot :smile:

think about adding in a few shift registers and driving a bunch more LEDS

@uzar, don’t forget the amazing Spark Internet Button as well which gives you color LEDs as well! :stuck_out_tongue:

Yeah :slight_smile: though getting it to work is first

1 Like

I thought the original code seemed a bit weird how it operated. @uzar PM'd me for some help with the button... thought I'd reply here to keep the info flowing for all.

They are really only using the analog input as a digital function, so it would be better to just use it as a digital pin. Also the way it was originally written you needed to press the button to stop the game from starting over and over. I would change the loop as follows:

void loop() {
    if (digitalRead(buttonPin) == LOW) {
        gameStart( random(1, TOTAL) );
        while (digitalRead(buttonPin) == LOW); // wait here until the button is released
    }
}

You also need to add pinMode(buttonPin, INPUT); to the setup() code as follows:

void setup() {
    pinMode(buttonPin, INPUT);
    randomSeed(analogRead(floatingPin));
    for (int i = 0; i < 10; i++)
        pinMode(pin[i], OUTPUT);
}

Good luck! :smile:

I tried the new code but it does the same thing. Guess ill get a toggle switch :stuck_out_tongue:
This is how i added the code. Anyway thanks .

// www.kreativekiste.de
// roulette Code für arduino uno
// Translated to English and Spark Core by BDub
//-------------------------------
#define TIME 40 // Blink Rate
#define TOTAL 10
#define START 2000 // Program Start

int pin[] = {
    A1, A0, D0, D1, D2, D3, D4, D5, D6, D7
}; // LED Pin Outputs
int buttonPin = A2;
int floatingPin = A3; // Leave this pin unconnected
int value = 0;
int lastNumber = 1;

void gameStart(int num) { // roulette Start
    digitalWrite(lastNumber + 1, LOW);
    lastNumber = num;
    int k = 1;
    for (int i = 0; i < 6; i++) {
        if (i > 2) k++;
        for (int j = 0; j < 10; j++) {
            digitalWrite(pin[j], HIGH);
            if ((i == 5) && (j + 1 == num)) break;
            delay(TIME * k);
            digitalWrite(pin[j], LOW);
        }
    }
}

void setup() {
    pinMode(buttonPin, INPUT);
    randomSeed(analogRead(floatingPin));
    for (int i = 0; i < 10; i++)
        pinMode(pin[i], OUTPUT);
}
 
void loop() {
    if (digitalRead(buttonPin) == LOW) {
        gameStart( random(1, TOTAL) );
        while (digitalRead(buttonPin) == LOW); // wait here until the button is released
    }
}