Programming Problems with an Arduino based project.

Sep 5, 2011 at 6:04pm

Programming Problems with an Arduino based project.

First of all – please let me know if I’m in the wrong forum – I think I can post here to reach ‘All’.

I am an Ma student in Spatial Design that have run into some problems with the programming for this Arduino based project. The piece relays on a set of 20 electromagnets to switch on and off the one after the other with a short overlapping time so that they can hand over magnetic material from one to another.

I am using an Arduino 2560 Mega – 20 electromagnets connected to the 0-19 outputs.

I do not have a great deal of experience with programming so have been given some help. The problem I am having is that the magnet number does not increase – if I do list 0 as the first magnet only this magnet will go on and off – being off during the overlap time. If I don’t list 0 as a magnet the magnet connected to output 0 will stay on indefinitely and the magnet connected to output 1 will switch on and off – being off during the overlap time. The rest of the magnets never switch on… I can only assume that the code that is setting ‘the next magnet’ is wrong. The idea is that the magnet number should increase up till 20 and then reset.

I’ll paste the code here, hopefully someone can give me a helping hand..

//////////////////////////////
// initialise all variables.//
//////////////////////////////
int magnets[] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20}; //sets up an array for 20 magnets/pin numbers – for more just ad them to the end.
int currentMagnet = 0; //this is the array number, though the pins start at 1 arrays start at zero… Confusing!!!
int nextMagnet = currentMagnet+1; //sets the next magnet number

// create variabe to track time – must be declared as an unsigned long variable type
unsigned long onDuration = 1000; // this is the duration magnet
unsigned long timeOff; // this is the time at which the magnet should be switched off
unsigned long timeOverlap = 250; // this is the overlap time, when both magnets are on.

///////////////////////////////////////////
//initialise pins and enable first magnet//
///////////////////////////////////////////

void setup () {
//the ‘for’ section cycles through all the pins and sets them to output and initially turns them all off.
int i;
for (i = 0; i < 19; i = i + 1) { //arrays start on 0 so the < value should be one less than the number of magnets.
// set the pins declared above to output
pinMode(magnets[i], OUTPUT);
//sets each pin to low (off) state
digitalWrite(magnets[i], LOW);
}

// Set the times for the first magnet – starts the process going
digitalWrite(magnets[currentMagnet], HIGH); // turns on the magnet on pin 1 (in this case)
timeOff = millis()+onDuration; // sets the time to turn off the current Magnet
}

/////////////////////////////////////////////////
//start the loop – should continue indefinately//
/////////////////////////////////////////////////

void loop () {

//tests when to switch on the next magnet
if(millis()>=(timeOff-timeOverlap)) { //test the current time to see if it is equal or greater than the current magnet off-time – the overlap.
digitalWrite(magnets[nextMagnet], HIGH); //turns on the next magnet
}

//detects if the current magnet should be switched off
if(millis()>=timeOff) {
digitalWrite(magnets[currentMagnet], LOW); //turns off the current magnet
currentMagnet = nextMagnet; // makes the current magnet now the next magnet
timeOff = millis()+onDuration;

// set the number for the next magnet
nextMagnet = currentMagnet+1;
//to ensure that the magnest cycle, if the nextMagnet value exceeds the number of magnets in the array we need to reset it.
if(nextMagnet=20) {
nextMagnet = 0;
}
}
}

Very much hope that someone can help – I am lost…

/Daniel

#58758
Sep 5, 2011 at 6:33pm

Do you plan on using it with your computer? If so it might be easier/better handling your code/logic IN max using Maxuino or something similar.

Barring that, I’ve always found the folks on the Arduino forum quite helpful (arduino.cc).

#211226
Sep 5, 2011 at 6:38pm

Hi Rodrigo!

I have got a 9V power adapter which will power the arduino and a separate power supply which powers the electromagnets – as they need allot more power.

Any ideas on what I’m doing wrong?

#211227
Sep 5, 2011 at 6:57pm

This is better posted at arduino.cc, unless you have a question regarding Max/MSP.

#211228

You must be logged in to reply to this topic.