Permalänk

Error i Ardino kod

Tjena, har ganska nyligen köpt en ardino uno och börjat moddat lite med den. Har nu försökt "mixa" tre st exempel projekt. Problemet är att inte men koden vill inte fungera, så undrar om nån här kan kolla och rätta till min kod

/*CODE 15 modded by kagstrom2100.*/ //LED Pin Variables int ledpin_1 = 9; int ledpin_2 = 5; int ledpin_3 = 4; int ledpin_4 = 3; int ledpin_5 = 2; //An array to hold the pin each LED is connected to int sensorPin = A0; int sensorValue = 0; int ledpin(); //i.e. LED #0 is connected to pin 2, LED #1, 3 and so on //to address an array use ledpin[0] this would equal 2 //and ledpin[7] would equal 9 /* * setup() - this function runs once when you turn your Arduino on * We the three control pins to outputs */ void setup(); //Set each pin connected to an LED to output mode (pulling high (on) or low (off) for(int i = 0; i < 9; i++){ //this is a loop and will repeat eight times pinMode(ledpin[i],OUTPUT); //we use this to set each LED pin to output } //the code this replaces is below void loop() // run over and over again { oneAfterAnotherNoLoop(); //this will turn on each LED one by one then turn each off //oneAfterAnotherLoop(); //does the same as oneAfterAnotherNoLoop but with //much less typing //oneOnAtATime(); //this will turn one LED on then turn the next one //on turning the //former off (one LED will look like it is scrolling //along the line //inAndOut(); //lights the two middle LEDs then moves them out then back //in again } /* * oneAfterAnotherNoLoop() - Will light one LED then delay for delayTime then light * the next LED until all LEDs are on it will then turn them off one after another * * this does it without using a loop which makes for a lot of typing. * oneOnAtATimeLoop() does exactly the same thing with less typing */ void oneAfterAnotherNoLoop(){ int delayTime = 100; //the time (in milliseconds) to pause between LEDs //make smaller for quicker switching and larger for slower digitalWrite(ledpin5[0], HIGH); //Turns on LED #0 (connected to pin 2 ) delay(delayTime); //waits delayTime milliseconds digitalWrite(ledpin4[1], HIGH); //Turns on LED #1 (connected to pin 3 ) delay(delayTime); //waits delayTime milliseconds digitalWrite(ledpin3[2], HIGH); //Turns on LED #2 (connected to pin 4 ) delay(delayTime); //waits delayTime milliseconds digitalWrite(ledpin2[3], HIGH); //Turns on LED #3 (connected to pin 5 ) delay(delayTime); //waits delayTime milliseconds digitalWrite(ledpin[4], HIGH); //Turns on LED #4 (connected to pin 6 ) delay(delayTime); //waits delayTime milliseconds digitalWrite(ledpin[5], HIGH); //Turns on LED #5 (connected to pin 7 ) delay(delayTime); //waits delayTime milliseconds digitalWrite(ledpin[6], HIGH); //Turns on LED #6 (connected to pin 8 ) delay(delayTime); //waits delayTime milliseconds digitalWrite(ledpin1[7], HIGH); //Turns on LED #7 (connected to pin 9 ) delay(delayTime); //waits delayTime milliseconds //Turns Each LED Off digitalWrite(ledpin5[7], LOW); //Turns on LED #0 (connected to pin 2 ) delay(delayTime); //waits delayTime milliseconds digitalWrite(ledpin4[6], LOW); //Turns on LED #1 (connected to pin 3 ) delay(delayTime); //waits delayTime milliseconds digitalWrite(ledpin3[5], LOW); //Turns on LED #2 (connected to pin 4 ) delay(delayTime); //waits delayTime milliseconds digitalWrite(ledpin2[4], LOW); //Turns on LED #3 (connected to pin 5 ) delay(delayTime); //waits delayTime milliseconds digitalWrite(ledpin[3], LOW); //Turns on LED #4 (connected to pin 6 ) delay(delayTime); //waits delayTime milliseconds digitalWrite(ledpin[2], LOW); //Turns on LED #5 (connected to pin 7 ) delay(delayTime); //waits delayTime milliseconds digitalWrite(ledpin[1], LOW); //Turns on LED #6 (connected to pin 8 ) delay(delayTime); //waits delayTime milliseconds digitalWrite(ledpin5[0], LOW); //Turns on LED #7 (connected to pin 9 ) delay(delayTime); //waits delayTime milliseconds } /* * oneAfterAnotherLoop() - Will light one LED then delay for delayTime then light * the next LED until all LEDs are on it will then turn them off one after another * * this does it using a loop which makes for a lot less typing. * than oneOnAtATimeNoLoop() does exactly the same thing with less typing */ void oneAfterAnotherLoop(){ int delayTime = 100; //the time (in milliseconds) to pause between LEDs //make smaller for quicker switching and larger for slower //Turn Each LED on one after another for(int i = 0; i <= 7; i++){ digitalWrite(ledpin[i], HIGH); //Turns on LED #i each time this runs i delay(delayTime); //gets one added to it so this will repeat } //8 times the first time i will = 0 the final //time i will equal 7; //Turn Each LED off one after another for(int i = 7; i >= 0; i--){ //same as above but rather than starting at 0 and counting up //we start at seven and count down digitalWrite(ledpin[i], LOW); //Turns off LED #i each time this runs i delay(delayTime); //gets one subtracted from it so this will repeat } //8 times the first time i will = 7 the final //time it will equal 0 } /* * oneOnAtATime() - Will light one LED then the next turning off all the others */ void oneOnAtATime(){ int delayTime = 100; //the time (in milliseconds) to pause between LEDs //make smaller for quicker switching and larger for slower for(int i = 0; i <= 7; i++){ int offLED = i - 1; //Calculate which LED was turned on last time through if(i == 0) { //for i = 1 to 7 this is i minus 1 (i.e. if i = 2 we will offLED = 7; //turn on LED 2 and off LED 1) } //however if i = 0 we don't want to turn of led -1 (doesn't exist) //instead we turn off LED 7, (looping around) digitalWrite(ledpin[i], HIGH); //turn on LED #i digitalWrite(ledpin[offLED], LOW); //turn off the LED we turned on last time delay(delayTime); } } /* * inAndOut() - This will turn on the two middle LEDs then the next two out * making an in and out look */ void inAndOut(){ int delayTime = 100; //the time (in milliseconds) to pause between LEDs //make smaller for quicker switching and larger for slower //runs the LEDs out from the middle for(int i = 0; i <= 3; i++){ int offLED = i - 1; //Calculate which LED was turned on last time through if(i == 0) { //for i = 1 to 7 this is i minus 1 (i.e. if i = 2 we will offLED = 3; //turn on LED 2 and off LED 1) } //however if i = 0 we don't want to turn of led -1 (doesn't exist) //instead we turn off LED 7, (looping around) int onLED1 = 3 - i; //this is the first LED to go on ie. LED #3 when i = 0 and LED //#0 when i = 3 int onLED2 = 4 + i; //this is the first LED to go on ie. LED #4 when i = 0 and LED //#7 when i = 3 int offLED1 = 3 - offLED; //turns off the LED we turned on last time int offLED2 = 4 + offLED; //turns off the LED we turned on last time digitalWrite(ledpin[onLED1], HIGH); digitalWrite(ledpin[onLED2], HIGH); digitalWrite(ledpin[offLED1], LOW); digitalWrite(ledpin[offLED2], HIGH); delay(delayTime); } //runs the LEDs into the middle for(int i = 3; i >= 0; i--){ int offLED = i + 1; //Calculate which LED was turned on last time through if(i == 3) { //for i = 1 to 7 this is i minus 1 (i.e. if i = 2 we will offLED = 0; //turn on LED 2 and off LED 1) } //however if i = 0 we don't want to turn of led -1 (doesn't exist) //instead we turn off LED 7, (looping around) int onLED1 = 3 - i; //this is the first LED to go on ie. LED #3 when i = 0 and LED //#0 when i = 3 int onLED2 = 4 + i; //this is the first LED to go on ie. LED #4 when i = 0 and LED //#7 when i = 3 int offLED1 = 3 - offLED; //turns off the LED we turned on last time int offLED2 = 4 + offLED; //turns off the LED we turned on last time digitalWrite(ledpin[onLED1], HIGH); digitalWrite(ledpin[onLED2], HIGH); digitalWrite(ledpin[offLED1], LOW); digitalWrite(ledpin[offLED2], HIGH); delay(delayTime); } void loop(); // read the value from the sensor: sensorValue = analogRead(sensorPin); // turn the ledPin on digitalWrite(ledpin, HIGH); // stop the program for <sensorValue> milliseconds: delay(sensorValue); // turn the ledPin off: digitalWrite(ledpin, LOW); // stop the program for for <sensorValue> milliseconds: delay(sensorValue); } }

