Advertisement
Guest User

Código Marcos_Rev1.0

a guest
Aug 21st, 2014
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.45 KB | None | 0 0
  1. #include <EEPROM.h>
  2.  
  3. //Variáveis do Projeto
  4. int Ans1,Ans2,Ans3,Ans4,Ans5,Ans6,Ans7,Ans8,Ans9;
  5. int x ;
  6. char buffer[18];
  7.  
  8. //Prototipação das funções
  9. void splitString(char* data);
  10. void setLED(char* data);
  11.  
  12. void setup(){
  13.     Serial.begin(9600);
  14.     pinMode(3,OUTPUT);
  15.     pinMode(5,OUTPUT);
  16.     pinMode(6,OUTPUT);
  17.     pinMode(9,OUTPUT);
  18.     pinMode(10,OUTPUT);
  19.     pinMode(11,OUTPUT);
  20.     pinMode(13,OUTPUT);
  21.    
  22.     //Para ler na inicialização
  23.     x = EEPROM.read(0);
  24.     Serial.print("Valor lido ");
  25.     Serial.println(x);
  26. }
  27. void loop()
  28. {
  29.     if (Serial.available() > 0) {
  30.         int index=0;
  31.         delay(100); // permite o buffer encher
  32.         int numChar = Serial.available();
  33.     }
  34.     if (numChar>15) {
  35.         numChar=15;
  36.     }
  37.     while (numChar--) {
  38.         buffer[index++] = Serial.read();
  39.     }
  40.     splitString(buffer);
  41. }
  42.  
  43. //Subrotinas do projeto prototipar na declaração
  44. //da seguinte maneira tipo nomeFuncao (parametros);
  45. void splitString(char* data) {
  46.     char* parameter;
  47.     parameter = strtok (data, " ,");
  48.     while (parameter != NULL) {
  49.         setLED(parameter);
  50.         parameter = strtok (NULL, " ,");
  51.     }
  52.     // Limpa os textos e o buffer serial
  53.     for (int x=0; x<16; x++) {
  54.         buffer[x]='\0';
  55.     }
  56.     Serial.flush();
  57. }
  58.  
  59. void setLED(char* data) {
  60.  
  61.     int Ans1,Ans2,Ans3,Ans4,Ans5,Ans6,Ans7,Ans8,Ans9;
  62.  
  63.     if (data[0] == 'a') {
  64.         Ans1 = strtol(data+1, NULL, 10);
  65.         Ans1 = constrain(Ans1,0,9999);
  66.     }
  67.  
  68.     if (data[0] == 'b') {
  69.         Ans2 = strtol(data+1, NULL, 10);
  70.         Ans2 = constrain(Ans2,0,9999);
  71.     }
  72.  
  73.     if (data[0] == 'c') {
  74.         int Ans3 = strtol(data+1, NULL, 10);
  75.         Ans3 = constrain(Ans3,0,9999);
  76.     }
  77.  
  78.     if (data[0] == 'd') {
  79.         Ans4 = strtol(data+1, NULL, 10);
  80.         Ans4 = constrain(Ans4,0,255);
  81.         analogWrite(3, Ans4);
  82.     }
  83.  
  84.     if (data[0] == 'e') {
  85.         Ans5 = strtol(data+1, NULL, 10);
  86.         Ans5 = constrain(Ans5,0,255);
  87.         analogWrite(5, Ans5);
  88.     }
  89.     if (data[0] == 'f') {
  90.         Ans6 = strtol(data+1, NULL, 10);
  91.         Ans6 = constrain(Ans6,0,255);
  92.         analogWrite(6, Ans6);
  93.     }
  94.  
  95.     if (data[0] == 'g') {
  96.         Ans7 = strtol(data+1, NULL, 10);
  97.         Ans7 = constrain(Ans7,0,255);
  98.         analogWrite(9, Ans7);
  99.     }
  100.  
  101.     if (data[0] == 'h') {
  102.         Ans8 = strtol(data+1, NULL, 10);
  103.         Ans8 = constrain(Ans8,0,255);
  104.         analogWrite(10, Ans8);
  105.     }
  106.  
  107.     if (data[0] == 'i') {
  108.         x = EEPROM.read(0);
  109.         Ans9 = strtol(data+1, NULL, 10);
  110.         Ans9 = constrain(Ans9,0,255);
  111.         x = Ans9;
  112.         EEPROM.write(0,x);
  113.         analogWrite(11, x);
  114.         Serial.println(x);
  115.     }
  116.      
  117.     if(Ans9 == 255){
  118.       digitalWrite(13,HIGH);
  119.     }
  120.  
  121.     if(data[0] == 'x'){
  122.         digitalWrite(13,LOW);
  123.     }
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement