Table of Contents

SimpleTon

~~META:description abstract=AVR based IO interface / automaton~~ microcontroller sensors

AVR based IO interface or automaton versatile and extensible device. Up to 256 digital inputs, 256 digital outputs, 8 analog inputs and 2 analog outputs.

Features

About digital input modules

About digital output modules

About analog input module

About analog output module

SimpleTon:graphlg

This is a grafcet-like graphical programming language for SimpleTon.

Core features :

SimpleTon:graphlg allows to define aliases for any expression, this is pretty much of a C define functionnality. Extenstion librairies can also expose their own aliases and functions.

Notations

IO

Internals

Supported graphical elements

Electronics

simpleton_motherboard.svg

simpleton_analog_input_module.svg

simpleton_analog_output_module.svg

simpleton_digital_input_module.svg

simpleton_digital_output_module.svg

simpleton_digital_output_with_relays_module.svg

simpleton_backbone.svg

Source code

main.c
// Source code

How to

Manual

Extensions

RGB color sensor

This is an inexpensive 8 colors sensor with digital and analog output.

It is based on light reflexion measurement :

Dedicated library for SimpleTon:graphlg.

Features

SimpleTon:graphlg library

{
	"name": "Simple RGB color sensor",
	"aliases": [
		"COLOR_NONE": 0,
		"COLOR_BLUE": 1,
		"COLOR_GREEN": 2,
		"COLOR_CYAN": 3,
		"COLOR_RED": 4,
		"COLOR_PURPLE": 5,
		"COLOR_YELLOW": 6,
		"COLOR_WHITE": 7
	],
	"functions": [
		{
			"name": "color",
			"args": ["I", "I", "I"],
			"call": "simple_rgb_sensor_digital_color"
		},
		{
			"name": "color",
			"args": ["AI"],
			"call": "simple_rgb_sensor_analog_color"
		}
	]
}

Electronics

rgb_sensor.svg

Source code

main.c
//====================== Simple RGB sensor : main.c ========================
// MELEARD Etienne
// Year 2012
// ATMega8L @ 8MHz (internal RC oscillator)
//==========================================================================
 
 
 
//============================== WARNING ==================================//
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
//                                                                         //
// Fuses must be like -U lfuse:w:0xc4:m -U hfuse:w:0xd1:m                  //
//                                                                         //
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
//=========================================================================//
 
 
 
// ========================================= //
// ==         INCLUDES & MACROS           == //
// ========================================= //
 
#include <avr/io.h>
#include <sup_avr/io2.h>
#include <avr/interrupt.h>
#include <util/delay.h>
 
 
#ifndef cbi
	#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
#endif
 
#ifndef sbi
	#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
#endif
 
 
// ========================================= //
// ==             DEFINITIONS             == //
// ========================================= //
 
// ================= FUSES ================= //
 
// Run avrdude -c usbasp -p m8 -U lfuse:w:0xc4:m -U hfuse:w:0xd1:m at first (-F may be needed)
 
 
// ================ PINOUTS ================ //
 
#define LED_R	Bit(PORTD).bitX
#define LED_G	Bit(PORTD).bitY
#define LED_B	Bit(PORTD).bitZ
 
#define OUT_R	Bit(PORTD).bit2	// MUST be continous
#define OUT_G	Bit(PORTD).bit1
#define OUT_B	Bit(PORTD).bit0
 
#define LDR_CH	ADCX
 
 
// =============== CONSTANTS =============== //
 
#define false 0
#define true 1
 
#define OFF 0
#define ON 1
 
#define LDR_SETTIME	100	// ms
 
#define COLOR_NONE	0
#define COLOR_BLUE	1
#define COLOR_GREEN	2
#define COLOR_CYAN	3
#define COLOR_RED	4
#define COLOR_PURPLE	5
#define COLOR_YELLOW	6
#define COLOR_WHITE	7
 
 
// ================= INITS ================= //
 
void init(void) {
	cli();
 
	// Outputs
 
	// ADC
	ADCSRA = 0x80;	// Manual mode, 64 prescaler (0.1ms/conv)
	ADMUX = LDR_CH;	// Right adjust, external ref, XX channel
 
	sei();
}
 
 
// =============== FUNCTIONS =============== //
 
uint16_t getLevel(void) {
	ADCSRA |= 1<<ADSC;		// Start conversion
	while(ADCSRA & (1<<ADSC));	// Wait for conversion to end
	return ADC;	// °C
}
 
void setColor(uint8_t c) {
	LED_R = (c & COLOR_RED);
	LED_G = (c & COLOR_GREEN);
	LED_B = (c & COLOR_BLUE);
}
 
uint16_t getLevelForColor(uint8_t c) {
	setColor(c);
	_delay_ms(LDR_SETTIME);	// Wait for LDR to react
	return getLevel();
}
 
uint16_t max(uint16_t a, uint16_t b) {
	if(a > b) return a;
	return b;
}
 
 
// ============= MAIN FUNCTION ============= //
 
int16_t main(void) {
	uint8_t color;
	uint16_t red, green, blue, base, trig;
 
	// Set up
	init();
 
	// Startup dance
	setColor(COLOR_RED);
	_delay_ms(250);
	setColor(COLOR_GREEN);
	_delay_ms(250);
	setColor(COLOR_BLUE);
	_delay_ms(250);
	setColor(COLOR_NONE);
	_delay_ms(250);
 
	while(1) {
		// Get no-component legel for compensation
		base = getLevelForColor(COLOR_NONE);
 
		// Get compensated components levels
		red = getLevelForColor(COLOR_RED) - base;
		green = getLevelForColor(COLOR_GREEN) - base;
		blue = getLevelForColor(COLOR_BLUE) - base;
 
		// Level above which component is considered as present
		trig = max(max(red, green), blue) / 2;
 
		// Compute color
		color = COLOR_NONE;
		if(red > trig) color += COLOR_RED;
		if(green > trig) color += COLOR_GREEN;
		if(blue > trig) color += COLOR_BLUE;
 
		// Send value
		PORTN &= 0xMASK;
		PORTN |= color;
	}
 
	return 0;
}