//
// State machine example with two parallel state machines.
// State machine A is responsible for motor control.
// State machine B is responsible for LED control.
//
int stateA, stateB;
unsigned long t0_A, t0_B;
void setup()
{
// Set up pins etc
// Begin both state machines in state 1
setStateA(1);
setStateB(1);
// Open serial connnection to PC
Serial.begin(9600);
}
void loop()
{
int cs;
// Read colour sensor
cs = analogRead(0);
// State machine A - motor control
if (stateA == 1) // pivot forward and left
{
// State 1 actions
setMotors(0, 1); // LM stop, RM forward
// State transition?
if (cs < 512) setStateA(2);
}
else if (stateA == 2) // pivot forward and right
{
// State 2 actions
setMotors(1, 0); // LM forward, RM stop
// State transition?
if (cs > 512) setStateA(1);
}
// State machine B - LED control
if (stateB == 1) // LED on
{
// State 1 actions
setLEDs(1); // LED on
// State transition?
// 0.5s elapsed -> state 2
if (millis() - t0_B > 500) setStateB(2);
}
else if (stateB == 2) // LED off
{
// State 2 actions
setLEDs(0); // LED off
// State transition?
// 1.0s elapsed -> state 1
if (millis() - t0_B > 1000) setStateB(1);
}
}
void setStateA(int s)
{
stateA = s;
t0_A = millis();
Serial.print(t0_A);
Serial.print(" stateA = ");
Serial.println(stateA);
}
void setStateB(int s)
{
stateB = s;
t0_B = millis();
Serial.print(t0_B);
Serial.print("stateA = ");
Serial.println(stateA);
}
void setMotors(int LM_direction, int RM_direction)
{
digitalWrite(2, LM_direction > 0);
digitalWrite(3, LM_direction < 0);
digitalWrite(4, RM_direction > 0);
digitalWrite(5, RM_direction < 0);
}
void setLEDs(unsigned int current_state)
{
digitalWrite(6, current_state & 1);
digitalWrite(7, current_state & 2);
digitalWrite(8, current_state & 4);
digitalWrite(9, current_state & 8);
}
Copyright © 2010-2022 Ted Burke, All Rights Reserved.
Blog Stats
- 94,869 hits
-
Recent Posts
- State machine example from today’s class
- State Machine code examples
- Example code: two state machines running in parallel
- State machine examples
- Dual HC-SR04 rangefinder example for Arduino Nano
- Mini project brainstorming
- State machine example from today’s class
- State machine example from lecture on 12 Nov 2020
- State machine example from class – “Shoo Robot”
- State machine example – Golf ball collector
- SCARA kinematics – example M-file and whiteboard snapshot
- Kinematics and mini-project ideas – whiteboard snapshots
- HC-SR04 ultrasonic rangefinder example code for dsPIC30F4011
- Slaughterbots video
- Two alternative state machine structures with Timer 1 millisecond counter
- A handy “super example” for the dsPIC30F4011
- Millisecond timer for state machine using Timer 1 interrupt
- Two SCARA control examples from today’s class
- Stepper motor reference position example from today’s class
- Great video about the 28BYJ-48 stepper motor
- Timer Interrupt examples for dsPIC30F4011
- Stepper robot example from today’s class
- State Machine Challenge
- State Machine example code from class (two versions)
- Example: control servo angle with a potentiometer
- SCARA fixed sequence example code
- UART printing example for dsPIC30F4011 with stepper controlled by potentiometer
- Simple analog input example for dsPIC30F4011
- Speed control of a DC motor using the dsPIC30F4011
- Simple stepper motor control using the dsPIC30F4011
- Micro-robots
- Servo angle controlled by an analog voltage using dsPIC30F4011
- Explanation of a bit-banging PWM example
- Best of luck in the exam tomorrow!
- Using Python and Tkinter to send keystrokes to a dsPIC via serial
- Example Blog Post
- Analysing some audio data recorded using the dsPIC
- Microphone amplifier circuit for dsPIC
- Inserting quotes and blockquotes in a WordPress blog post
- Snapshot of brainstorming ideas from whiteboard
- Some interesting robotics videos
- Introducing Robotics and Industrial Electronics
- Servo control example from today’s class using output compare
- Servo control using the dsPIC Output Compare module
- Stepper motor example from today’s class
- Servo control by dsPIC using bit banging
- Musical dsPIC Example
- Basic RoboSumo template code
- Best of luck!
- DirectShow machine vision – work in progress