Hi folks,
Im a technology teacher in Scotland and the powers above have decided that we move from our old PBASIC run Stamp boards, and use Arduino. It has also been decided this will start of next month! this means during my holidays I have been left with no choice but to learn the language and how it works myself, but to create a scheme of work for my pupils to follow. Most of what Ive done is based on Jeremy Blums brilliant online tutorials , and things that are different I was mostly able to work out by looking up.
I have came across a problem though and for the life of my I cant seem to solve it and I was hoping someone on here could help! Ive designed a project of dancing ballerina toy (my feeble attempts to try and get more girls into Engineering). When a master switch is pressed, it will put on an ultabrite LED to act as a spotlight for the dancer. The ballerina will then "dance" by sitting on top of a turntable that will rotate one way until it hits a limit switch. When it hits this limit switch it will rotate the other direction until it hits another limit switch, sending it back the original direction. This dancing will repeat 5 times, then everything will switch off.
This is a flowchart of what I'm attempting to do, which will hopefully make things a little clearer:
This is how I have wired up my Arduino Uno:
and this is my attempt at the code. It was originally more complicated added in debouncing and commands to keep outputs on once the switches have been presssed, but I deleted all that to try and work out what is going wrong.
/*
dancing toy System
*/
int MasterSwitchPin = 13; // Master switch
int LeftSwitchPin = 12; // Makes motor turn left
int RightSwitchPin = 8; // Makes motor turn right
int Motor1APin = 7; // H-bridge leg 1
int Motor2APin = 6; // H-bridge leg 2
int LEDPin = 2; // LED
void setup()
{
Serial.begin(9600);
pinMode (MasterSwitchPin, INPUT);
pinMode (LeftSwitchPin, INPUT);
pinMode (RightSwitchPin, INPUT);
pinMode (Motor1APin, OUTPUT);
pinMode (Motor2APin, OUTPUT);
pinMode (LEDPin, OUTPUT);
}
void loop()
{
if (digitalRead(MasterSwitchPin) == HIGH)
{
digitalWrite(LEDPin, HIGH);
}
Serial.print("button pressed LED on");
for (int counter = 1 ; counter <= 5; counter = counter +1)
{
while (digitalRead(LeftSwitchPin) == HIGH)
{
digitalWrite(Motor1APin, HIGH);
digitalWrite(Motor2APin, LOW);
Serial.print("motor goes one way");
}
delay(1000);
while (digitalRead(RightSwitchPin) == HIGH)
{
digitalWrite(Motor1APin, LOW);
digitalWrite(Motor2APin, HIGH);
Serial.print("motor goes the other wat way");
}
delay(1000);
}
Serial.print("repeated 5 times");
digitalWrite(LEDPin, LOW);
digitalWrite(Motor1APin, LOW);
digitalWrite(Motor2APin, LOW);
Serial.print("switch everything off");
}
can anyone help me please and fix my code? thanks in advance!!!