Dold text

Tack på förhand

Permalänk
Medlem

- ledpin() är deklarerad som en funktion, men senare i koden försöker du komma åt sen som en array.
- setup() står som en funktionsdeklaration enbart, sen har du kod direkt efter som inte ligger i någon funktion alls (det är inte tillåtet)
- ledpin_1..ledpin_x verkar du inte använda, misstänker att du vill att dessa ska vara en array ledpin[]

Om du försöker kompilera detta kommer du säkert få lite bra tips på vad som behöver fixas. Se till att skruva upp alla varningar till max. Försök lösa felen från kompilatorn på egen hand, fråga här igen om du stöter på problem.

Permalänk
Skrivet av pelleplu:

- ledpin() är deklarerad som en funktion, men senare i koden försöker du komma åt sen som en array.
- setup() står som en funktionsdeklaration enbart, sen har du kod direkt efter som inte ligger i någon funktion alls (det är inte tillåtet)
- ledpin_1..ledpin_x verkar du inte använda, misstänker att du vill att dessa ska vara en array ledpin[]

Om du försöker kompilera detta kommer du säkert få lite bra tips på vad som behöver fixas. Se till att skruva upp alla varningar till max. Försök lösa felen från kompilatorn på egen hand, fråga här igen om du stöter på problem.

Tack för svar. Får kolla detta när jag får tid

Permalänk
Medlem

Vi ska få våra ardino's snart, funderar på att köpa en egen också..

Visa signatur

CPU: Ryzen 9 3900x Noctua NH-D14 MOBO: TUF Gaming X570-PLUS GPU: GTX 980 RAM: 32 GB 3200 MHz Chassi: R4 PSU: Corsair AX860 Hörlurar: SteelSeries 840 Mus: Logitech G502 Lightspeed V.v. nämn eller citera mig för att få svar.

Permalänk
Skrivet av Haptic:

Vi ska få våra ardino's snart, funderar på att köpa en egen också..

Nice